Add sticky notes, text highlights, underlines, and rubber-stamp annotations to PDF pages. All annotation types conform to the PDF 1.7 spec.
use pdfluent::{PdfDocument, Annotation, Color, Rect};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut doc = PdfDocument::open("report.pdf")?;
let page = doc.page_mut(0)?;
page.add_annotation(
Annotation::highlight(Rect::new(72.0, 680.0, 350.0, 695.0))
.color(Color::yellow())
.author("Jasper")
.contents("Check this figure"),
)?;
doc.save("report_annotated.pdf")?;
Ok(())
}Add the pdfluent crate to Cargo.toml.
[dependencies]
pdfluent = "0.9"Open the PDF and borrow a mutable reference to the page you want to annotate. Page indices are zero-based.
use pdfluent::PdfDocument;
let mut doc = PdfDocument::open("draft.pdf")?;
let page = doc.page_mut(0)?; // first pageA text annotation appears as a small icon on the page. Viewers show the contents as a pop-up note.
use pdfluent::{Annotation, Color, Point};
page.add_annotation(
Annotation::text(Point::new(100.0, 750.0))
.contents("Needs legal review before publishing.")
.author("Jasper")
.color(Color::rgb(1.0, 0.8, 0.0)), // yellow icon
)?;Highlight annotations draw a translucent colour over a text region. Supply the bounding rectangle in PDF points (72 points per inch, origin at bottom-left).
use pdfluent::{Annotation, Color, Rect};
page.add_annotation(
Annotation::highlight(Rect::new(72.0, 640.0, 400.0, 655.0))
.color(Color::rgb(1.0, 1.0, 0.0))
.contents("Revenue figure confirmed"),
)?;Stamp annotations display predefined labels such as "DRAFT", "APPROVED", or "CONFIDENTIAL" over the page.
use pdfluent::{Annotation, Rect, StampStyle};
page.add_annotation(
Annotation::stamp(Rect::new(400.0, 700.0, 550.0, 740.0))
.style(StampStyle::Approved)
.author("Manager"),
)?;
doc.save("draft_annotated.pdf")?;No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.
Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.
Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.