XFDF Support

XFDF is an XML-like standard from Adobe XFDF for encoding annotations and forms (see this XFDF overview). It is compatible with Adobe Acrobat and several other third-party frameworks.

ℹ️ Note: XFDF has various limitations. In most cases, using PSPDFKit Instant will result in a smaller file and better synchronization.

PSPDFKit for Android supports both reading and writing XFDF files. The XfdfFormatter class contains parseXfdf() and writeXfdf() methods you can use to perform these operations. It also contains their asynchronous counterparts, parseXfdfAsync() and writeXfdfAsync() (recommended so you can easily offload parsing from the UI thread).

Parsing XFDF

To read from XFDF file:

Copy
1
2
3
4
5
6
7
// Your data provider, serving the XFDF file which we'll parse.
var yourDataProvider = SomeDataProvider()

// Async parse method is recommended (so you can easily offload parsing from the UI thread).
XfdfFormatter.parseXfdfAsync(pdfDocument, yourDataProvider)
    .subscribeOn(Schedulers.io()) // Specify thread on which to parse XFDF.
    .subscribe({annotations -> ...}) // Handle list of annotations here.
Copy
1
2
3
4
5
6
7
8
9
10
11
// Your data provider, serving the XFDF file which we'll parse.
DataProvider yourDataProvider = new SomeDataProvider();

// Async parse method is recommended (so you can easily offload parsing from the UI thread).
XfdfFormatter.parseXfdfAsync(pdfDocument, yourDataProvider)
    .subscribeOn(Schedulers.io()) // Specify thread on which to parse XFDF.
    .subscribe(new Consumer<List<Annotation>>() {
            @Override public void accept(@io.reactivex.annotations.NonNull List<Annotation> annotations) throws Exception {
                // Handle list of annotations here.
            }
        });

The list of annotations you get in the callback will not be added to the document automatically. This allows you to have better control over which of these annotations you want to add. To add annotations to a document, use document.getAnnotationProvider().addAnnotationToPage() on the annotations you want to add.

Exporting to XFDF

You can export annotations and form fields from the document to an XFDF file like so:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
// List of annotations from the document to be exported.
val listOfAnnotationsToExport = ...

// List of form fields from the document to be exported.
val listOfFormFieldsToExport = ...

// Output stream pointing to the XFDF file into which to write the data.
val outputStream = ...

// Async write method is recommended (so you can easily offload writing from the UI thread).
XfdfFormatter.writeXfdfAsync(pdfDocument, listOfAnnotationsToExport, listOfFormFieldsToExport, outputStream)
        .subscribeOn(Schedulers.io()) // Specify thread on which to write XFDF.
        .subscribe(..)
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
// List of annotations from the document to be exported.
List<Annotation> listOfAnnotationsToExport = ... ;

// List of form fields from the document to be exported.
List<FormField> listOfFormFieldsToExport = ... ;

// Output stream pointing to the XFDF file into which to write the data.
OutputStream outputStream = ... ;

// Async write method is recommended (so you can easily offload writing from the UI thread).
XfdfFormatter.writeXfdfAsync(pdfDocument, listOfAnnotationsToExport, listOfFormFieldsToExport, outputStream)
        .subscribeOn(Schedulers.io()) // Specify thread on which to write XFDF.
        .subscribe(..);