Ir al contenido
NexPDF

Anotaciones

Añade, enumera, mueve, elimina y aplana anotaciones PDF, e importa o exporta XFDF, con NexPDF para .NET.

Español

Módulo de licencia

Crear y modificar anotaciones necesita el módulo annotations (nivel Indie y superiores). Enumerar 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. Para convertir un clic sobre una página renderizada, usa NexPdfPageView.

Añadir marcado y formas

AddAnnotation(pageIndex, kind, colorHex, opacity, contents, llx, lly, urx, ury) añade una anotación de un subtipo 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.

using var doc = NexPdfDocument.Open("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("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");

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.
var m = """{"kind":"distance","points":[[100,100],[300,100]],"unit":"mm","per_point":0.3528}""";
var result = JsonNode.Parse(doc.AddMeasurement(0, m))!;
Console.WriteLine(result["text"]);

kind es distance, perimeter o area. doc.MeasurementsJson() enumera las almacenadas.

Enumerar, editar, mover y eliminar

using System.Text.Json.Nodes;

foreach (var a in JsonNode.Parse(doc.AnnotationsJson(0))!.AsArray())
    Console.WriteLine($"{a!["num"]} {a["gen"]} {a["kind"]} {a["contents"]}");

var first = JsonNode.Parse(doc.AnnotationsJson(0))!.AsArray()[0]!;
var num = first["num"]!.GetValue<uint>();
var gen = first["gen"]!.GetValue<ushort>();
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, un uint) y su generación (gen, un ushort).

Aplanar

Aplanar incrusta las anotaciones en el contenido de la página para que ya no se puedan editar:

doc.FlattenAnnotations(-1);   // every page; pass a page index for one page

Importación y exportación de XFDF

var xfdf = doc.ExportXfdf();
using var other = NexPdfDocument.Open("clean.pdf");
other.ImportXfdf(xfdf);
other.Save("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):

doc.AddFileAttachmentAnnot(0, 520, 760, "invoice.xml", File.ReadAllBytes("invoice.xml"), "application/xml", "Source data");

En esta página