Aller au contenu
NexPDF

Annotations

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

Français

Module de licence

La création et la modification d'annotations nécessitent le module annotations (offre Indie et supérieures). Lister les annotations avec annotationsJson(pageIndex:) fonctionne sans clé.

Les coordonnées sont en points PDF, avec l'origine en bas à gauche de la page. Les rectangles sont llx, lly, urx, ury. Le lecteur propose les mêmes outils à l'utilisateur dans son mode annotations.

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 adhésive). Le marquage de texte est ajusté aux lignes situées sous le rectangle.

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)

Lignes, flèches et encre

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

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

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

C'est uniquement une signature visuelle. Pour une signature cryptographique, voir Signatures.

Tampons d'image et mesures

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

Lister, modifier, déplacer et supprimer

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

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

Aplatir

L'aplatissement fusionne les annotations dans le contenu de la page : elles ne peuvent plus être modifiées :

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

Import et export XFDF

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

Pièces jointes

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

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

Sur cette page