Annotations can be read from, and written to, a MuPDF document instance.
In this way a developer is able to separately store annotations and programmatically manage them.
To read annotation data from a document we use the annotation.get() method.
Once we have the data we can work with it just as we would with any JSON object.Example
Copy
async function saveAnnotationsLocally() { const annotations = await mupdf.annotation.get(); let annotsJSON = JSON.stringify(annotations) // for example store the data in local storage localStorage.setItem('pdfAnnotations', annotsJSON); mupdf.toast.show({ type: 'success', content: 'Annotations saved locally' });}
To write annotation data to a document we use the annotation.add() method.For example, to write annotations to the document from JSON data stored in local storage we could do the following:Example
Annotations can be removed from a document by using the annotation.remove() method.All that is required is to send through the name or oid identifier along with the pageIndex for the annotation you want to remove.Example
Copy
mupdf.annotation.remove({annotations:[{name:"squiggle_1", pageIndex:0]); // remove by namemupdf.annotation.remove({annotations:[{oid:30, pageIndex:1]); // remove by oid
When creating annotations in MuPDF WebViewer you can predefine the author of the annotation by listening to the ANNOTATION_CREATE event and then setting the annotation metadata for the author name.Example