importAnnotations doesn't clear existing annotations when empty list is passed

WebViewer Version: 8.6.0

Do you have an issue with a specific file(s)? No
Can you reproduce using one of our samples or online demos?
Are you using the WebViewer server?
Does the issue only happen on certain browsers?
Is your issue related to a front-end framework?
Is your issue related to annotations? Yes

Please give a brief summary of your issue:
I have annotations in my document bucketed in several categories. The user can select a category from a drop-down (separate from the WebViewer component) and I pass the filtered list of annotations to the WebViewer to be displayed, replacing any annotations that were displayed prior to the userā€™s category selection. This is working fine in most cases, but when the user selects a category that doesnā€™t have any annotations, the WebViewer continues to show the previously displayed annotations (i.e. the ā€œreplaceā€ operation appears to be skipped).

Please describe your issue and provide steps to reproduce it:
Iā€™m calling importAnnotations like this: annotationManager.importAnnotations(xfdf, { replace : [Core.Annotations.Annotation] }) with the XFDF string for the empty list like this: <?xml version="1.0" encoding="UTF-8" ?><xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"><fields /><annots></annots><pages><defmtx matrix="1,0,0,-1,0,843" /></pages></xfdf> . Note that you must first load some annotations before passing the empty string in order to see the issue.

Please provide a link to a minimal sample where the issue is reproducible:

Hello, Iā€™m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi Ryan,

If you try zoom in after importing the empty annotation, you will see that the previous annotations are all gone as expected because the pages are ā€œrefreshedā€.

Itā€™s recommended to refresh annotations after importing. For your particular case, you can try:

// First, get all the page numbers of current annotations
const pageNumbers = annotationManager.getAnnotationsList().map(annot=>annot.PageNumber);

// But there can be duplicated page numbers if a page has multiple annotations, so lets remove the duplicate ones:
const pageNumbersOfOldAnnotations = [...new Set(pageNumbers)]

// your xfdf importing logic here
annotationManager.importAnnotations(xfdf, { replace : [instance.Core.Annotations.Annotation] });

pageNumbersOfOldAnnotations.forEach(pageNumber=> {
  // This line will refresh the pages that previously had annotations
  annotationManager.drawAnnotations({pageNumber})
})
// This line will refresh the pages that currently have annotations
annotationManager.drawAnnotationsFromList(annotationManager.getAnnotationsList())

Thanks for the response Yixiao. Indeed after refreshing the old pages I no longer see the annotations appearing in the document, however they are still present in the notes panel - is there a way to similarly refresh that element?

You can use annotationManager.deleteAnnotations() so the code becomes

const pageNumbers = annotationManager.getAnnotationsList().map(annot=>annot.PageNumber);
const pageNumbersOfOldAnnotations = [...new Set(pageNumbers)];

annotationManager.deleteAnnotations(annotationManager.getAnnotationsList())
annotationManager.importAnnotations(xfdf);

annotationManager.drawAnnotationsFromList(annotationManager.getAnnotationsList())
pageNumbersOfOldAnnotations.forEach(pageNumber=> {
  annotationManager.drawAnnotations({pageNumber})
})

The problem with calling deleteAnnotations is that it triggers an annotationChanged event, and my handler would treat this the same as a delete action by the user rather than just temporarily hiding the annotations. It looks like I am able to work around this by using the options argument in deleteAnnotations, but this feels like a hacky solution to a missed edge case. The ā€˜replaceā€™ option works well for me in every other scenario, so I would expect it to work exactly the same when Iā€™m replacing the current annotations with an empty list. I hope the development team will consider looking into this and addressing it in a future release.

That makes sense. Iā€™m adding this to our backlog now.

1 Like