Identifying form fields names

Aspose.PDF for .NET provides the capability to create, edit and fill already created Pdf forms. Aspose.Pdf.Facades namespace contains Form class, which contains the function named FillField and it takes two arguments i.e. Field name and field value. So in-order to fill the form fields, you must be aware of the exact form field name.

Implementation details

We often come across a scenario where we need to fill the form which is created in some tool i.e. Adobe Designer, and we are not sure about the form fields names. So in order to fill the form fields, first we need to read the names of all the Pdf form fields. Form class provides the property named FieldNames which returns the entire field’s names and returns null if PDF does not contain any field. However, when using this property, we get the names of the entire field’s in PDF form and we might not be certain that which name corresponds to which field over the form.

As a solution to this problem, we will use the appearance attributes of each field. Form class has a function named GetFieldFacade which returns attributes, including location, color, border style, font, list item and so on. To save these values we need to use FormFieldFacade class, which is used to record visual attributes of the fields. Once we have these attributes we can add a text field beneath every field which would be displaying the field name.

In Aspose.Pdf.Facades namespace we have a class named FormEditor which provides the capability to manipulate PDF forms. Open a pdf form; add a text field beneath every existing form field and save the Pdf form with new name.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void IdentifyFormFieldsNames()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdfFacades_TechnicalArticles();
    // First a input pdf file should be assigned
    var form = new Aspose.Pdf.Facades.Form(dataDir + "FilledForm.pdf");
    // Get all field names
    var allfields = form.FieldNames;
    // Create an array which will hold the location coordinates of Form fields
    var box = new System.Drawing.Rectangle[allfields.Length];
    for (int i = 0; i < allfields.Length; i++)
    {
        // Get the appearance attributes of each field, consequtively
        var facade = form.GetFieldFacade(allfields[i]);
        // Box in FormFieldFacade class holds field's location
        box[i] = facade.Box;
    }
    // Save PDF document
    form.Save(dataDir + "IdentifyFormFields_1_out.pdf");

    // Create PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "FilledForm - 2.pdf"))
    {
        // Now we need to add a textfield just upon the original one
        using (var editor = new Aspose.Pdf.Facades.FormEditor(document))
        {
            for (int i = 0; i < allfields.Length; i++)
            {
                // Add text field beneath every existing form field
                editor.AddField(Aspose.Pdf.Facades.FieldType.Text, 
                "TextField" + i, allfields[i], 1, 
                box[i].Left, box[i].Top, box[i].Left + 50, box[i].Top + 10);
            }
            // Save PDF document
            editor.Save(dataDir + "IdentifyFormFields_out.pdf");
        }
    }
}