FormField
Form fields are the data containers responsible for preserving separate pieces of the interactive form data. Several types of form fields exist, each responsible for preserving a different type of data.
FormField Class
The FormField class is the base class for all fields. You can find instances of this class by iterating the FormFieldCollection of AcroForm.
You can find complete examples for Creating Interactive Forms and Modifying Forms in the SDK repository.
FormField Properties
The FormField class provides the following properties:
| Property | Description |
|---|---|
FieldType | Gets the FormFieldType of the specific field instance. Use this property to recognize the type of the concrete field and cast the instance to the concrete FormField class inheritor. |
Name | Gets or sets the name of the field. Each field must have a unique name when added to a FormFieldCollection of an AcroForm. Setting the name is available starting with R2 2020. |
UserInterfaceName | Gets the name used by the UI when referencing the field. Typically shown in a tooltip when hovering the field representation on the page. Also shown in error messages related to field error calculations. |
MappingName | Gets the name used when exporting the field data from the document. |
IsReadOnly | Gets or sets a Boolean value indicating whether the field is treated as read-only in a PDF viewer UI. |
IsRequired | Gets or sets a Boolean value indicating whether the field is required for submitting the interactive form data. |
ShouldBeSkipped | Gets or sets a Boolean value indicating whether the field is skipped when submitting the form. |
TextProperties | Gets a VariableTextProperties instance used when creating a Widget for visualizing the concrete field. These properties dynamically construct the Widget appearance when it contains text content. |
FormField Types
You can recognize each field type from the FormField base class by getting the value from its FieldType property. This allows you to convert the field to its inheritor type by casting to one of the FormField class inheritors.
Example 1: Obtain fields from a document
using (Stream stream = File.OpenRead("InteractiveForms.pdf"))
{
RadFixedDocument document = new PdfFormatProvider().Import(stream, TimeSpan.FromSeconds(10));
foreach (FormField field in document.AcroForm.FormFields)
{
switch (field.FieldType)
{
case FormFieldType.TextBox:
ProcessTextBox((TextBoxField)field);
break;
case FormFieldType.ListBox:
ProcessListBox((ListBoxField)field);
break;
case FormFieldType.RadioButton:
ProcessRadioButtons((RadioButtonField)field);
break;
case FormFieldType.CheckBox:
ProcessCheckBoxes((CheckBoxField)field);
break;
}
}
}
The following list shows all the inheritors of the FormField class:
- CheckBoxField
- ComboBoxField
- CombTextBoxField
- ListBoxField
- PushButtonField
- RadioButtonField
- SignatureField
- TextBoxField
Rename Form Fields
Starting with R2 2020, the Rename method allows you to rename form fields. Pass the existing field name and the new name.
Example 2: Rename form fields
public void RenameFields(RadFixedDocument document)
{
document.AcroForm.FormFields.Rename("OldName", "NewName");
}
Merging Documents with Form Fields
When merging documents that contain form fields, you must ensure that each field in the document has a unique name. Use the MergedFieldNameResolving event to achieve this. This event gives you access to all used field names and allows you to change the current field name if it is already in use.
Example 3: Merge files with form fields
public void MergeFields()
{
PdfFormatProvider provider = new PdfFormatProvider();
var document = provider.Import(File.ReadAllBytes(@"D:\FormFieldDoc.pdf"), TimeSpan.FromSeconds(10));
var document1 = provider.Import(File.ReadAllBytes(@"D:\FormFieldDoc1.pdf"), TimeSpan.FromSeconds(10));
document.MergedFieldNameResolving += Document_MergedFieldNameResolving;
document.Merge(document1);
using (FileStream fs = new FileStream(@"MergedResult.pdf", FileMode.OpenOrCreate))
{
provider.Export(document, fs, TimeSpan.FromSeconds(10));
}
}
private void Document_MergedFieldNameResolving(object sender, MergedFormFieldNameResolvingEventArgs e)
{
if (e.UsedNames.Contains(e.Name))
{
e.NewName = e.Name + "1";
}
}