WebViewer Version: latest
I’ve got an API which stores pages and their annotations. So in my case, annotations belongs to pages. So the API is serving pages and their annotations.
The API can serve different pages which are assembled on-the-fly to generate a “document” which is given to the PDFTron DocumentViewer.
That being said, here’s the code I’m using for loading existing annotations into the document’s right page, and for saving if any annotation has been added / edited or deleted :
// load annnotations
documentViewer.addEventListener(customEvent.LOAD_ANNOTATION, async ({ annotations }) => {
// I'm using WebViewer in different places of my app and I didn't need Annotations loading everywhere
if (enableAnnots && annotations?.length) {
const annots = await annotationManager.importAnnotCommand(annotations);
annots.forEach((a) => {
annotationManager.redrawAnnotation(a);
});
}
});
// Manual annotation changed
annotationManager.on('annotationChanged', async (annotations, action, { imported }) => {
// If the event is triggered by importing then it can be ignored
if (imported) return;
await documentViewer.getAnnotationsLoadedPromise();
const xfdfString = await annotationManager.exportAnnotationCommand();
// Send annotation change event for saving annotations to the right page in the API
onChangeAnnotation?.({ xfdfString, pageNum: documentViewer.getCurrentPage() });
});
The annotations loading works well.
But each time I’m adding an annotation on a page, it automatically erases all existing annotations on this page. I took a look at the annotationChanged
annotations
array, and it always contains the only annotation I just created, not the ones that previously existed.
So please can you tell me how to get all the annotations of the current page when an annotations has been added, modified or deleted OR how the get the whole annotations array in order to keep the existing ones and send the right XFDF string to the API?
Cheers!