Placing form fields into the document via code is easy. DocumentBuilder has special methods for inserting them, one for each form field type.
Each of the methods accepts a string parameter representing the name of the form field. The name could be an empty string. If however you specify a name for the form field, then a bookmark is automatically created with the same name.
Inserting Form Fields
Use DocumentBuilder.InsertTextInput, InsertCheckBox or InsertComboBox to insert form fields into a document.
Example DocumentBuilderInsertComboBoxFormField
Shows how to insert a combobox form field into a document.
[C#]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
string[] items = { "One", "Two", "Three" };
builder.InsertComboBox("DropDown", items, 0);
[Visual Basic]
Dim doc As Document = New Document()
Dim builder As DocumentBuilder = New DocumentBuilder(doc)
Dim items As String() = { "One", "Two", "Three" }
builder.InsertComboBox("DropDown", items, 0)
[Java]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
String[] items = { "One", "Two", "Three" };
builder.insertComboBox("DropDown", items, 0);
Obtaining Form Fields
A collection of form fields is represented by the FormFieldCollection class that could be retrieved using the Range.FormFields property. That means you can obtain form fields contained in any document node including the document itself.
Example FormFieldsGetFormFieldsCollection
Shows how to get a collection of form fields.
[C#]
Document doc = new Document(MyDir + "FormFields.doc");
FormFieldCollection formFields = doc.Range.FormFields;
[Visual Basic]
Dim doc As Document = New Document(MyDir & "FormFields.doc")
Dim formFields As FormFieldCollection = doc.Range.FormFields
[Java]
Document doc = new Document(getMyDir() + "FormFields.doc");
FormFields formFields = doc.getRange().getFormFields();
Then you can get a particular form field by its index or name.
Example FormFieldsGetByName
Shows how to access form fields.
[C#]
Document doc = new Document(MyDir + "FormFields.doc");
FormFieldCollection documentFormFields = doc.Range.FormFields;
FormField formField1 = documentFormFields[3];
FormField formField2 = documentFormFields["CustomerName"];
[Visual Basic]
Dim doc As Document = New Document(MyDir & "FormFields.doc")
Dim documentFormFields As FormFieldCollection = doc.Range.FormFields
Dim formField1 As FormField = documentFormFields(3)
Dim formField2 As FormField = documentFormFields("CustomerName")
[Java]
Document doc = new Document(getMyDir() + "FormFields.doc");
FormFields documentFormFields = doc.getRange().getFormFields();
FormField formField1 = documentFormFields.get(3);
FormField formField2 = documentFormFields.get("CustomerName");
The FormField properties allow you to work with form field name, type, and result.
Example FormFieldsWorkWithProperties
Shows how to work with form field name, type, and result.
[C#]
Document doc = new Document(MyDir + "FormFields.doc");
FormField formField = doc.Range.FormFields[3];
if (formField.Type.Equals(FieldType.FieldFormTextInput))
formField.Result = "My name is " + formField.Name;
[Visual Basic]
Dim doc As Document = New Document(MyDir & "FormFields.doc")
Dim formField As FormField = doc.Range.FormFields(3)
If formField.Type.Equals(FieldType.FieldFormTextInput) Then
formField.Result = "My name is " & formField.Name
End If
[Java]
Document doc = new Document(getMyDir() + "FormFields.doc");
FormField formField = doc.getRange().getFormFields().get(3);
if (formField.getType() == FieldType.FIELD_FORM_TEXT_INPUT)
formField.setResult("My name is " + formField.getName());