Form Filling | |
SelectPdf can be used to open existing PDF documents that contain AcroForms form fields, get information about the form fields (name, type, value, status) fill the form fields and change some of their properties (like read-only or flatten).
This can be done using PdfFormManager object. Using it, the list of fields can be accessed through the Fields property. The following types of fields can be handled by SelectPdf:
Textbox using PdfFormFieldTextBox. For a textbox field, the field value can be set using the property Text.
Checkbox using PdfFormFieldCheckBox. For a checkbox field, the checked status can be set or unset using the property Checked.
Listbox using PdfFormFieldListBox. For a list box field, the selected items can be set using either property SelectedIndexes or SelectedValues. The number of items in the list can be obtained using ItemsCount property. The list of items from the listbox can be accessed using the Items property. MultiSelect can be used to determine if more than 1 item can be selected from the list box.
Combo box using PdfFormFieldComboBox. For a combo box field, the selected item can be set using either property SelectedIndex or SelectedValue. The number of items in the list can be obtained using ItemsCount property. The list of items from the combo box can be accessed using the Items property.
Radio buttons list using PdfFormFieldRadioButtonList. For a radio buttons list field, the selected item can be set using either property SelectedIndex or SelectedValue. The number of items in the list can be obtained using ItemsCount property. The list of items can be accessed using the Items property.
For all form fields, the following properties can be accessed:
Name - Gets or sets the name of the field.
FullName - Gets the fully-qualified field name (parent-qualified for nested fields). Use this when the field name in the PDF is qualified (for example address.city).
DisplayPage - Gets the zero-based index of the page on which the field is displayed.
DisplayRectangle - Gets the bounds of the field on its page as a RectangleF.
Widgets - Gets the collection of widget annotations that represent the field on the page. A single form field can have more than one widget (for example a check box repeated in the header and footer).
ReadOnly - Gets or sets a value indicating whether the field is read-only or not.
Flatten - Gets or sets a value indicating whether to flatten this form field or not.
When a check box field has multiple widgets that represent the same logical value on different pages, each widget can be read or written individually through GetWidgetCheckedValue(Int32) and SetWidgetCheckedValue(Boolean, Int32), where the integer is the widget index in the Widgets collection. Setting Checked applies to every widget at once.
The whole PDF form can be marked as read-only or flattened using the properties ReadOnly or Flatten.
Starting with version 21.1 of SelectPdf, a new PDF forms engine was introduced with SelectPdf.Extras package. To use it, SelectPdfPdfFormManager needs to be replaced with SelectPdf.FormsPdfFormManager.
// load the pdf form manager PdfFormManager form = new PdfFormManager(); form.Load(file); // fill some fields PdfFormFieldTextBox txtField = form.Fields["textfieldname"] as PdfFormFieldTextBox; txtField.Text = "Some text"; PdfFormFieldCheckBox chkField = form.Fields["checkboxfieldname"] as PdfFormFieldCheckBox; chkField.Checked = true; // save pdf document form.Save("Sample.pdf"); // close pdf document form.Close();