Anotaciones
Añade, lista, mueve, elimina y aplana anotaciones PDF, e importa o exporta XFDF, con NexPDF para Java.
Módulo de licencia
Crear y modificar anotaciones necesita el módulo annotations (nivel Indie y superiores). Listar
las anotaciones con annotationsJson 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.
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.
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"));
}Líneas, flechas y tinta
// 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");Una firma manuscrita es una anotación de tinta con varios trazos separados por |:
doc.addInkSignature(0, "#111827", 1.5, "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.
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 es distance, perimeter o area. doc.measurementsJson() lista las almacenadas.
Listar, editar, mover y eliminar
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);Las anotaciones se identifican por su número de objeto (num) y su generación (gen, un short).
Aplanar
Aplanar graba las anotaciones en el contenido de la página, de modo que ya no se pueden editar:
doc.flattenAnnotations(-1); // every page; pass a page index for one pageImportación y exportación de XFDF
String xfdf = doc.exportXfdf();
try (NexPdfDocument other = NexPdfDocument.open(Path.of("clean.pdf"))) {
other.importXfdf(xfdf);
other.save(Path.of("with-annotations.pdf"));
}Archivos adjuntos
addFileAttachmentAnnot(pageIndex, x, y, name, data, mime, description) añade un icono de clip cuya
esquina superior izquierda está en (x, y):
byte[] xml = Files.readAllBytes(Path.of("invoice.xml"));
doc.addFileAttachmentAnnot(0, 520, 760, "invoice.xml", xml, "application/xml", "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 Java.
Formularios
Lista, rellena, crea, recalcula y aplana campos de formulario PDF, e intercambia datos de formulario como JSON, FDF o XFDF, con NexPDF para Java.