Annotations
Add, list, move, remove and flatten PDF annotations, and import or export XFDF, with NexPDF for Rust.
License module
Creating and changing annotations needs the annotations module (Indie tier and above). Listing
annotations with annotations_json works without a key.
Coordinates are PDF points (f64) with the origin at the bottom-left of the page. Rectangles are
llx, lly, urx, ury.
Add markup and shapes
add_annotation(page_index, kind, color_hex, 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.
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")
}Lines, arrows and ink
// 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")?;A handwritten signature is an ink annotation with several strokes separated by |:
doc.add_ink_signature(0, "#111827", 1.5, "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
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 is distance, perimeter or area. doc.measurements_json() lists the stored ones.
List, edit, move and remove
annotations_json(page_index) lists the annotations of a page. With the serde feature,
annotations_json_value parses the list:
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)?;Annotations are identified by their object number (num, a u32) and generation (gen, a
u16).
Flatten
Flattening burns annotations into the page content so they can no longer be edited:
doc.flatten_annotations(-1)?; // every page; pass a page index for one pageXFDF import and export
let xfdf = doc.export_xfdf()?;
let mut other = Document::open("clean.pdf")?;
other.import_xfdf(&xfdf)?;
other.save("with-annotations.pdf")?;File attachments
add_file_attachment_annot(page_index, x, y, name, data, mime, description) adds a paperclip icon
whose top-left corner is at (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")?;