Create PDF forms using PDF4NET
PDF4NET library supports the creation of PDF forms (Acro forms) both from code and visually using the included PDF viewer controls.
In this article we'll focus on creating PDF forms from code.
PDF forms
PDF forms are electronic forms embedded in PDF files, they consist of one or more PDF fields that allow data collection from end-users. Data can be stored in the form for later retrieval or it can be submitted to a server for further processing.
A PDF file can contain a single PDF form that spans across the entire document.
PDF fields
The PDF fields are the building blocks of PDF forms. Each field consists of a non-visual logical field and its visual representation on the PDF page called widget. A field can have one or more widgets as the same field can visually appear multiple times on the same page or on different pages.
Note
In A PDF document the PDF fields have document scope and not page scope. This means that the field name is unique at document level and what is seen in a viewer as 2 fields with the same name on the same page or on different pages is actually a single field with 2 visual representations (2 widgets).
In PDF4NET the logical field is represented by the PDFField class and its descendants while the field widgets are represented by the PDFFieldWidget class and its descendants.
The field widgets can be accessed through the field's Widgets property.
Some properties, such as name or value are defined at field level while other properties such as position or color are defined at widget level.
Supported field types
PDF forms support several types of fields, such as:
- Text
- List Box
- Combo Box
- Push-button
- Check Box
- Radio Button
- Signature
Text fields
Text fields are boxes or spaces in which the user can enter text from the keyboard.
The PDFTextBoxField class in PDF4NET library is used to create a text field.
| C# | |
|---|---|
After the field has been created, it is recommended to the added to the desired page before setting its properties.
| C# | |
|---|---|
After the field has been added to the page, its position on the page is set using the widget's DisplayRectangle property.
| C# | |
|---|---|
The default field properties make it blend with the page background so an end user might not see it. You can highlight the field by adding a border to it or by coloring its background.
| C# | |
|---|---|
The font used for editing and displaying the field content is set through the Font property.
| C# | |
|---|---|
The Value property can be used to set the initial field value.
| C# | |
|---|---|
Text fields can have also a DefaultValue, this is used to set the Value property when a Reset action is executed on the PDF form. The current Value is cleared and replaced with the DefaultValue.
List Box fields
List boxes are type of choice fields that contain several text items that can be scrolled.
The PDFListBoxField class in PDF4NET library is used to create a list box field.
| C# | |
|---|---|
After the field has been created, it is recommended to the added to the desired page before setting its properties.
| C# | |
|---|---|
After the field has been added to the page, its position on the page is set using the widget's DisplayRectangle property.
| C# | |
|---|---|
The default field properties make it blend with the page background so an end user might not see it. You can highlight the field by adding a border to it or by coloring its background.
| C# | |
|---|---|
The font used for displaying the list box items is set through the Font property.
| C# | |
|---|---|
New items can be added to the list box using the field's Items property that is a PDFListItemCollection object. A list item is an object that consists of a name and a value, the name being the string displayed in the list box.
The item that is selected initially can be set using the SelectedIndex property. The item can also be selected using the SelectedItem property.
| C# | |
|---|---|
List boxes support the selection of multiple items, this can be achieved by setting the MultiSelect property to true. In this situation the SelectedIndexes property is used to set the selected items.
Combo Box fields
Combo boxes are type of choice fields that contain several text items, at most one of which may be selected as the field value.
The PDFDropDownListField class in PDF4NET library is used to create a combo box field.
| C# | |
|---|---|
After the field has been created, it is recommended to the added to the desired page before setting its properties.
| C# | |
|---|---|
After the field has been added to the page, its position on the page is set using the widget's DisplayRectangle property.
| C# | |
|---|---|
The default field properties make it blend with the page background so an end user might not see it. You can highlight the field by adding a border to it or by coloring its background.
| C# | |
|---|---|
The font used for displaying the combo box items is set through the Font property.
| C# | |
|---|---|
New items can be added to the combo box using the field's Items property that is a PDFListItemCollection object. A list item is an object that consists of a name and a value, the name being the string displayed in the combo box.
The item that is selected initially can be set using the SelectedIndex property. The item can also be selected using the SelectedItem property.
| C# | |
|---|---|
Push-button fields
A push-button is a purely interactive control that responds immediately to user input without retaining a permanent value.
The PDFPushButtonField class in PDF4NET library is used to create a push-button field.
| C# | |
|---|---|
After the field has been created, it is recommended to the added to the desired page before setting its properties.
| C# | |
|---|---|
After the field has been added to the page, its position on the page is set using the widget's DisplayRectangle property.
| C# | |
|---|---|
The default field properties make it blend with the page background so an end user might not see it. You can highlight the field by adding a border to it or by coloring its background.
| C# | |
|---|---|
The text displayed on the push-button is set using the Caption property.
| C# | |
|---|---|
Push-buttons are used to execute an action when the user interacts with them.
| C# | |
|---|---|
Check Box fields
Check box fields are buttons that toggle between two states, on and off.
The PDFCheckBoxField class in PDF4NET library is used to create a check box field.
| C# | |
|---|---|
After the field has been created, it is recommended to the added to the desired page before setting its properties.
| C# | |
|---|---|
After the field has been added to the page, its position on the page is set using the widget's DisplayRectangle property.
| C# | |
|---|---|
The default field properties make it blend with the page background so an end user might not see it. You can highlight the field by adding a border to it or by coloring its background.
| C# | |
|---|---|
The style of the checked box can be set using the CheckStyle property.
| C# | |
|---|---|
Multiple styles are available: Check, Circle, Cross, Diamond, Square, Star.
The check box fields have an associated ExportValue, this value is used to show that the field is checked when the form data is exported to FDF, XFDF or XML.
| C# | |
|---|---|
The checked/unchecked state of the field is set using the Checked property.
| C# | |
|---|---|
Check box fields can have also a DefaultValue, this is used to set the Checked property when a Reset action is executed on the PDF form. The current state is cleared and replaced with the DefaultValue. DefaultValue should be set to null if the check box should be unchecked on reset and set to ExportValue if the check box should be checked on reset.
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 in PDF4NET library is used to create a radio button field.
| C# | |
|---|---|
Each item in a radio button is represented by a PDFRadioButtonWidget object. The code below creates 2 radio options and adds them to the radio field and then adds the field to the page.
| C# | |
|---|---|
After the field has been added to the page, the position on the page of each item is set using its DisplayRectangle property.
| C# | |
|---|---|
The default radio item properties make it blend with the page background so an end user might not see it. You can highlight a radio item by adding a border to it or by coloring its background.
| C# | |
|---|---|
The style of the checked radio item can be set using the CheckStyle property.
| C# | |
|---|---|
Multiple styles are available: Check, Circle, Cross, Diamond, Square, Star.
Each radio button item has an associated ExportValue, this value is used to show which item is checked when the form data is exported to FDF, XFDF or XML.
The checked/unchecked state of a radio button item is set using the Checked property. When a radio button item is checked, the library unchecks automatically the previously checked radio button item.
| C# | |
|---|---|
Radio button fields can have also a DefaultValue, this is used to set the checked radio item when a Reset action is executed on the PDF form. The current state is cleared and replaced with the DefaultValue. DefaultValue should be set to null if the no radio button item should checked on reset and set to ExportValue of the radio button item that should be checked checked on reset.
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 in PDF4NET library is used to create a signature field.
| C# | |
|---|---|
After the field has been created, it is recommended to the added to the desired page before setting its properties.
| C# | |
|---|---|
After the field has been added to the page, its position on the page is set using the widget's DisplayRectangle property.
| C# | |
|---|---|
The default field properties make it blend with the page background so an end user might not see it. You can highlight the field by adding a border to it or by coloring its background.
| C# | |
|---|---|
The font used for displaying the signature related information is set through the Font property.
| C# | |
|---|---|
Sample code
The complete FormGenerator sample is available online on GitHub.