Create rectangle annotations in PDFs using Java

Annotations are created using the AnnotationProvider.AddAnnotationJson method. To create an annotation on a page, use the annotation JSON format to add the annotation you want.

Creating rectangle annotations

A rectangle annotation is a type of [shape][shape-guide] annotation.

Here’s an example of adding a rectangle annotation with JSON:

{
"bbox": [10, 10, 400, 400],
"blendMode": "normal",
"creatorName": "Me",
"opacity": 1,
"pageIndex": 0,
"pdfObjectId": 363,
"strokeColor": "#F74A3E",
"fillColor": "#FFFB00",
"strokeWidth": 3,
"type": "pspdfkit/shape/rectangle",
"v": 1
}

The JSON can be loaded from a file and added using addAnnotationJson:

String jsonString = Files.readAllBytes(Paths.get("path/to/my-annotation.json")).toString();
document.getAnnotationProvider().addAnnotationJson(new JSONObject(jsonString));