Create Interactive Forms

  • 5 minutes to read

This topic describes how to create interactive form fields and add them to an existing PDF document.


If you work in the global coordinate system, you can create interactive form fields as a graphic content. Refer to the Add Interactive Form Fields topic for more information.

Create an Interactive Form Field

The PdfAcroFormField object represents an interactive form field. Use the PdfDocument.AcroForm property to access the document's collection of form fields.

The table below lists available form fields and API used to create each type.

Form Field Class Method
Check Box PdfAcroFormCheckBoxField PdfAcroFormField.CreateCheckBox
Combo box PdfAcroFormComboBoxField PdfGraphicsAcroFormField.CreateComboBox
Group PdfAcroFormGroupField PdfAcroFormField.CreateGroup
List Box PdfAcroFormListBoxField PdfGraphicsAcroFormField.CreateListBox
Radio Group PdfGraphicsAcroFormRadioGroupField PdfGraphicsAcroFormField.CreateRadioGroup
Signature PdfAcroFormSignatureField PdfGraphicsAcroFormField.CreateSignature
Text Box PdfAcroFormTextBoxField PdfGraphicsAcroFormField.CreateTextBox

To specify the form field name, tooltip and appearance settings, use the PdfAcroFormField.Name, PdfAcroFormField.ToolTip, and PdfAcroFormVisualField.Appearance properties.

Pass a collection of form fields (for example, PdfAcroFormTextBoxField objects) as a parameter to the PdfDocumentProcessor.AddFormFields method to add a form field to the document.

This example shows how to create text box and radio button group fields and add them to a document.

using DevExpress.Pdf;

namespace AddFormFieldsToExistingDocument {
    class Program {
        static void Main(string[] args) {
            using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
                // Load a document.

                // Create a text box field specifying the field name, page number, and field location on the page.
                PdfAcroFormTextBoxField textBox = 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.
                PdfAcroFormRadioGroupField radioGroup = 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()
                { 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.

Field Name Collisions

When you add a form field to a document, make sure that it's name is unique. Otherwise, a conflict may occur.

Pass created form fields to the PdfDocumentProcessor.CheckFormFieldNameCollisions method to check whether the form field name already exists. Use the PdfAcroFormFieldNameCollision.Field property to obtain a conflicting form field.

The PdfDocumentProcessor generates a collection of conflicting names after a collision is found. Use the PdfAcroFormFieldNameCollision.ForbiddenNames property to access the collection of forbidden names.

The code sample below checks whether the created fields' names already exist in the loaded document and renames the conflicting field.

List<PdfAcroFormField> fields = new List<PdfAcroFormField>();
//Check whether new form fields' names already exist in the document
IList<PdfAcroFormFieldNameCollision> collisions = processor.CheckFormFieldNameCollisions(fields);
if (collisions.Count == 0)
    Console.WriteLine("No name conflicts are detected");
    foreach (var collision in collisions)
        //Rename conflicting field
        Console.WriteLine("The specified form field name ({0}) already exist in the document. Renaming...", collision.Field.Name);
        while (collision.ForbiddenNames.Contains(collision.Field.Name))
            collision.Field.Name = Guid.NewGuid().ToString();
//Add fields to the document
//And save the result

Obtain Form Field's Location

Use the PdfInteractiveFormField.Widget property to access the visual representation of a form field (the PdfWidgetAnnotation object).

The PdfAnnotation.Rect property allows you to obtain the widget's margins, the PdfAnnotation.Page property) provides access to the page where the widget is located.

Refer to the How to: Replace a Form Field with an Image topic for information on how to use the retrieved information to replace an interactive form.

See Also