Remove Annotations in Java

To remove annotations, use the AnnotationsProvider.removeAnnotation() method, providing the pdfObjectId, i.e. the unique identifier, of the annotation you want to remove. In the following example, we’re fetching all annotations for a single page using getAnnotationsJson() and retrieve the identifier of the first annotation on the page:

// Get the annotation provider.
AnnotationProvider annotationProvider = document.getAnnotationProvider();
// Get all the annotations for page 0.
JSONArray annotations = annotationProvider.getAnnotationsJson(0);
// To remove the first one:
JSONObject firstAnnotation = annots.getJSONObject(0);
// Get the PDF object ID to remove it.
int pdfObjectId = firstAnnotation.getInt("pdfObjectId");

To delete an annotation, use AnnotationsProvider.removeAnnotation and pass the pageIndex and pdfObjectId of the annotation in the document:

annotationProvider.removeAnnotation(0, pdfObjectId);