Annotation delete all after flatten

I am using the following below standard code with a documentloaded listener, and this is prior to loading new annoations from my database. This is because i want to strip user annotations (and comments) that a user may have imported themselves from extrenal party software. The flattening of annotations is working but the but not the following -“annotationManager.deleteAnnotations(annotationManager.getAnnotationsList());” as i can see both flattened and annotations overlapped on the pdf in webviewer. Do you know what i could be doing wrong?

WebViewer({
fullAPI: true,
// other constructor options
}, viewerElement).then(instance => {
const { documentViewer, PDFNet, annotationManager } = instance.Core;

document.getElementById(‘myBtn’).addEventListener(‘click’, async () => {
await PDFNet.initialize();
const doc = await documentViewer.getDocument().getPDFDoc();

// export annotations from the document
const annots = await annotationManager.exportAnnotations();

// Run PDFNet methods with memory management
await PDFNet.runWithCleanup(async () => {

  // lock the document before a write operation
  // runWithCleanup will auto unlock when complete
  doc.lock(); 

  // import annotations to PDFNet
  const fdf_doc = await PDFNet.FDFDoc.createFromXFDF(annots);
  await doc.fdfUpdate(fdf_doc);

  // flatten all annotations in the document
  await doc.flattenAnnotations();

  // or optionally only flatten forms
  // await doc.flattenAnnotations(true);

  // clear the original annotations
  annotationManager.deleteAnnotations(annotationManager.getAnnotationsList());

  // optionally only clear widget annotations if forms were only flattened
  // const widgetAnnots = annots.filter(a => a instanceof Annotations.WidgetAnnotation);
  // annotationManager.deleteAnnotations(widgetAnnots);
});

// clear the cache (rendered) data with the newly updated document
documentViewer.refreshAll();

// Update viewer to render with the new document
documentViewer.updateView();

// Refresh searchable and selectable text data with the new document
documentViewer.getDocument().refreshTextData();

});
});

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 @sam.t.saber,

I think what you are missing here is that you are trying to delete annotations created by a different user than the one you have currently in the viewer, and for this reason the deletion fails due to permissions.

What you can do is call deleteAnnotations with the options object, and pass the force: true flag. This will then override any permissions and delete the annotations, leaving you only with the flattened annotations.

Let me know if this works for you.

Best Regards,
Armando Bollain
Software Developer
PDFTron Systems, Inc.
www.pdftron.com

thanks. worked. annotationManager.deleteAnnotations(annotationManager.getAnnotationsList(), {force:true});