Some test text!

menu
search

Import/export annotations

There are a few ways to import or export annotations such as from a file, a database, or a document. There are also more advanced loading options to help with finer control of the data.

Advanced annotation loading

For more advanced control over the annotation loading process you can use the setPagesUpdatedInternalAnnotationsTransform function on DocumentViewer. When WebViewer loads a document it will import the annotation data as XFDF and this function allows you to transform that data before it gets loaded into the viewer.

The function that you pass to setPagesUpdatedInternalAnnotationsTransform may be called multiple times, specifying the list of pages that the annotation data is a part of.

You can use the function like this:

WebViewer(...)
  .then(function(instance) {
    var docViewer = instance.docViewer;

    docViewer.setPagesUpdatedInternalAnnotationsTransform(function(xfdfData, pageList, callback) {
      // if the pageList is [0, 1, 2, 3] then the data is of all annotations on the first four pages
      // make modifications here
      // ...
      callback(newXfdfData);
    });
  });

One way to use setPagesUpdatedInternalAnnotationsTransform is to replace the original annotation data inside the document with the data from your server. Note that this would also replace any existing links or form fields unless they are also saved on your server.

Alternatively you can modify the existing data that is passed in. When modifying the data it is easiest to first parse it into DOM elements, perform your modifications and then serialize back to a string.

An example of something you might want to do is remove all clickable links:

docViewer.setPagesUpdatedInternalAnnotationsTransform(function(xfdfData, callback) {
  var parser = new DOMParser();
  var xfdfElements = parser.parseFromString(xfdfData, 'text/xml');
  [].forEach.call(xfdfElements.querySelectorAll('link'), function(e) {
    e.parentNode.removeChild(e);
  });

  var serializer = new XMLSerializer();
  callback(serializer.serializeToString(xfdfElements));
});

Here's another example of changing the color attribute of every annotation to blue:

docViewer.setPagesUpdatedInternalAnnotationsTransform(function(xfdfData, callback) {
  var parser = new DOMParser();
  var xfdfElements = parser.parseFromString(xfdfData, 'text/xml');
  var annotations = xfdfElements.querySelector('annots').children;
  [].forEach.call(annotations, function(annotElement) {
    annotElement.setAttribute('color', '#0000FF');
  });

  var serializer = new XMLSerializer();
  callback(serializer.serializeToString(xfdfElements));
});

With setPagesUpdatedInternalAnnotationsTransform you have very fine control over all of the annotation data and can modify it exactly as you like.

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn More
close