Deleting annotations in Android

Using the Apryse library, you can delete a single annotation, delete all the annotations on a page, or delete all the annotations in a PDF document.

The Apryse library has an annotation list dialog fragment. The dialog fragment allows users to delete annotations easily. For more information, see annotation dialog fragment .

To delete an annotation, you need the page number of the annotation. To avoid looping through the whole document to find the page number of the annotation, we recommend you always keep track of the page number when you get an annotation.

Delete an annotation

To delete an annotation, you need to lock PDFViewCtrl first. See the section on document locking for more information.

You can delete an annotation as follows:

1public void deleteAnnotation(final PDFViewCtrl pdfViewCtrl, final Annot annot, final int pageNum) throws Exception {
2 // Lock the document first, because accessing annotation/doc information isn't thread-safe.
3 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
4 @Override
5 public void run() throws Exception {
6 Page page = pdfViewCtrl.getDoc().getPage(pageNum);
7 page.annotRemove(annot);
8 pdfViewCtrl.update(annot, pageNum);
9 }
10 });
11}

Delete all annotations on a page

The following snippet shows how to enumerate all the annotations on a page, as well as how to delete them.

1void deleteAnnotationsOnPage(final PDFViewCtrl pdfViewCtrl, final int pageNum) throws Exception {
2 // Lock the document first, because accessing annotation/doc information isn't thread-safe.
3 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
4 @Override
5 public void run() throws Exception {
6 Page page = pdfViewCtrl.getDoc().getPage(pageNum);
7 int annotCount = page.getNumAnnots();
8 while (annotCount > 0) {
9 page.annotRemove(0);
10 annotCount = page.getNumAnnots();
11 }
12 pdfViewCtrl.update(true);
13 }
14 });
15}

Delete all the annotations in a PDF document

The following snippet shows how to traverse all the pages in a PDF document and delete all the annotations.

1void deleteAllAnnotations(final PDFViewCtrl pdfViewCtrl) throws Exception {
2 // Lock the document first, because accessing annotation/doc information isn't thread-safe.
3 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
4 @Override
5 public void run() throws Exception {
6 PDFDoc pdfDoc = pdfViewCtrl.getDoc();
7 PageIterator itr = pdfDoc.getPageIterator();
8 while (itr.hasNext()) {
9 Page page = (Page) itr.next();
10 int annotCount = page.getNumAnnots();
11 while (annotCount > 0) {
12 page.annotRemove(0);
13 annotCount = page.getNumAnnots();
14 }
15 }
16 pdfViewCtrl.update(true);
17 }
18 });
19}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales