Creating Interactive Form
- 4 minutes to read
This topic describes how to create interactive form fields and add them to an existing PDF document.
Note
To learn how to create interactive form fields using the PDF Document Creation API, see the Adding Interactive Form Fields topic.
Before adding interactive form fields to a PDF document, create an instance of the PdfDocumentProcessor class and load a PDF document into the PDF Document Processor, using one of the PdfDocumentProcessor.LoadDocument overloaded methods.
An instance of the PdfAcroFormField object represents the interactive form field.
To create an interactive form field, do one of the following:
- create an instance of the class that represents the specific form field (for example, PdfAcroFormTextBoxField);
call the corresponding static Create method for the PdfAcroFormField class (for example, PdfAcroFormField.CreateTextBox).
The PdfAcroFormField.CreateTextBox method returns a PdfAcroFormTextBoxField object that represents a created text box field. This method needs the name of the field, a PdfRectangle instance that defines the position of the form field on a document page, and a page number.
Then, specify the interactive form field properties. For example, specify the text box text, and type using PdfAcroFormTextBoxField.Text, and PdfAcroFormTextBoxField.Type properties, respectively.
To specify the text box name, tooltip and appearance settings, use the PdfAcroFormField.Name, PdfAcroFormField.ToolTip, and PdfAcroFormVisualField.Appearance properties.
Note
Form field names must be unique in an interactive form.
To find a collision in the interactive form field names, call one of the PdfDocumentProcessor.CheckFormFieldNameCollisions overloaded methods and pass interactive form fields that should be checked as a parameter. These methods return a collection of PdfAcroFormFieldNameCollision objects that contains information about a collision found in interactive form field names.
To obtain the interactive form field in which a collision was found with a field name, use the PdfAcroFormFieldNameCollision.Field property.
You can also get the forbidden field names using the PdfAcroFormFieldNameCollision.ForbiddenNames property.
To add interactive form fields to a document, pass a collection of form fields (for example, PdfAcroFormTextBoxField objects) as a parameter to one of the PdfDocumentProcessor.AddFormFields overloaded methods.
To save a PDF document, call one of the PdfDocumentProcessor.SaveDocument overloaded methods.
Example
This example shows how to create interactive form fields (e.g., text box and radio button group fields) and add them to a document.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-add-interactive-form-fields-to-a-document-t494642
Imports DevExpress.Pdf
Namespace AddFormFieldsToExistingDocument
Friend Class Program
Shared Sub Main(ByVal args() As String)
Using processor As New PdfDocumentProcessor()
' Load a document.
processor.LoadDocument("..\..\Document.pdf")
' Create a text box field specifying the field name, page number, and field location on the page.
Dim textBox As New PdfAcroFormTextBoxField("text box", 1, New PdfRectangle(230, 690, 280, 710))
' Specify text box text, and appearance.
textBox.Text = "Text Box"
textBox.Appearance.BackgroundColor = New PdfRGBColor(0.8, 0.5, 0.3)
textBox.Appearance.FontSize = 12
' Create a radio group field specifying its name and the page number.
Dim radioGroup As New PdfAcroFormRadioGroupField("Gender Group", 1)
' Add the first radio button to the group and specify its location using a PdfRectangle object.
radioGroup.AddButton("button1", New PdfRectangle(230, 635, 250, 655))
' Add the second radio button to the group.
radioGroup.AddButton("button2", New PdfRectangle(310, 635, 330, 655))
' Specify radio group selected index, and appearance.
radioGroup.SelectedIndex = 0
radioGroup.Appearance.BorderAppearance = New PdfAcroFormBorderAppearance() With {.Color = New PdfRGBColor(0.8, 0.5, 0.3), .Width = 3}
' Add form fields to the page.
processor.AddFormFields(textBox, radioGroup)
' Save the result document.
processor.SaveDocument("..\..\Result.pdf")
End Using
End Sub
End Class
End Namespace