# 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](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/acroform).

> note You can find complete examples for [Creating Interactive Forms](https://github.com/telerik/document-processing-sdk/tree/master/PdfProcessing/CreateInteractiveForms) and [Modifying Forms](https://github.com/telerik/document-processing-sdk/tree/master/PdfProcessing/ModifyForms) 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.

**Obtain fields from a document**

```C#
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](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/form-fields/checkboxfield)
* [ComboBoxField](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/form-fields/comboboxfield)
* [CombTextBoxField](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/form-fields/combtextboxfield)
* [ListBoxField](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/form-fields/listboxfield)
* [PushButtonField](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/form-fields/pushbuttonfield)
* [RadioButtonField](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/form-fields/radiobuttonfield)
* [SignatureField](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/form-fields/signaturefield)
* [TextBoxField](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/form-fields/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.

**Rename form fields**

```C#
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.

**Merge files with form fields**

```C#
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";
    }
}
```

## See Also

* [PDF Annotation Widgets](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/annotations/widgets)
* [AcroForm](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/acroform)
* [Create Interactive Forms SDK example](https://github.com/telerik/document-processing-sdk/tree/master/PdfProcessing/CreateInteractiveForms)
* [Modifying Forms SDK example](https://github.com/telerik/document-processing-sdk/tree/master/PdfProcessing/ModifyForms)
* [Creating a PDF Table with Form Fields Inside the Cells](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/insert-form-xobject-elements-pdf-table-cell)
* [Resetting Form Fields](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/form-fields/resetting-form-fields)
