Add Highlight Annotations to PDFs in 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 Highlight Annotations

A highlight annotation is a type of markup annotation.

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

{
	"bbox": [10, 10, 400, 400],
	"color": "#00FF00",
	"opacity": 1,
	"pageIndex": 0,
	"rects": [[10, 10, 400, 400]],
	"type": "pspdfkit/markup/highlight",
	"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));