Aller au contenu
NexPDF

Annotations

Ajoutez, listez, déplacez, supprimez et aplatissez des annotations PDF, et importez ou exportez du XFDF, avec NexPDF pour Java.

Français

Module de licence

La création et la modification des annotations nécessitent le module annotations (niveau Indie et supérieurs). Lister les annotations avec annotationsJson fonctionne sans clé.

Les coordonnées sont des points PDF, l'origine se trouvant en bas à gauche de la page. Les rectangles s'écrivent llx, lly, urx, ury.

Ajouter du marquage et des formes

addAnnotation(pageIndex, kind, colorHex, opacity, contents, llx, lly, urx, ury) ajoute une annotation d'un sous-type PDF tel que Highlight, Underline, StrikeOut, Squiggly, Square, Circle, FreeText ou Text (une note autocollante). Le marquage de texte s'ajuste aux lignes situées sous le 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"));
}

Lignes, flèches et encre

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

Une signature manuscrite est une annotation d'encre avec plusieurs traits séparés par | :

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

Il s'agit uniquement d'une signature visuelle. Pour une signature cryptographique, voir Signatures.

Tampons image et mesures

// 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 vaut distance, perimeter ou area. doc.measurementsJson() liste celles qui sont enregistrées.

Lister, modifier, déplacer et supprimer

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

Les annotations sont identifiées par leur numéro d'objet (num) et leur génération (gen, un short).

Aplatir

L'aplatissement incruste les annotations dans le contenu de la page, de sorte qu'elles ne sont plus modifiables :

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

Import et export XFDF

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

Pièces jointes

addFileAttachmentAnnot(pageIndex, x, y, name, data, mime, description) ajoute une icône de trombone dont le coin supérieur gauche se trouve en (x, y) :

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

Sur cette page