Add Shape 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 Shape Annotations

[Shape annotations][] of the following types can be created (click the links for the JSON format specification):

Here’s an example of adding an ellipse 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/ellipse",
	"v": 1

To create rectangles, see the rectangle annotation guide. For other shapes, refer to the annotation JSON format.

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));