Annotations
Add, list, move, remove and flatten PDF annotations, and import or export XFDF, with NexPDF for iOS (Swift).
License module
Creating and changing annotations needs the annotations module (Indie tier and above). Listing
annotations with annotationsJson(pageIndex:) works without a key.
Coordinates are PDF points with the origin at the bottom-left of the page. Rectangles are
llx, lly, urx, ury. The reader offers the same tools to the user in its
annotations mode.
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.
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)Lines, arrows and ink
// 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")A handwritten signature is an ink annotation with several strokes separated by |:
try doc.addInkSignature(pageIndex: 0, colorHex: "#111827", strokeWidth: 1.5,
strokes: "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.
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 is distance, perimeter or area. measurementsJson() lists the stored ones.
List, edit, move and remove
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)
}Annotations are identified by their object number (num) and generation (gen).
Flatten
Flattening burns annotations into the page content so they can no longer be edited:
try doc.flattenAnnotations(pageIndex: -1) // every page; pass a page index for one pageXFDF import and export
let xfdf = try doc.exportXfdf()
let other = try NexPdfDocument.open(url: cleanURL)
try other.importXfdf(xfdf: xfdf)
try other.write(to: outputURL)File attachments
addFileAttachmentAnnot(pageIndex:x:y:name:data:mime:description:) adds a paperclip icon whose
top-left corner is at (x, y):
try doc.addFileAttachmentAnnot(pageIndex: 0, x: 520, y: 760, name: "invoice.xml",
data: xmlData, mime: "application/xml", description: "Source data")