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:

1WebViewer(...)
2 .then(instance => {
3 const { documentViewer } = instance.Core;
4
5 documentViewer.setPagesUpdatedInternalAnnotationsTransform((xfdfData, pageList, callback) => {
6 // if the pageList is [0, 1, 2, 3] then the data is of all annotations on the first four pages
7 // make modifications here
8 // ...
9 callback(newXfdfData);
10 });
11 });

Examples

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:

1WebViewer(...)
2 .then(instance => {
3 const { docViewer } = instance.Core;
4 docViewer.setPagesUpdatedInternalAnnotationsTransform((xfdfData, pageList, callback) => {
5 const parser = new DOMParser();
6 const xfdfElements = parser.parseFromString(xfdfData, 'text/xml');
7 [].forEach.call(xfdfElements.querySelectorAll('link'), e => {
8 e.parentNode.removeChild(e);
9 });
10
11 const serializer = new XMLSerializer();
12 callback(serializer.serializeToString(xfdfElements));
13 });
14 })

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

1WebViewer(...)
2 .then(instance => {
3 const { docViewer } = instance.Core;
4 docViewer.setPagesUpdatedInternalAnnotationsTransform((xfdfData, pageList, callback) => {
5 const parser = new DOMParser();
6 const xfdfElements = parser.parseFromString(xfdfData, 'text/xml');
7 const annotations = xfdfElements.querySelector('annots').children;
8 [].forEach.call(annotations, annotElement => {
9 annotElement.setAttribute('color', '#0000FF');
10 });
11
12 const serializer = new XMLSerializer();
13 callback(serializer.serializeToString(xfdfElements));
14 });
15 })

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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales