Skip to content

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#
// Create a text field with the name "textBox1"
PDFTextBoxField textBox = new PDFTextBoxField("textBox1");

After the field has been created, it is recommended to the added to the desired page before setting its properties.

C#
page.Fields.Add(textBox);

After the field has been added to the page, its position on the page is set using the widget's DisplayRectangle property.

C#
1
2
3
// The field is placed at (30,30) points from the top left corner of the page
// its width is 200 points and height is 20 points
textBox.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(30, 30, 200, 20);

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#
1
2
3
4
// Set the field's background to light gray and surround it with a black border
textBox.Widgets[0].BackgroundColor = new PDFRgbColor(224, 224, 224);
textBox.Widgets[0].BorderColor = PDFRgbColor.Black;
textBox.Widgets[0].BorderWidth = 1;

The font used for editing and displaying the field content is set through the Font property.

C#
textBox.Widgets[0].Font = new PDFStandardFont(PDFStandardFontFace.Helvetica, 12);

The Value property can be used to set the initial field value.

C#
textBox.Value = "Sample text";

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#
// Create a list box field with the name "listBox1"
PDFListBoxField listBox = new PDFListBoxField("listBox1");

After the field has been created, it is recommended to the added to the desired page before setting its properties.

C#
page.Fields.Add(listBox);

After the field has been added to the page, its position on the page is set using the widget's DisplayRectangle property.

C#
1
2
3
// The field is placed at (30,30) points from the top left corner of the page
// its width is 200 points and height is 100 points
listBox.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(30, 30, 200, 100);

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#
1
2
3
4
// Set the field's background to light gray and surround it with a black border
listBox.Widgets[0].BackgroundColor = new PDFRgbColor(224, 224, 224);
listBox.Widgets[0].BorderColor = PDFRgbColor.Black;
listBox.Widgets[0].BorderWidth = 1;

The font used for displaying the list box items is set through the Font property.

C#
listBox.Widgets[0].Font = new PDFStandardFont(PDFStandardFontFace.Helvetica, 12);

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.

C#
listBox.Items.Add(new PDFListItem("Mercedes", "Mercedes"));
listBox.Items.Add(new PDFListItem("BMW", "BMW"));
listBox.Items.Add(new PDFListItem("Audi", "Audi"));
listBox.Items.Add(new PDFListItem("Volkswagen", "Volkswagen"));
listBox.Items.Add(new PDFListItem("Porsche", "Porsche"));
listBox.Items.Add(new PDFListItem("Honda", "Honda"));
listBox.Items.Add(new PDFListItem("Toyota", "Toyota"));
listBox.Items.Add(new PDFListItem("Lexus", "Lexus"));
listBox.Items.Add(new PDFListItem("Infiniti", "Infiniti"));
listBox.Items.Add(new PDFListItem("Acura", "Acura"));

The item that is selected initially can be set using the SelectedIndex property. The item can also be selected using the SelectedItem property.

C#
listBox.SelectedIndex = 4;

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#
// Create a combo box field with the name "comboBox1"
PDFDropDownListField comboBox = new PDFDropDownListField("comboBox1");

After the field has been created, it is recommended to the added to the desired page before setting its properties.

C#
page.Fields.Add(comboBox);

After the field has been added to the page, its position on the page is set using the widget's DisplayRectangle property.

C#
1
2
3
// The field is placed at (30,30) points from the top left corner of the page
// its width is 200 points and height is 20 points
comboBox.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(30, 30, 200, 20);

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#
1
2
3
4
// Set the field's background to light gray and surround it with a black border
listBox.Widgets[0].BackgroundColor = new PDFRgbColor(224, 224, 224);
listBox.Widgets[0].BorderColor = PDFRgbColor.Black;
listBox.Widgets[0].BorderWidth = 1;

The font used for displaying the combo box items is set through the Font property.

C#
comboBox.Widgets[0].Font = new PDFStandardFont(PDFStandardFontFace.Helvetica, 12);

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.

C#
comboBox.Items.Add(new PDFListItem("Mercedes", "Mercedes"));
comboBox.Items.Add(new PDFListItem("BMW", "BMW"));
comboBox.Items.Add(new PDFListItem("Audi", "Audi"));
comboBox.Items.Add(new PDFListItem("Volkswagen", "Volkswagen"));
comboBox.Items.Add(new PDFListItem("Porsche", "Porsche"));
comboBox.Items.Add(new PDFListItem("Honda", "Honda"));
comboBox.Items.Add(new PDFListItem("Toyota", "Toyota"));
comboBox.Items.Add(new PDFListItem("Lexus", "Lexus"));
comboBox.Items.Add(new PDFListItem("Infiniti", "Infiniti"));
comboBox.Items.Add(new PDFListItem("Acura", "Acura"));

