Available Now: Explore our latest release with enhanced accessibility and powerful IDP features
By Apryse | 2020 Oct 21
5 min
Tags
tutorial
annotation
XFDF is an XML-based format and stands for XML Forms Data Format. Like other PDF components, XFDF serves many possible uses. It’s most commonly used to exchange PDF annotations and form data between computers independently of the underlying PDF file. In this manner, XFDF serves as an efficient medium of annotations interchange to support an online workflow like document collaboration.
In this article, we show how to export XFDF annotations from PDF in a few easy ways and what else you can do with XFDF.
Table of contents:
Adobe Acrobat can extract XFDF annotations from PDF. The export option is part of the comments tool and accessed through the sidebar.
While Acrobat is great for single end-users, developers looking to build a document collaboration workflow around PDF prefer a solution that lets them extract annotations to a server. Because XFDF is an important element in PDF manipulation, the WebViewer SDK by Apryse also offers a few ways to export XFDF from PDF.
WebViewer loads and saves XFDF separately as an overlay (e.g., for document collaboration) that can also be embedded directly in the PDF file.
1. Extract from a new document: The first method doesn’t require a visible viewer. Create a PDF document in memory and then call the extractXFDF
function:
const documentURL = 'https://pdftron.s3.amazonaws.com/downloads/pl/demo-annotated.pdf';
const doc = await Core.createDocument(documentURL);
const xfdf = await doc.extractXFDF();
console.log(xfdf);
2. Extract from an existing document: This next method assumes you’ve uploaded a document in WebViewer already and it’s visible on screen. Here you can use the exportAnnotations function on AnnotationManager to get the XFDF for all of the annotations currently loaded.
WebViewer({
// options here
}).then(async (instance) => {
const { documentViewer, annotationManager } = instance.Core;
await documentViewer.getAnnotationsLoadedPromise();
const xfdf = await annotationManager.exportAnnotations();
console.log(xfdf);
});
3. Extract changed annotations: Finally, WebViewer can also export the XFDF for specific, individual annotations that have recently changed. This makes it easier to implement features like real-time collaboration, where users communicate with one another on the same documents with their markups, comments, and replies to comments.
WebViewer({
// options here
}).then(async (instance) => {
const { documentViewer, annotationManager } = instance.Core;
await documentViewer.getAnnotationsLoadedPromise();
const xfdf = await annotationManager.exportAnnotations();
console.log(xfdf);
});
There are a few more ways to import and export XFDF annotations, along with more advanced loading options to help with finer control of the data. To learn more, visit the Apryse documentation pages on Import/Export Advanced Annotation Loading and AnnotationManager.
Besides WebViewer, Apryse C# SDK also offers the ability to create PDF from XML C#, and to import and export XFDF on most popular platforms and programming languages, such as importing XML to PDF. Here’s an example of how to extract data from PDF to FDF, then export FDF as XFDF, using the cross-platform (Core) Apryse SDK.
PDFDoc doc = new PDFDoc(filename);
// Extract annotations to FDF.
// Optionally use e_both to extract both forms and annotations
FDFDoc doc_fields = doc.FDFExtract(PDFDoc.ExtractFlag.e_annots_only); //PDFDoc.ExtractFlag.e_forms_only
// Export annotations from FDF to XFDF.
doc_fields.SaveAsXFDF(output_xfdf_filename);
// Optionally write XFDF to a string
string XFDF_string = doc_fields.SaveAsXFDF();
XML is basically a structured language that begins with the XML file itself as the topmost object. XFDF files have an element called fields that contain Field and Value.
Here’s the basic XFDF structure:
Inside of the following sample XFDF file (beneath) you’ll notice several checkbox, combox, listbox, and multiline text elements. Checkbox 1, for example, contains value ‘A’.
Checkboxes always have just two possible values, true or false. However, in a PDF file, you can have multiple states with different appearances linked to each of them.
<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>
<field name="CheckBox1">
<value>A</value>
</field>
<field name="CheckBox2">
<value>Off</value>
</field>
<field name="CheckBox3">
<value>Off</value>
</field>
<field name="ComboBox1">
<value>Option 1</value>
</field>
<field name="ListBox1">
<value>Item2</value>
</field>
<field name="MultiLineText1">
<value>This is a Multi-line text box. Note that the text wraps around to the next line!</value>
</field>
<field name="RadioGroup1">
<value>Yes</value>
</field>
<field name="TextBox1">
<value>Sample Text</value>
</field>
</fields>
</xfdf>
Since XFDF acts as a minified version of a PDF, here’s how to open XFDF files in any standards-compliant PDF viewer.
For a more detailed look into XFDF and XFDF file manipulation, with code examples for developers, visit the Apryse documentation page.
Additionally, check out our “Saving PDF Annotations, Creating Annotations in React JavaScript” video on YouTube for more information.
We hope this article was helpful! If you have any questions or feedback about our PDF SDK, don’t hesitate to contact us.
PRODUCTS
Enterprise
Small Business
Popular Content