Work with PDF Forms (AcroForms)
- 3 minutes to read
PDF documents can contain interactive forms (AcroForms). An AcroForm consists of one or more form fields that allow users to enter or select data. Common field types include text boxes, check boxes, radio button groups, list boxes, combo boxes, and signature fields.
The PDF Document API allows you to:
- Create interactive PDF forms.
- Read and modify form fields.
- Import and export form data.
- Group form fields.
- Flatten the form field hierarchy.
- Configure field formatting and validation.
Supported Form Field Types
The PDF Document API supports the following AcroForm field types:
AcroForm Object Model
An AcroForm consists of the following elements:
- Form field
- A non-visual object that stores a field’s name, value, and behavior.
- Widget annotation
- Defines how the field appears on a page and where users interact with it.
A form field can have one or more widget annotations. This structure allows the same field value to appear in multiple locations in a document.
PdfDocument
└── Fields
└── FormField
├── Name
├── Value
└── Widget Annotation(s)
├── Location
├── Size
└── Appearance
Access Form Fields
Use the PdfDocument.getFields() method to access the collection of form fields in a PDF document.
The following code snippet iterates through all form fields and prints their names and types:
// Iterate through all form fields in the document, including grouped fields.
for (FormField field : pdfDocument.getFields().toFlatIterable()) {
System.out.println(field.getName());
System.out.println(field.getClass().getSimpleName());
}
Refer to the following help topic for additional information: Access and Find PDF Form Fields.
Configure Document-Level Form Settings
The PdfDocument.getFields() method returns an IDocumentFormFieldCollection object. Call the following IDocumentFormFieldCollection methods to configure the document’s interactive form:
| Method | Description |
|---|---|
| setNeedAppearances | Sets whether the interactive form asks PDF viewers to generate appearances for form fields. |
| setSignatureFlags | Sets signature flags for the document’s interactive form. |
| getHasXfa | Returns whether the document contains XFA form data. |
| removeXfa | Removes XFA form data from the document. |
Note
The API saves values passed to setNeedAppearances(value) and setSignatureFlags(value) methods only if the document contains form fields.
The following code snippet removes XFA form data from a document, asks PDF viewers to generate form field appearances, and checks whether the form contains signatures:
import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;
public class Main {
public static void main(String[] args) throws Exception {
Files.createDirectories(Path.of("output"));
try (InputStream inputStream = Files.newInputStream(Path.of("Document.pdf"));
PdfDocument pdfDocument = new PdfDocument(inputStream)) {
IDocumentFormFieldCollection fields = pdfDocument.getFields();
// Remove XFA form data to keep AcroForm fields only.
if (fields.getHasXfa())
fields.removeXfa();
// Ask PDF viewers to generate appearances for form fields.
fields.setNeedAppearances(true);
// Check whether the form contains signatures.
FormSignatureFlags flags = fields.getSignatureFlags();
System.out.println("Signature flags: " + flags);
System.out.println("Contains signatures: " + flags.hasFlag(FormSignatureFlags.SIGNATURES_EXIST));
System.out.println("Save incrementally: " + flags.hasFlag(FormSignatureFlags.APPEND_ONLY));
try (OutputStream outputStream = Files.newOutputStream(Path.of("output", "Document.pdf"))) {
pdfDocument.save(outputStream);
}
}
}
}