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

An ink annotation consists of a series of x,y coordinate Points (in pixels) starting from the top left of the page. Each point has a corresponding Intensity with a value from 0.0 to 1.0 (the default is 0.5).

Here’s an example of adding an ink annotation with JSON:

{
	"id": "10",
	"v": 1,
	"pdfObjectId": 10,
	"pageIndex": 0,
	"bbox": [0, 0, 700, 900],
	"opacity": 1,
	"type": "pspdfkit/ink",
	"lines": {
		"points": [
			[
				[445.5, 503.9],
				[438.6, 503.1],
				[433.4, 500.5],
				[267.5, 435.2]
			]
		],
		"intensities": [[0.5, 0.5, 0.5, 0.5]]
	},
	"lineWidth": 5,
	"strokeColor": "#2483c7",
	"isDrawnNaturally": false,
	"isSignature": false
}

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