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

Exporting XFDF

Copy
1
2
3
instance.exportXFDF().then(xfdf => {
  console.log(xfdf); // => <?xml version="1.0" encoding="UTF-8"?><xfdf xml:space="preserve" xmlns="http://ns.adobe.com/xfdf/"> ...
});
Copy
1
2
3
instance.exportXFDF().then(function(xfdf) {
  console.log(xfdf); // => <?xml version="1.0" encoding="UTF-8"?><xfdf xml:space="preserve" xmlns="http://ns.adobe.com/xfdf/"> ...
});

XFDF works seamlessly with the annotation API. If you want to persist annotations whenever changes are made, we recommend you use the annotations.didSave event. This event will be triggered automatically and can be configured via Configuration#autoSaveMode.

Note for standalone users: Instead of saving annotations to the backend, a save operation will persist the annotations in memory until they are either exported via Instance#exportXFDF or written to the PDF document when you export it using Instance#exportPDF. Unsaved annotations will not be exported, in order to reflect the server behavior:

Copy
1
2
3
4
5
6
7
8
9
10
instance.addEventListener("annotations.didSave", async () => {
  const xfdf = await instance.exportXFDF();
  await fetch("https://your-server.com/xfdf", {
    method: "post",
    headers: {
      "Content-Type": "application/vnd.adobe.xfdf"
    },
    body: JSON.stringify(xfdf)
  });
});
Copy
1
2
3
4
5
6
7
8
9
10
instance.addEventListener('annotations.didSave', function () {
  instance.exportXFDF().then(function (xfdf) {
      var req = new XMLHttpRequest();
      req.open("GET", "https://your-server.com/xfdf");
      req.setRequestHeader("Content-Type", "application/vnd.adobe.xfdf");
      req.send(JSON.stringify(xfdf));
      req.send();
    });
  });
});

Importing XFDF

Standalone Importing

To import XFDF when using standalone deployment, you can use the Configuration#XFDF option.

In addition to the Configuration#XFDF option, you can also set the Configuration#XFDFKeepCurrentAnnotations flag. This flag will make sure annotations that are already in the source PDF are kept and not replaced with those defined in the XFDF:

Copy
1
2
3
4
5
PSPDFKit.load({
  pdf: "https://your-server.com/some/document.pdf",
  XFDF:
    '<?xml version="1.0" encoding="UTF-8"?><xfdf xml:space="preserve" xmlns="http://ns.adobe.com/xfdf/"> ..'
});
Copy
1
2
3
4
5
PSPDFKit.load({
  pdf: "https://your-server.com/some/document.pdf",
  XFDF:
    '<?xml version="1.0" encoding="UTF-8"?><xfdf xml:space="preserve" xmlns="http://ns.adobe.com/xfdf/"> ..'
});

Server Importing

When you upload a new document to PSPDFKit Server, you can include an XFDF document as a second file in the upload. Follow the server guides for an in-depth example.