Skip to main content
A newer version of this page is available. .

Adding Interactive Form Fields

  • 3 minutes to read

You can create interactive form fields (for example, a text box, combo box, check box, list box) and add these fields to a document using graphics represented by an instance of the PdfGraphics class.

To create a PdfGraphics object, call the PdfDocumentProcessor.CreateGraphics method.

Note

To access the PdfGraphics class, you need to reference the DevExpress.Pdf.Drawing assembly.

The PdfGraphics class methods use a world coordinate system. See Coordinate Systems to learn more about this coordinate system.

An instance of the PdfGraphicsAcroFormField object represents the interactive form field.

To create a form field:

Then, specify the interactive form field properties. For example, you can specify the text box text and type using PdfGraphicsAcroFormTextBoxField.Text, and PdfGraphicsAcroFormTextBoxField.Type properties, respectively.

To specify the text box name, tooltip and appearance settings, use the PdfGraphicsAcroFormField.Name, PdfGraphicsAcroFormField.ToolTip, and PdfGraphicsAcroFormField.Appearance properties.

Note

Form field names must be unique in an interactive form.

To add an interactive form field to graphics, call the PdfGraphics.AddFormField method, and pass the form field to this method as an argument.

The graphics that contain interactive form fields can be added only to a single page.

To clear form fields that were previously added to graphics, call the PdfGraphics.ClearFormFields method.

Finally, add interactive form fields to a document using one of the following ways:

Example

This example shows how to create a text box field and add it to a document.

using DevExpress.Pdf;
using System.Drawing;

namespace AddTextBoxField {
    class Program {
        static void Main(string[] args) {
            using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {

                // Create an empty document. 
                processor.CreateEmptyDocument("..\\..\\Result.pdf");

                // Create and draw a text box field.
                using (PdfGraphics graphics = processor.CreateGraphics()) {
                    DrawTextBoxField(graphics);

                    // Render a page with graphics.
                    processor.RenderNewPage(PdfPaperSize.Letter, graphics);
                }
            }
        }

        static void DrawTextBoxField(PdfGraphics graphics) {

            // Create a text box field and specify its location on the page using a RectangleF object.
            PdfGraphicsAcroFormTextBoxField textBox = new PdfGraphicsAcroFormTextBoxField("text box", new RectangleF(0, 10, 200, 30));

            // Specify text box properties.
            textBox.Text = "Text Box";           
            textBox.TextAlignment = PdfAcroFormStringAlignment.Near;
            textBox.Appearance.FontSize = 12;
            textBox.Appearance.BackgroundColor = Color.AliceBlue;          

            // Add the field to graphics.
            graphics.AddFormField(textBox);
        }
    }
}