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.

Importing and exporting annotations using the document

Another option is to merge the annotations back into the document, avoiding the need to handle XFDF separately. It is achieved by using getFileData which returns an ArrayBuffer of the PDF with annotations. It can be sent to the server with a POST request, so that the file can be updated on the server.

1WebViewer(...)
2 .then(instance => {
3 const { documentViewer, annotationManager } = instance.Core;
4 const filename = 'myfile.pdf';
5
6 // load a document with annotations
7 instance.UI.loadDocument('https://myserver.com/myfile.pdf', { filename });
8
9 // later save the document with updated annotations data
10 documentViewer.addEventListener('annotationsLoaded', () => {
11 annotationManager.exportAnnotations().then(xfdfString => {
12 const doc = documentViewer.getDocument();
13 doc.getFileData({ xfdfString }).then(data => {
14 const arr = new Uint8Array(data);
15 const blob = new Blob([ arr ], { type: 'application/pdf' });
16 const formData = new FormData();
17 formData.append('blob', blob);
18 fetch(`/server/annotationHandler.js?filename=${filename}`, {
19 method: 'POST',
20 body: formData // written into a PDF file in the server
21 });
22 })
23 });
24 })
25 })
26
27// Full sample is available at the end of this section.
Apryse Docs Image

This setup can also be useful for applications that do not have a server or that will handle documents in the user's device locally. However, it would not be suitable for applications where multiple users are annotating and sharing the same document.

For samples about saving annotations into the document itself, see Github repo below:

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales