Import and Export Annotations from XFDF Files in Java
XFDF is an XML-like standard from Adobe XFDF (ISO 19444-1:2016) for encoding annotations and forms. It’s compatible with Adobe Acrobat and several other third-party frameworks.
ℹ️ Note: XFDF has various limitations. In most cases, using PSPDFKit Instant JSON will result in a smaller file and better synchronization.
The PSPDFKit Java Library supports both reading and writing XFDF files. The PdfDocument
class contains importXfdf
and exportXfdf
methods you can use to perform these operations.
Importing XFDF
Below is an example showing how to import XFDF from a file. However, the DataProvider
can be inherited from to implement custom readers for encryption:
document.importXfdf(new FileDataProvider("Assets/importXfdf.xfdf"));
Exporting XFDF
You can export annotations and form field values from a document to an XFDF file like so:
File file = new File("xfdfOutput.xfdf"); document.exportXfdf(new FileDataProvider(file), new HashSet<>(), new HashSet<>());
The second two parameters take in HashSet
s. The first states which annotations to export, and the second states which form field to export. By passing an empty HashSet
, PSPDFKit assumes all annotations or form fields should be exported.
Much like the import XFDF, with the export XFDF, it’s also possible to inherit from WritableDataProvider
to implement custom writers for encryption.
Exporting Annotations to XFDF via Adobe Acrobat
Adobe Acrobat can export annotations into XFDF. The export menu is part of the Comments tool and is accessed by opening the tool in the sidebar.
You can access the export function by clicking on the three dots and then choosing Export All To Data File.
-
At the bottom of the page, choose Acrobat XFDF Files.
-
Select the directory you wish to save the XFDF file to and name the file.
-
Click save.
A successful export will result in a file with an .xfdf
extension.
Importing Annotations to XFDF via Adobe Acrobat
The export function is part of the Comments tool and is accessed by clicking on its icon.
Click on the three dots to open the import menu, and then click on Import Data File.
Highlight the .xfdf
file you wish to import and click Select.
The import function completes with the annotations being placed on the document.
Adobe Acrobat Error Conditions
Error Description | Screenshot |
---|---|
Damaged/missing document body | |
Damaged/missing description tag | |
Missing document flag |