Flatten PDF Annotations and Form Fields
- 2 minutes to read
Flattening converts annotations and form fields into static page content. After you flatten an annotation or form field, its appearance becomes part of the page content and users cannot edit or interact with it in a PDF viewer.
Use the following methods to flatten PDF annotations and form fields:
| Method | Description |
|---|---|
| PdfDocument.flatten() | Flattens all annotations and form fields in the document. |
| PdfDocument.flattenAnnotations(…) | Flattens annotations. |
| PdfDocument.flattenFormFields(…) | Flattens form fields. |
Flatten All Annotations and Form Fields
Call the PdfDocument.flatten() method to flatten all annotations and form fields in a PDF document.
The following code snippet opens a PDF document, flattens all interactive content, and saves the result:
package pdf;
import com.devexpress.docs.pdf.*;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (FileChannel channel = FileChannel.open(Path.of("Document.pdf"));
PdfDocument pdfDocument = new PdfDocument(channel)) {
// Flatten all annotations and form fields.
pdfDocument.flatten();
try (WritableByteChannel writableByteChannel =
FileChannel.open(Path.of("Flattened_Document.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(writableByteChannel);
}
}
}
}
Flatten Annotations
Use the PdfDocument.flattenAnnotations() method to flatten all annotations in a PDF document.
// Flatten all annotations.
pdfDocument.flattenAnnotations();
Flatten Specific Annotations
Use the PdfDocument.flattenAnnotations(BaseAnnotation... annotations) overload to flatten specific annotations.
BaseAnnotation annotation = pdfDocument.getPages()
.getFirst()
.getAnnotations()
.getFirst();
// Flatten the specified annotation.
pdfDocument.flattenAnnotations(annotation);
Flatten Annotations That Match a Condition
Use the PdfDocument.flattenAnnotations(predicate) overload to flatten annotations that match a specified condition.
// Flatten text annotations only.
pdfDocument.flattenAnnotations(annotation ->
annotation.getClass().getSimpleName().equals("TextAnnotation"));
Flatten Form Fields
Use the PdfDocument.flattenFormFields() method to flatten all form fields in a PDF document.
package pdf;
import com.devexpress.docs.pdf.*;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (FileChannel channel = FileChannel.open(Path.of("Invoice_Approved.pdf"));
PdfDocument pdfDocument = new PdfDocument(channel)) {
// Flatten all form fields.
pdfDocument.flattenFormFields();
try (WritableByteChannel writableByteChannel =
FileChannel.open(Path.of("Flattened_Document.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(writableByteChannel);
}
}
}
}
Flatten Specific Form Fields
Use the PdfDocument.flattenFormFields(fieldNames) overload to flatten form fields with specified names.
// Flatten the specified form fields.
pdfDocument.flattenFormFields("FirstName", "LastName");
Flatten Form Fields on a Specific Page
Use the PdfDocument.flattenFormFields(pageIndex) overload to flatten form fields on a specific page.
// Flatten form fields on the first page.
pdfDocument.flattenFormFields(0);