The item that is selected initially can be set using the SelectedIndex property. The item can also be selected using the SelectedItem property.

C#
comboBox.SelectedIndex = 4;

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#
// Create a push-button field with the name "pushButton1"
PDFPushButtonField pushButton = new PDFPushButtonField("pushButton1");

After the field has been created, it is recommended to the added to the desired page before setting its properties.

C#
page.Fields.Add(pushButton);

After the field has been added to the page, its position on the page is set using the widget's DisplayRectangle property.

C#
1
2
3
// The field is placed at (30,30) points from the top left corner of the page
// its width is 100 points and height is 20 points
pushButton.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(30, 30, 100, 20);

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#
1
2
3
4
// Set the field's background to light gray and surround it with a black border
pushButton.Widgets[0].BackgroundColor = new PDFRgbColor(224, 224, 224);
pushButton.Widgets[0].BorderColor = PDFRgbColor.Black;
pushButton.Widgets[0].BorderWidth = 1;

The text displayed on the push-button is set using the Caption property.

C#
(pushButton.Widgets[0] as PDFPushButtonWidget).Caption = "Reset form";

Push-buttons are used to execute an action when the user interacts with them.

C#
// This push-button resets the PDF form when it is clicked.
pushButton.Widgets[0].MouseUp = new PDFResetFormAction();

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#
// Create a check box field with the name "checkBox1"
PDFCheckBoxField checkBox = new PDFCheckBoxField("checkBox1");

After the field has been created, it is recommended to the added to the desired page before setting its properties.

C#
page.Fields.Add(checkBox);

After the field has been added to the page, its position on the page is set using the widget's DisplayRectangle property.

C#
1
2
3
// The field is placed at (30,30) points from the top left corner of the page
// its width is 20 points and height is 20 points
checkBox.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(30, 30, 20, 20);

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#
1
2
3
4
// Set the field's background to light gray and surround it with a black border
checkBox.Widgets[0].BackgroundColor = new PDFRgbColor(224, 224, 224);
checkBox.Widgets[0].BorderColor = PDFRgbColor.Black;
checkBox.Widgets[0].BorderWidth = 1;

The style of the checked box can be set using the CheckStyle property.

C#
(checkBox.Widgets[0] as PDFCheckWidget).CheckStyle = PDFCheckStyle.Check;

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#
(checkBox.Widgets[0] as PDFCheckWidget).ExportValue = "YES";

The checked/unchecked state of the field is set using the Checked property.

C#
checkBox.Checked = true;

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#
// Create a radio button field with the name "radioButton1"
PDFRadioButtonField radioButton = new PDFRadioButtonField("radioButton1");

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#
1
2
3
4
5
PDFRadioButtonWidget radioItem1 = new PDFRadioButtonWidget();
radioButton.Widgets.Add(radioItem1);
PDFRadioButtonWidget radioItem2 = new PDFRadioButtonWidget();
radioButton.Widgets.Add(radioItem2);
page.Fields.Add(radioButton);

After the field has been added to the page, the position on the page of each item is set using its DisplayRectangle property.

C#
1
2
3
// The radio item is placed at (30,30) points from the top left corner of the page
// its width is 20 points and height is 20 points
radioItem1.DisplayRectangle = new PDFDisplayRectangle(30, 30, 20, 20);

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#
1
2
3
4
// Set the field's background to light gray and surround it with a black border
radioItem1.BackgroundColor = new PDFRgbColor(224, 224, 224);
radioItem1.BorderColor = PDFRgbColor.Black;
radioItem1.BorderWidth = 1;

The style of the checked radio item can be set using the CheckStyle property.

C#
radioItem1.CheckStyle = PDFCheckStyle.Circle;

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.

C#
radioItem1.ExportValue = "M";
radioItem2.ExportValue = "F";

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#
radioItem1.Checked = true;

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#
// Create a signature field with the name "signature1"
PDFSignatureField signatureField = new PDFSignatureField("signature1");

After the field has been created, it is recommended to the added to the desired page before setting its properties.

C#
page.Fields.Add(signatureField);

After the field has been added to the page, its position on the page is set using the widget's DisplayRectangle property.

C#
1
2
3
// The field is placed at (30,30) points from the top left corner of the page
// its width is 200 points and height is 50 points
signatureField.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(30, 30, 200, 50);

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#
1
2
3
4
// Set the field's background to light gray and surround it with a black border
signatureField.Widgets[0].BackgroundColor = new PDFRgbColor(224, 224, 224);
signatureField.Widgets[0].BorderColor = PDFRgbColor.Black;
signatureField.Widgets[0].BorderWidth = 1;

The font used for displaying the signature related information is set through the Font property.

C#
signatureField.Widgets[0].Font = new PDFStandardFont(PDFStandardFontFace.Helvetica, 12);

Sample code

The complete FormGenerator sample is available online on GitHub.