Add Text 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 Text Annotations
Text annotations can be placed anywhere on the screen. Keep in mind that fonts are platform specific, so you should only use fonts you know are present in the operating system where they should be displayed. If a font isn’t found, PSPDFKit will automatically fall back to a sans-serif font.
See the JSON format reference for all the properties that can be applied to text annotations.
Here’s an example of adding a text annotation with JSON:
{ "bbox": [10, 10, 400, 400], "borderWidth": 0, "createdAt": "2013-05-24T19:26:44Z", "updatedAt": "2013-05-24T19:26:44Z", "creatorName": "PSPDFKit", "font": "Georgia", "fontColor": "#7B10E6", "fontSize": 16, "horizontalAlign": "left", "verticalAlign": "top", "name": "ID1", "opacity": 1, "pageIndex": 0, "pdfObjectId": 42, "rotation": 0, "text": "Free Text", "type": "pspdfkit/text", "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));