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:

  1. Get the AnnotationProvider from the PdfDocument:

    final AnnotationProvider annotationProvider = document.getAnnotationProvider();
  2. 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);
  3. 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);
  4. 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");
  5. 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);
  6. Add the annotation back using the JSONObject edited in the previous steps:

    annotationProvider.addAnnotationJson(annotationToEdit);