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:
create an instance of the class that represents the specific form field (for example, PdfGraphicsAcroFormTextBoxField);
or
call the corresponding static Create method for the PdfGraphicsAcroFormField class (for example, PdfGraphicsAcroFormField.CreateTextBox).
The PdfGraphicsAcroFormField.CreateTextBox method returns a PdfGraphicsAcroFormTextBoxField object that represents a created text box field. This method needs the name of the field and a RectangleF instance that defines the position of the form field on a document page.
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:
- Render a new page with created interactive form fields by calling one of the PdfDocumentProcessor.RenderNewPage overloaded methods;
- Add the interactive form fields to an existing document page by calling one of the overloaded methods: PdfGraphics.AddToPageBackground, and PdfGraphics.AddToPageForeground.
Example
This example shows how to create a text box field and add it to a document.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-add-a-text-box-field-to-a-document-t494157
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);
}
}
}