Skip to content

Read and fill PDF forms using PDF4NET

In a previous article we have shown how to create PDF forms using PDF4NET. Your application logic might require to fill these forms with information or if they have already been filled by end users you might need to read the form data and store it in a database for additional processing.

Loading a PDF form

In order to process an existing PDF form you load it in a PDFFixedDocument object.

C#
PDFFixedDocument document = new PDFFixedDocument("form.pdf");

Once the form has been loaded, the Fields collection of the document's Form and the Fields collection of each page are automatically populated with the corresponding field.
The fields in the collection are accessible through their name or index.

C#
PDFTextBoxField fnTextBox = document.Form.Fields["firstName"] as PDFTextBoxField;
PDFCheckBoxField chkAgree = document.Form.Fields["agree"] as PDFCheckBoxField;

Fill and read the form fields

The value of a PDF form field can read or written using the generic Value property of object type available in the PDFField class. Each field type provides additional properties that let you access its value in a typed manner.

Text fields

Text fields are used to store string values. The PDFTextBoxField class provides the Text property of type string for reading and writing the field value.

C#
textBox.Text = "John Doe";
string name = textBox.Name;

For text fields, when using the Value property, the provided value is converted to string using the value's ToString() method.

C#
1
2
3
textBox.Value = someObject;
// is equivalent to
textBox.Text = someObject?.ToString();

List Box fields

List box fields store a collection of list items where each item has a Value and a Name that is displayed in the list box. The value of a list box field is the index of the selected item.
The PDFListBoxField class provides the SelectedIndex property of type int for reading and writing the field value.

C#
1
2
3
// Select first item in the list
listBox.SelectedIndex = 0;
int selectedItemIndex = listBox.SelectedIndex;

The SelectedIndex property is -1 if there is no selected item.
The SelectedItem property gives you directly the selected item.

C#
1
2
3
4
5
6
7
PDFListItem item = listBox.SelectedItem;
if (item != null)
{
    Console.WriteLine($"Selected item is {item.Name}");
}
// Select the last item in the list
listbox.SelectedItem = listBox.Items[listBox.Items.Count - 1];

An exception is thrown if the value provided to SelectedItem property is not null and it does not exist in the Items collection.
If the list box allows multiple selected items (MultiSelect property is true), they can be accessed using the SelectedIndexes property of type int[].

C#
1
2
3
4
5
6
7
8
// Select the first 3 items in the list.
listBox.SelectedIndexes = new int[] {0, 1, 2};
// Get the list of selected items
int[] selectedItemIndexes = listBox.SelectedIndexes;
if (selectedItemIndexes != null)
{
    // Process the selected items.
}

For list box fields, when using the Value property, the accepted value types are int, int[] and PDFListItem. If the value is int then the SelectedIndex property is set to that value. If the value is int[] then the SelectedIndexes property is set to that value. If the value is PDFListItem then the SelectedItem property is set to that value.

Combo Box fields

Combo box fields store a collection of list items where each item has a Value and a Name that is displayed in the combo box. The value of a combo box field is the index of the selected item.
The PDFDropDownListField class provides the SelectedIndex property of type int for reading and writing the field value.

C#
1
2
3
// Select first item in the combo box
comboBox.SelectedIndex = 0;
int selectedItemIndex = comboBox.SelectedIndex;

The SelectedIndex property is -1 if there is no selected item.
The SelectedItem property gives you directly the selected item.

C#
1
2
3
4
5
6
7
PDFListItem item = comboBox.SelectedItem;
if (item != null)
{
    Console.WriteLine($"Selected item is {item.Name}");
}
// Select the last item in the list
comboBox.SelectedItem = comboBox.Items[listBox.Items.Count - 1];

An exception is thrown if the value provided to SelectedItem property is not null and it does not exist in the Items collection.
If the combo box is editable (Edit property is true) then any string value can be stored in the field, similar to a text field.
For combo box fields, when using the Value property, the accepted value types are int, PDFListItem and if the combo box is editable, any object. If the value is int then the SelectedIndex property is set to that value. If the value is PDFListItem then the SelectedItem property is set to that value. If the value is any object and the combo box is editable then the object is converted to string and stored in the combo box.

Push-button fields

Push-button fields are purely interactive controls that do not store a value. They cannot be read or written.

Checkbox fields

Checkbox fields are used for representing on/off states.
The PDFCheckBoxField class provides the Checked property of type bool for reading and writing the field value.

C#
1
2
3
4
5
6
checkBox.Checked = true;
bool isChecked = checkBox.Checked;
if (isChecked)
{
    Console.WriteList("I Agree");
} 

The actual value stored in the field is the ExportValue of the widget that is actually checked. When the field is not checked, no value is stored.
For checkbox fields, when using the Value property, the accepted value types are bool and object. If the value is bool then the Checked property is set to that value. If the value is an object then the object is converted to string and the widget whose ExportValue matches the string is checked. If there is no match then the checkbox is unchecked.

Radio button fields

Radio button fields contain a set of related button items that can each be on or off. Typically, at most one radio button item in a set may be on at any given time, and selecting any one of the buttons automatically deselects all the others.
The PDFRadioButtonField class provides the SelectedIndex property of type int for reading and writing the field value.

C#
1
2
3
4
// Select the first radiobutton item
radioButton.SelectIndex = 0;
// Get the radiobutton item that is selected
int selectedIndex = radioButton.SelectedIndex;

SelectedIndex is -1 when there is no selection. The actual value stored in the field is the ExportValue of the widget that is actually checked. When the field is not checked, no value is stored.
The Checked property lets you test if the radio button is checked (if one of the radio button items is checked).
For radio button fields, when using the Value property, the accepted value types are bool, int and object. If the value is bool then the Checked property is set to that value (this actually checks the last radio button item in the collection). If the value is int then the SelectedIndex property is set to that value. If the value is an object then the object is converted to string and the radio button item whose ExportValue matches the string is checked. If there is no match then the radio button is unchecked.

Signature fields

Signature fields represent digital signatures and optional data for authenticating the name of the signer and the document’s contents.
The PDFSignatureField class provides the Signature property of type PDFBaseDigitalSignature for reading and writing the field value.
Digital signatures are a complex subject and they are documented in the DigitalSignatures section.

Sample code

The complete FormFill sample is available online on GitHub.

Low overhead PDF form filling and reading

When a PDF file is loaded into a PDFFixedDocument object, it is transformed into a complex object model to allow easy manipulation of the PDF file. This can add a lot of overhead when working with large PDF file. If your intent is just to fill a PDF form, then using PDFFileEx class and SetFieldValue method will reduce the overhead when working with large files.
The SetFieldValue takes 2 parameter, the field name and its value. The value is handled according to the rules for PDFField.Value property presented in the previous section.

Sample code

The rewritten FormFill sample using PDFFileEx class and SetFieldValue method is also available online on GitHub.

Import and export of PDF form data

PDF forms can be read or filled in bulk using form import/export operations. Import operations let you fill the PDF form data from a file or a stream while the export operations let you write the form data to a file or stream.
The PDFForm class includes methods for handling the import/export operations.

C#
1
2
3
4
// Import the form data from a FDF file
document.Form.ImportData("data.fdf", PDFFormDataFormat.FDF);
// Export the form data to XML
document.Form.ExportData("data.xml", PDFFormDataFormat.XML, "document.pdf");

The supported form data formats for import/export operations are: FDF, XFDF and XML.