Anotaciones
Añade, lista, mueve, elimina y aplana anotaciones de PDF, e importa o exporta XFDF, con NexPDF para iOS (Swift).
Módulo de licencia
Crear y cambiar anotaciones necesita el módulo annotations (nivel Indie y superiores). Listar las
anotaciones con annotationsJson(pageIndex:) funciona sin clave.
Las coordenadas son puntos PDF con el origen en la esquina inferior izquierda de la página. Los
rectángulos son llx, lly, urx, ury. El lector ofrece las mismas herramientas al
usuario en su modo de anotaciones.
Añadir marcado y formas
addAnnotation(pageIndex:kind:colorHex:opacity:contents:llx:lly:urx:ury:) añade una anotación de un
subtipo de PDF como Highlight, Underline, StrikeOut, Squiggly, Square, Circle, FreeText o
Text (una nota adhesiva). El marcado de texto se ajusta a las líneas que hay bajo el rectángulo.
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)Líneas, flechas y tinta
// 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")Una firma manuscrita es una anotación de tinta con varios trazos separados por |:
try doc.addInkSignature(pageIndex: 0, colorHex: "#111827", strokeWidth: 1.5,
strokes: "300,120;320,130;340,118|350,125;380,128")Esto es solo una firma visual. Para una firma criptográfica consulta Firmas.
Sellos de imagen y mediciones
// 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 es distance, perimeter o area. measurementsJson() lista las almacenadas.
Listar, editar, mover y eliminar
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)
}Las anotaciones se identifican por su número de objeto (num) y su generación (gen).
Aplanar
Aplanar incorpora las anotaciones al contenido de la página para que ya no se puedan editar:
try doc.flattenAnnotations(pageIndex: -1) // every page; pass a page index for one pageImportación y exportación de XFDF
let xfdf = try doc.exportXfdf()
let other = try NexPdfDocument.open(url: cleanURL)
try other.importXfdf(xfdf: xfdf)
try other.write(to: outputURL)Archivos adjuntos
addFileAttachmentAnnot(pageIndex:x:y:name:data:mime:description:) añade un icono de clip cuya esquina
superior izquierda está en (x, y):
try doc.addFileAttachmentAnnot(pageIndex: 0, x: 520, y: 760, name: "invoice.xml",
data: xmlData, mime: "application/xml", description: "Source data")Códigos de barras
Lee códigos QR, Data Matrix, PDF417 y códigos de barras lineales de páginas e imágenes de PDF, y dibuja códigos de barras vectoriales reales, con NexPDF para iOS (Swift).
Formularios
Lista, rellena, crea, recalcula y aplana campos de formulario PDF, e intercambia datos de formulario como JSON, FDF o XFDF, con NexPDF para iOS (Swift).