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.

How to Export XFDF from PDF with Acrobat

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

How to Export XFDF from PDF with JavaScript

While Acrobat is great for single end-users, developers looking to build a document collaboration workflow around PDF would prefer a solution that lets them extract annotations to a server. Because XFDF is an important element in PDF manipulation, the WebViewer SDK by PDFTron 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 even 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();
  1. 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.
  // options here
}).then(async (instance) => {
  const { documentViewer, annotationManager } = instance.Core;
  await documentViewer.getAnnotationsLoadedPromise();
  const xfdf = await annotationManager.exportAnnotations();
  1. 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.
  // options here
}).then(async (instance) => {
  const { documentViewer, annotationManager } = instance.Core;

  await documentViewer.getAnnotationsLoadedPromise();
  const xfdf = await annotationManager.exportAnnotations();

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 PDFTron documentation pages on Import/Export Advanced Annotation Loading and AnnotationManager.

Besides WebViewer, PDFTron C# SDK also offers the ability to import and export XFDF on most popular platforms and programming languages. Here’s an example of how to extract data from PDF to FDF, then export FDF as XFDF, using the cross-platform (Core) PDFTron 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.

// Optionally write XFDF to a string
string XFDF_string = doc_fields.SaveAsXFDF();

How an XFDF File is Structured

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:

<XFDF>Topmost element
<fields>All the value fields for this template
<field (T)>nameField name
<value (V)>valueField value

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, in most cases. 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">
      <field name="CheckBox1">
      <field name="CheckBox2">
      <field name="CheckBox3">
      <field name="ComboBox1">
      <value>Option 1</value>
      <field name="ListBox1">
      <field name="MultiLineText1">
      <value>This is a Multi-line text box. Note that the text wraps around to the next line!</value>
      <field name="RadioGroup1">
      <field name="TextBox1">
      <value>Sample Text</value>

How to Open an XFDF File

Since XFDF acts as a minified version of a PDF, you should be able to open it 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 open an XFDF file 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

For a more detailed look into XFDF and XFDF file manipulation, with code examples for developers, visit the PDFTron documentation page.

We hope this article was helpful! If you have any questions or feedback about our PDF SDK, don’t hesitate to contact us.