FormFieldCollection
The FormFieldCollection class holds a collection of FormField instances, assigned to the AcroForm of the document. The collection exposes useful properties and methods that allow you to access, add, or remove the form fields in a document.
Properties
The FormFieldCollection class exposes an indexer and a Count property. Use the indexer to get a FormField instance by its name, or use Count to get the number of all form fields in the document.
Methods
The following methods allow you to construct a form field and add it to the collection. Each method accepts a string parameter representing the unique name of the form field. The generated field is returned from the method so you can customize it.
-
AddPushButton(): Creates a PushButton field and adds it to the collection. Returns the created field. -
AddCheckBox(): Creates a CheckBox field and adds it to the collection. Returns the created field. -
AddRadioButton(): Creates a RadioButton field and adds it to the collection. Returns the created field. -
AddCombTextBox(): Creates a CombTextBox field and adds it to the collection. Returns the created field. -
AddTextBox(): Creates a TextBox field and adds it to the collection. Returns the created field. -
AddComboBox(): Creates a ComboBox field and adds it to the collection. Returns the created field. -
AddListBox(): Creates a ListBox field and adds it to the collection. Returns the created field. -
AddSignature(): Creates a Signature field and adds it to the collection. Returns the created field.
Example 1 shows how to use the previous methods to generate a form field and add it to the collection.
Example 1: Create a Form Field
CombTextBoxField comb = document.AcroForm.FormFields.AddCombTextBox("comb");
comb.MaxLengthOfInputCharacters = 10;
comb.Value = "0123456789";
You can also use the following methods to modify the collection of form fields in the document AcroForm:
-
Add(): Accepts a parameter of type FormField and inserts it to the collection. -
Remove(): Accepts a parameter of type FormField and removes it from the collection. -
Contains(): Accepts a string representing the form field name. Returnstruewhen a field with such a name is present in the collection, otherwisefalse.
Example 2: Use the Methods of FormFieldCollection
string fieldName = "my-field-name";
if (document.AcroForm.FormFields.Contains(fieldName))
{
document.AcroForm.FormFields.Remove(document.AcroForm.FormFields[fieldName]);
}