Zum Inhalt springen
NexPDF

Annotationen

PDF-Annotationen mit NexPDF für Java hinzufügen, auflisten, verschieben, entfernen und verflachen sowie XFDF importieren oder exportieren.

Deutsch

Lizenzmodul

Das Erstellen und Ändern von Annotationen erfordert das Modul annotations (Tarif Indie und höher). Das Auflisten von Annotationen mit annotationsJson funktioniert ohne Schlüssel.

Koordinaten sind PDF-Punkte mit dem Ursprung unten links auf der Seite. Rechtecke sind llx, lly, urx, ury.

Markierungen und Formen hinzufügen

addAnnotation(pageIndex, kind, colorHex, opacity, contents, llx, lly, urx, ury) fügt eine Annotation eines PDF-Untertyps hinzu, etwa Highlight, Underline, StrikeOut, Squiggly, Square, Circle, FreeText oder Text (eine Haftnotiz). Textmarkierungen werden an die Zeilen unter dem Rechteck angepasst.

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

Linien, Pfeile und Freihand

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

Eine handschriftliche Signatur ist eine Freihand-Annotation mit mehreren, durch | getrennten Strichen:

doc.addInkSignature(0, "#111827", 1.5, "300,120;320,130;340,118|350,125;380,128");

Das ist nur eine visuelle Signatur. Für eine kryptografische Signatur siehe Signaturen.

Bildstempel und Messungen

// 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 ist distance, perimeter oder area. doc.measurementsJson() listet die gespeicherten auf.

Auflisten, bearbeiten, verschieben und entfernen

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

Annotationen werden durch ihre Objektnummer (num) und Generation (gen, ein short) identifiziert.

Verflachen

Das Verflachen brennt Annotationen in den Seiteninhalt ein, sodass sie nicht mehr bearbeitet werden können:

doc.flattenAnnotations(-1);   // every page; pass a page index for one page

XFDF-Import und -Export

String xfdf = doc.exportXfdf();
try (NexPdfDocument other = NexPdfDocument.open(Path.of("clean.pdf"))) {
    other.importXfdf(xfdf);
    other.save(Path.of("with-annotations.pdf"));
}

Dateianhänge

addFileAttachmentAnnot(pageIndex, x, y, name, data, mime, description) fügt ein Büroklammer-Symbol hinzu, dessen obere linke Ecke bei (x, y) liegt:

byte[] xml = Files.readAllBytes(Path.of("invoice.xml"));
doc.addFileAttachmentAnnot(0, 520, 760, "invoice.xml", xml, "application/xml", "Source data");

Auf dieser Seite