Edit PDF Annotations in Java
There is no API for editing annotations directly in PSPDFKit for Java. This guide shows you how to do it using the AnnotationProvider
’s add, get, and remove functionality:
-
Get the
AnnotationProvider
from thePdfDocument
:final AnnotationProvider annotationProvider = document.getAnnotationProvider();
-
Get all the annotations for the page on which you want to edit an annotation. This returns a
JSONArray
containing the JSON representations for each annotation on the page:final int pageIndex = 0; // First page. final JSONArray annotations = annotationProvider.getAnnotationsJson(pageIndex);
-
Check which annotation you want to edit. For example, if you want to edit the first one in the array, get the first annotation:
final JSONObject annotationToEdit = annotations.getJSONObject(0);
-
Decide which property you want to edit. For example, to change the highlight color of the highlight markup annotation to red, do the following:
annotationToEdit.put("color", "#FF0000");
-
Remove the annotation from the document. For more details on this step, see the annotation removal guide:
final int pdfObjectId = annotationToEdit.getInt("pdfObjectId"); annotationProvider.removeAnnotation(pageIndex, pdfObjectId);
-
Add the annotation back using the
JSONObject
edited in the previous steps:annotationProvider.addAnnotationJson(annotationToEdit);