Skip to main content
A newer version of this page is available. .

Fill Interactive Forms

  • 3 minutes to read

This document consists of the following sections:

Fill Interactive Form using Form Field Names

To load a PDF document that contains an interactive form, create an instance of the PdfDocumentProcessor object and call one of the PdfDocumentProcessor.LoadDocument overloaded methods.

Calling the PdfDocumentProcessor.GetFormData method acquires the interactive form data from a PDF document. This method returns a PdfFormData object that represents interactive form data.

Note

Do not use a previously obtained PdfFormData instance after the interactive form structure is changed. For example, after one of the following operations is applied: adding new interactive form fields, interactive form flattening, merging documents with interactive forms, or deleting pages with interactive form fields. You can obtain the new PdfFormData instance by calling the PdfDocumentProcessor.GetFormData method.

To set or update interactive form field values, provide the field name as a key (e.g., data[“FieldName1”].Value = “SomeValue1”).

To specify a value for a form field, use the PdfFormData.Value property.

Important

The value specified for an interactive form field can be a string for text fields, a name of the checked/unchecked appearance for radio buttons and check boxes, an array of strings (for multi select fields) or a PdfFormData object (for the complex fields for which the field name is provided as a key).

When all values have been set, call the PdfDocumentProcessor.ApplyFormData method to apply form data to the interactive form.

Call one of the PdfDocumentProcessor.SaveDocument overloaded methods to commit these changes to a PDF document.

See the code snippet below.

using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
      processor.LoadDocument(pathForPdf);
      PdfFormData data = processor.GetFormData();
      data["Your_Field_Name_1"].Value = "Some Value 1";
      data["Your_Field_Name_2"].Value = "Some Value 2";
      data["Your_Field_Name_3"].Value = "Some Value 3";
      processor.ApplyFormData(data);
      processor.SaveDocument(pathForPdf);
}

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T210253.

If required, you can easily retrieve a list of field name by iterating through the string collection returned by the PdfDocumentProcessor.GetFormFieldNames method.

foreach (string name in formData.GetFieldNames())
{
      object value = formData[name].Value;
}

Fill an Interactive Form using Data File

If you have a file with interactive form data (Fdf, Xml, Xfdf or Txt), you can fill the PdfFormData object directly from this file. To do this, pass the name of the data file and format when creating a PdfFormData object.

Note

If you omit the data format parameter, the PDF Document API component automatically detects this type.

See the code snippet below.

using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
      processor.LoadDocument(pathForPdf);
      PdfFormData data = new PdfFormData(pathForData); //automatic detection of type
      processor.ApplyFormData(data);
      processor.SaveDocument(pathForPdf);
}
See Also