Import and Export PDF Form Data
- 2 minutes to read
Import data from an external source into interactive PDF forms (AcroForms), or export form data for storage, exchange, or processing.
Supported Form Data Formats
The PDF Document API supports the following form data formats:
| Format | Description |
|---|---|
| FDF | Native PDF Forms Data Format. Use it to exchange form data between PDF applications. |
| XFDF | XML-based representation of FDF. Use it in web applications and document workflows. |
| XML | Generic XML representation of form field values. |
| TXT | Plain-text representation of form field data. Useful for simple processing and debugging. |
Import Form Data
Use the PdfDocument.importFormData() method to populate form fields from an InputStream or ReadableByteChannel. Pass the input source and the imported form data format.
The following code snippet imports form data from an XML file:
import com.devexpress.docs.pdf.*;
import java.io.*;
import java.nio.file.*;
import java.nio.channels.*;
try (PdfDocument pdfDocument = new PdfDocument(FileChannel.open(Path.of("form.pdf")));
InputStream inputStream = Files.newInputStream(Path.of("form-data.xml"))) {
// Import form field values.
pdfDocument.importFormData(
inputStream,
ExportDataFormat.XML);
// Save the updated document.
try (OutputStream outputStream = Files.newOutputStream(Path.of("result.pdf"))) {
pdfDocument.save(outputStream);
}
}
Export Form Data
Use the PdfDocument.exportFormData() method to write form field values to an OutputStream or WritableByteChannel. Pass the output destination and the exported form data format.
The following code snippet exports form data to an XFDF file:
import com.devexpress.docs.pdf.*;
import java.io.*;
import java.nio.file.*;
import java.nio.channels.*;
try (PdfDocument pdfDocument = new PdfDocument(FileChannel.open(Path.of("form.pdf")));
OutputStream outputStream = Files.newOutputStream(Path.of("form-data.xfdf"))) {
// Export form field values.
pdfDocument.exportFormData(outputStream, ExportDataFormat.XFDF);
}
See Also