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 JSON will result in a smaller file and better synchronization.

PSPDFKit for Windows supports both reading and writing XFDF files. The Document class contains ImportXfdfAsync(), ImportXfdfFileAsync(), ExportXfdfToDataWriterAsync(), and ExportXfdfToDataSinkAsync() methods you can use to perform these operations.

Importing XFDF

Read XFDF from File

Copy
1
2
3
// Get the xfdf `StorageFile`.
var xfdfFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/annotation.xfdf"));
await PdfView.Document.ImportXfdfFileAsync(xfdfFile);

This will parse the XFDF file, create any annotations that are required, and reload the document.

Import XFDF from a DataProvider

It is also possible to import from a custom DataProvider, thereby allowing for streams from different mediums:

Copy
1
2
3
4
5
using (var stream = Encoding.UTF8.GetBytes(xfdfString ?? "").AsBuffer().AsStream())
{
    var xfdfProvider = new RandomAccessStreamDataProvider(stream.AsRandomAccessStream());
    await PdfView.Document.ImportXfdfAsync(xfdfProvider);
}

The above is a simple example showing how to import XFDF from a string. However, the DataProvider can be expanded to, for example, implement custom readers for encryption.

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
using (var dataWriter = new DataWriter(new InMemoryRandomAccessStream()))
{
    await PdfView.Controller.GetPdfDocument().ExportXfdfToDataWriterAsync(dataWriter);

    var buffer = dataWriter.DetachBuffer();
    xfdfString = Encoding.UTF8.GetString(buffer.ToArray());
}

This example shows how the XFDF data can be exported to a string by simply calling [ExportXfdfAsync()][] on the Document.

To write to a file, just replace the stream with a file stream:

Copy
1
2
3
4
5
6
var xfdfFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/annotation.xfdf"));
using (var fileStream = await xfdfFile.OpenAsync(FileAccessMode.ReadWrite))
using (var dataWriter = new DataWriter(fileStream))
{
    await PdfView.Controller.GetPdfDocument().ExportXfdfToDataWriterAsync(dataWriter);
}