Create Link Annotations in PDFs with 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.

A link annotation can be used to trigger an Action when clicked. The link will be drawn on the bounding box.

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

{
	"action": {
		"pageIndex": 1,
		"type": "goTo"
	},
	"bbox": [10, 10, 400, 400],
	"createdAt": "2021-05-24T19:26:44Z",
	"opacity": 1,
	"pageIndex": 0,
	"pdfObjectId": 13,
	"type": "pspdfkit/link",
	"updatedAt": "2021-05-24T19:26:44Z",
	"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));