Import and export annotations from an XFDF file in UWP

XFDF is an XML-based standard from Adobe XFDF (ISO 19444-1:2016) for encoding annotations and form field values. It’s compatible with Adobe Acrobat and several other third-party frameworks.

Information

XFDF has various limitations. In most cases, using Nutrient Instant will result in a smaller file and better synchronization.

Nutrient UWP SDK 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

The following snippet shows how to import XFDF from a file, through 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.

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

using (var stream = Encoding.UTF8.GetBytes(xfdfAnnotationString).AsBuffer().AsStream())
{
    var xfdfProvider = new RandomAccessStreamDataProvider(stream.AsRandomAccessStream());
    await PdfView.Document.ImportXfdfAsync(xfdfProvider);
}

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

For more information on DataProviders and their uses, see our data providers guide.

Exporting to XFDF

You can export annotations and form field values from a document to an XFDF file like so:

using (var dataWriter = new DataWriter(new InMemoryRandomAccessStream()))
{
    await PDFView.Document.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 calling ExportXfdfToDataWriterAsync() on the Document.

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

var xfdfFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/annotation.xfdf"));

using (var dataWriter = new DataWriter(await xfdfFile.OpenAsync(FileAccessMode.ReadWrite)))
{
    await PDFView.Document.ExportXfdfToDataWriterAsync(dataWriter);
}

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.

  1. At the bottom of the page, choose Acrobat XFDF Files.

  2. Select the directory you wish to save the XFDF file to and name the file.

  3. 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