Ir al contenido
NexPDF

Anotaciones

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

Español

Módulo de licencia

Crear y modificar anotaciones necesita el módulo annotations (nivel Indie y superiores). Enumerar las anotaciones con annotations_json funciona sin clave.

Las coordenadas son puntos PDF (f64) con el origen en la esquina inferior izquierda de la página. Los rectángulos son llx, lly, urx, ury.

Añadir marcas y formas

add_annotation(page_index, kind, color_hex, 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). Las marcas de texto se ajustan a las líneas que hay bajo el rectángulo.

use nexpdf_sdk::Document;

fn main() -> nexpdf_sdk::Result<()> {
    let mut doc = Document::open("contract.pdf")?;
    doc.add_annotation(0, "Highlight", "#FFD400", 1.0, "", 72.0, 700.0, 300.0, 716.0)?;
    doc.add_annotation(0, "FreeText", "#E5484D", 1.0, "Checked", 72.0, 640.0, 260.0, 660.0)?;
    doc.add_annotation(0, "Text", "#2563EB", 1.0, "Please confirm this date", 320.0, 700.0, 340.0, 720.0)?;
    doc.save("annotated.pdf")
}

Líneas, flechas y tinta

// A red arrow from (100, 500) to (250, 560), 2 pt wide.
doc.add_line(0, "Arrow", "#DC2626", 1.0, 2.0, 100.0, 500.0, 250.0, 560.0)?;

// Freehand ink: points "x,y;x,y;..." in PDF points.
doc.add_ink(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.add_ink_signature(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

use serde_json::json;

// A picture stamp from RGBA pixels (width x height x 4 bytes), here 240 x 120.
doc.add_image_stamp(0, 400.0, 60.0, 520.0, 120.0, &rgba_bytes, 240, 120, 1.0)?;

// A stored distance measurement; per_point converts PDF points to the unit.
let m = json!({"kind": "distance", "points": [[100, 100], [300, 100]], "unit": "mm", "per_point": 0.3528});
let result = doc.add_measurement(0, &m.to_string())?;
println!("{result}");                     // {"num","gen","value","text"}

kind es distance, perimeter o area. doc.measurements_json() enumera las que están guardadas.

Enumerar, editar, mover y eliminar

annotations_json(page_index) enumera las anotaciones de una página. Con la característica serde, annotations_json_value analiza la lista:

let list = doc.annotations_json_value(0)?;
for a in list.as_array().into_iter().flatten() {
    println!("{} {} {} {}", a["num"], a["gen"], a["kind"], a["contents"]);
}

let a = &list[0];
let num = a["num"].as_u64().unwrap_or(0) as u32;
let generation = a["gen"].as_u64().unwrap_or(0) as u16;
doc.set_annotation_contents(num, generation, "Updated note")?;
doc.move_annotation(num, generation, 10.0, -20.0)?;     // dx, dy in points
doc.remove_annotation(0, num, generation)?;

Las anotaciones se identifican por su número de objeto (num, un u32) y su generación (gen, un u16).

Aplanar

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

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

Importación y exportación de XFDF

let xfdf = doc.export_xfdf()?;
let mut other = Document::open("clean.pdf")?;
other.import_xfdf(&xfdf)?;
other.save("with-annotations.pdf")?;

Archivos adjuntos

add_file_attachment_annot(page_index, x, y, name, data, mime, description) añade un icono de clip cuya esquina superior izquierda está en (x, y):

let xml = std::fs::read("invoice.xml")?;
doc.add_file_attachment_annot(0, 520.0, 760.0, "invoice.xml", &xml, "application/xml", "Source data")?;

En esta página