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();
});
});