Annotations
Add, list, move, remove and flatten PDF annotations, and import or export XFDF, with NexPDF for Java.
License module
Creating and changing annotations needs the annotations module (Indie tier and above). Listing
annotations with annotationsJson works without a key.
Coordinates are PDF points with the origin at the bottom-left of the page. Rectangles are
llx, lly, urx, ury.
Add markup and shapes
addAnnotation(pageIndex, kind, colorHex, opacity, contents, llx, lly, urx, ury) adds an annotation
of a PDF subtype such as Highlight, Underline, StrikeOut, Squiggly, Square, Circle,
FreeText or Text (a sticky note). Text markup is fitted to the lines under the rectangle.
try (NexPdfDocument doc = NexPdfDocument.open(Path.of("contract.pdf"))) {
doc.addAnnotation(0, "Highlight", "#FFD400", 1.0, "", 72, 700, 300, 716);
doc.addAnnotation(0, "FreeText", "#E5484D", 1.0, "Checked", 72, 640, 260, 660);
doc.addAnnotation(0, "Text", "#2563EB", 1.0, "Please confirm this date", 320, 700, 340, 720);
doc.save(Path.of("annotated.pdf"));
}Lines, arrows and ink
// A red arrow from (100, 500) to (250, 560), 2 pt wide.
doc.addLine(0, "Arrow", "#DC2626", 1.0, 2.0, 100, 500, 250, 560);
// Freehand ink: points "x,y;x,y;..." in PDF points.
doc.addInk(0, "#1D4ED8", 2.0, 1.0, "100,400;120,410;140,405;160,420");A handwritten signature is an ink annotation with several strokes separated by |:
doc.addInkSignature(0, "#111827", 1.5, "300,120;320,130;340,118|350,125;380,128");This is a visual signature only. For a cryptographic signature see Signatures.
Image stamps and measurements
// A picture stamp from RGBA pixels (width x height x 4 bytes), here 240 x 120.
doc.addImageStamp(0, 400, 60, 520, 120, rgba, 240, 120, 1.0);
// A stored distance measurement; per_point converts PDF points to the unit.
String m = "{\"kind\":\"distance\",\"points\":[[100,100],[300,100]],\"unit\":\"mm\",\"per_point\":0.3528}";
System.out.println(doc.addMeasurement(0, m)); // JSON with the formatted "text"kind is distance, perimeter or area. doc.measurementsJson() lists the stored ones.
List, edit, move and remove
ObjectMapper json = new ObjectMapper(); // Jackson, one JSON library among many
for (JsonNode a : json.readTree(doc.annotationsJson(0))) {
System.out.println(a.get("num") + " " + a.get("gen") + " " + a.get("kind") + " " + a.get("contents"));
}
JsonNode a = json.readTree(doc.annotationsJson(0)).get(0);
int num = a.get("num").asInt();
short gen = (short) a.get("gen").asInt();
doc.setAnnotationContents(num, gen, "Updated note");
doc.moveAnnotation(num, gen, 10, -20); // dx, dy in points
doc.removeAnnotation(0, num, gen);Annotations are identified by their object number (num) and generation (gen, a short).
Flatten
Flattening burns annotations into the page content so they can no longer be edited:
doc.flattenAnnotations(-1); // every page; pass a page index for one pageXFDF import and export
String xfdf = doc.exportXfdf();
try (NexPdfDocument other = NexPdfDocument.open(Path.of("clean.pdf"))) {
other.importXfdf(xfdf);
other.save(Path.of("with-annotations.pdf"));
}File attachments
addFileAttachmentAnnot(pageIndex, x, y, name, data, mime, description) adds a paperclip icon whose
top-left corner is at (x, y):
byte[] xml = Files.readAllBytes(Path.of("invoice.xml"));
doc.addFileAttachmentAnnot(0, 520, 760, "invoice.xml", xml, "application/xml", "Source data");