Convert Instant JSON to XFDF in Java

To achieve interoperability between XFDF (XML Forms Data Format) and JSON files that conform to PSPDFKit’s annotation schema, PSPDFKit has APIs for importing JSON data into a PDF and exporting it as XFDF.

Unlike PSPDFKit’s JSON schema, XFDF doesn’t have a concept of differentials for describing changes to a PDF (e.g. deleted annotations, new bookmarks, edited forms). XFDF is instead a representation of all annotation and form data inside a PDF document at a point in time. Since these concepts don’t directly translate to each other, there are some limitations that wouldn’t be represented in the final XFDF.

You can import conforming JSON files and export to XFDF using the following code:

fun export(jsonImportFile: File, document: PdfDocument, xfdfOutputFile: File) {
    // Import the data from the JSON file into the PDF document.
    document.importDocumentJson(FileDataProvider(jsonImportFile))

    // Write all annotation and form fields to the output XFDF file.
    xfdfOutputFile.createNewFile()
    document.exportXfdf(FileDataProvider(xfdfOutputFile), setOf(), setOf())
}
void export(@NotNull final File jsonImportFile,
            @NotNull final PdfDocument document,
            @NotNull final File xfdfOutputFile) throws IOException {
    // Import the data from the JSON file into the PDF document.
    document.importDocumentJson(new FileDataProvider(jsonImportFile));

    // Write all annotation and form fields to the output XFDF file.
    xfdfOutputFile.createNewFile();
    document.exportXfdf(new FileDataProvider(xfdfOutputFile), new HashSet<>(), new HashSet<>());
}