Zum Inhalt springen
NexPDF

Annotationen

PDF-Annotationen hinzufügen, auflisten, verschieben, entfernen und verflachen sowie XFDF importieren und exportieren mit NexPDF für iOS (Swift).

Deutsch

Lizenzmodul

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

Koordinaten sind PDF-Punkte mit dem Ursprung unten links auf der Seite. Rechtecke sind llx, lly, urx, ury. Der Reader bietet dem Benutzer in seinem Annotationsmodus dieselben Werkzeuge.

Markierungen und Formen hinzufügen

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

import NexPdf

let doc = try NexPdfDocument.open(url: url)
try doc.addAnnotation(pageIndex: 0, kind: "Highlight", colorHex: "#FFD400", opacity: 1, contents: "",
                      llx: 72, lly: 700, urx: 300, ury: 716)
try doc.addAnnotation(pageIndex: 0, kind: "FreeText", colorHex: "#E5484D", opacity: 1, contents: "Checked",
                      llx: 72, lly: 640, urx: 260, ury: 660)
try doc.addAnnotation(pageIndex: 0, kind: "Text", colorHex: "#2563EB", opacity: 1, contents: "Please confirm this date",
                      llx: 320, lly: 700, urx: 340, ury: 720)
try doc.write(to: outputURL)

Linien, Pfeile und Freihand

// A red arrow from (100, 500) to (250, 560), 2 pt wide.
try doc.addLine(pageIndex: 0, kind: "Arrow", colorHex: "#DC2626", opacity: 1, strokeWidth: 2,
                x1: 100, y1: 500, x2: 250, y2: 560)

// Freehand ink: points "x,y;x,y;..." in PDF points.
try doc.addInk(pageIndex: 0, colorHex: "#1D4ED8", strokeWidth: 2, opacity: 1,
               coords: "100,400;120,410;140,405;160,420")

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

try doc.addInkSignature(pageIndex: 0, colorHex: "#111827", strokeWidth: 1.5,
                        strokes: "300,120;320,130;340,118|350,125;380,128")

Das ist nur eine visuelle Signatur. Zur kryptografischen Signatur siehe Signaturen.

Bildstempel und Messungen

// A picture stamp from RGBA pixels (width x height x 4 bytes), here 240 x 120.
try doc.addImageStamp(page: 0, llx: 400, lly: 60, urx: 520, ury: 120,
                      rgba: rgbaData, width: 240, height: 120, opacity: 1)

// A stored distance measurement; per_point converts PDF points to the unit.
let m = #"{"kind":"distance","points":[[100,100],[300,100]],"unit":"mm","per_point":0.3528}"#
let result = NexPdf.json(try doc.addMeasurement(page: 0, json: m)) as? [String: Any]
print(result?["text"] ?? "")

kind ist distance, perimeter oder area. measurementsJson() listet die gespeicherten auf.

Auflisten, bearbeiten, verschieben und entfernen

let annots = NexPdf.json(try doc.annotationsJson(pageIndex: 0)) as? [[String: Any]] ?? []
for a in annots {
    print(a["num"] ?? 0, a["gen"] ?? 0, a["kind"] ?? "", a["contents"] ?? "")
}

if let a = annots.first, let n = a["num"] as? Int, let g = a["gen"] as? Int {
    let num = UInt32(n), gen = UInt16(g)
    try doc.setAnnotationContents(num: num, generation: gen, contents: "Updated note")
    try doc.moveAnnotation(num: num, generation: gen, dx: 10, dy: -20)    // in points
    try doc.removeAnnotation(pageIndex: 0, num: num, generation: gen)
}

Annotationen werden über ihre Objektnummer (num) und Generation (gen) identifiziert.

Verflachen

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

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

XFDF-Import und -Export

let xfdf = try doc.exportXfdf()
let other = try NexPdfDocument.open(url: cleanURL)
try other.importXfdf(xfdf: xfdf)
try other.write(to: outputURL)

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:

try doc.addFileAttachmentAnnot(pageIndex: 0, x: 520, y: 760, name: "invoice.xml",
                               data: xmlData, mime: "application/xml", description: "Source data")

Auf dieser Seite