Unlock the Power of Direct PDF Editing with WebViewer 10.7

How to Export XFDF Annotations from PDF

By Apryse | 2020 Oct 21

Sanity Image
Read time

5 min

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:

  • How to Export XFDF from PDF with Acrobat
  • How to Export XFDF from PDF with JavaScript
  • More Ways to Import/Export XFDF
  • How an XFDF File is Structured
  • How to Open an XFDF File

How to Export XFDF from PDF with Acrobat

Copied to clipboard

Adobe Acrobat can extract XFDF annotations from PDF. The export option is part of the comments tool and accessed through the sidebar.

  1. Open the Comments list, choose Tools > Comments.
  2. Once you make your annotations, click on the three dots.
  3. From the options menu in the comments list, choose Export All to Data File.
  4. Name the file and choose Acrobat XFDF Files (*.xfdf) for the file type.
  5. Specify a location for the file, and then click Save.
Export All to Data File

How to Export XFDF from PDF with JavaScript

Copied to clipboard

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.

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);
});

More Ways to Import and Export 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();

How an XFDF File Is Structured

Copied to clipboard

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:

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>

How to Open XFDF Files (XFDF to PDF)

Copied to clipboard

Since XFDF acts as a minified version of a PDF, here’s how to open XFDF files in any standards-compliant PDF viewer.

  1. Double click on the .xfdf file – If your file preferences are set up correctly, the XFDF file will open in applications such as Adobe Acrobat or for free with Adobe Reader.
  2. If it doesn’t open, you may not have the correct application to open the .xfdf file. If you don’t have the correct application, you may have to download or purchase it.
  3. Once the .xfdf file opens, it will open in the associated application.
  4. The easiest way to use XFDF to open a PDF is with Adobe Acrobat, another Adobe Reader, or a free text editor.
  5. If the file opens as a text document, use the text editor to read or edit the file.

Further Resources

Copied to clipboard

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.

Sanity Image

Apryse

Share this post

email
linkedIn
twitter