Form Filling

DynamicPDF Core Suite supports working with AcroForms while merging. A form can have its values completed programmatically one at a time, its field values reorganized, or complete a form's values using an FDF file.

Refer to the Interactive Forms topic for documentation on creating a form.

AcroForm Filling

AcroForm field values are set as they are merged into a new PDF document. Obtain the form fields using a MergeDocument instance's Form property. The existing document is read into the MergeDocument instance, the form field values are completed, and then the document is saved as a new document.

After completing a form, the document is flattened. You cannot modify a form after being completed.

MergeDocument.Form

AcroForm fields are merged using the Form property of the MergeDocument class. The Form class - inherited from the Document class - has a Fields property, a FormFieldList that contains the FormField instances. The FormFieldList contains zero or more FormField instances. A Form instance uses the Value property to obtain the FormField instance's value.

Unicode values are fully supported. When completing a form programmatically, the specified value must match the export value for the form field. The following example illustrates setting AcroForm's values.

MergeDocument document = new MergeDocument("form-example.pdf");
document.Form.Fields["check_box_nm"].Value = "Yes";
document.Form.Fields["combo_box_nm"].Value = "Two";
document.Form.Fields["list_box_nm"].Value = "Three";
document.Form.Fields["radio_button_name"].Value = "ghi";
document.Form.Fields["text_field_name"].Value = "This is a text box.";
document.Draw(outputPath);
Dim document As New MergeDocument("form-example.pdf")
document.Form.Fields("check_box_nm").Value = "Yes"
document.Form.Fields("combo_box_nm").Value = "Two"
document.Form.Fields("list_box_nm").Value = "Three"
document.Form.Fields("radio_button_name").Value = "ghi"
document.Form.Fields("text_field_name").Value = "This is a text box."
document.Draw(outputPath)  

form filling Figure 1. Completing a form.

Filling Reorganized Acro Form Fields

You can reorganize form fields as they are being merged. This reorganization prevents renaming issues and helps organize form field data. The following example illustrates merging two PDF documents containing form fields and reorganizing their names.

MergeDocument document = new MergeDocument("form-example.pdf", new MergeOptions(true, "form1"));
document.Append("form-example.pdf", new MergeOptions(true, "form2"));
document.Form.Fields["form1.text_field_name"].Value = "This is a text box.";
document.Form.Fields["form2.text_field_name"].Value = "This is another text box.";
document.Draw(outputPath);    
Dim document As New MergeDocument("form-example.pdf", New MergeOptions(True, "form1"))
document.Append("form-example.pdf", New MergeOptions(True, "form2"))
document.Form.Fields("form1.text_field_name").Value = "This is a text box."
document.Form.Fields("form2.text_field_name").Value = "This is another text box."
document.Draw(outputPath)   

In the previous example, both forms have a text_field_name field. To prevent a naming collision, the field in the first document prepends an form1 to the field name while the second document prepends an form2 to its field name.

Form Filing using FDF files

An FDF file is a text file format used for data exported from PDF form fields that contain only form field data, not a complete form. An AcroForm's fields can be completed using an FDF file through the ImportFormDataAction class.

The following example illustrates using an FDF file. Note that the completed PDF form is the form with the added action to import the FDF data upon the end-user opening the PDF.

The following FDF data is used to fill the form.

%FDF-1.2
%âãÏÓ
1 0 obj
<</FDF<</Fields[<</T(Submit)>><</T(descriptionField)/V(This is a description.)>><</T(nameField)/V(John Doe)>>]/ID[<CA4420574740BBED8326F4582D61BF85><CA4420574740BBED8326F4582D61BF85>]>>/Type/Catalog>>
endobj
trailer
<</Root 1 0 R>>
%%EOF

The path to the FDF file must be accessible to the end-user of the form.

MergeDocument document = new MergeDocument("simple-form-fill.pdf");
document.Pages[0].ReaderEvents.Open = new ImportFormDataAction("simple-form-fill_data.fdf");
document.Draw(outputPath);    
Dim document As New MergeDocument("simple-form-fill.pdf")
document.Pages(0).ReaderEvents.Open = New ImportFormDataAction("simple-form-fill_data.fdf")
document.Draw(outputPath)

form filling Figure 2. Completing a form with FDF data.

In this topic