How-to guides/Annotations

Add highlight, comment, and stamp annotations in Rust

Add sticky notes, text highlights, underlines, and rubber-stamp annotations to PDF pages. All annotation types conform to the PDF 1.7 spec.

rust
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(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Add PDFluent to your project

Add the pdfluent crate to Cargo.toml.

rust
[dependencies]
pdfluent = "0.9"
2

Open the document and get a mutable page

Open the PDF and borrow a mutable reference to the page you want to annotate. Page indices are zero-based.

rust
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open("draft.pdf")?;
let page = doc.page_mut(0)?; // first page
3

Add a sticky note (text) annotation

A text annotation appears as a small icon on the page. Viewers show the contents as a pop-up note.

rust
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
)?;
4

Add a highlight annotation

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).

rust
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"),
)?;
5

Add a stamp annotation and save

Stamp annotations display predefined labels such as "DRAFT", "APPROVED", or "CONFIDENTIAL" over the page.

rust
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")?;

Notes and tips

  • PDF coordinates use points (1/72 inch). Page height on A4 at 72 DPI is 841.9 points. Y=0 is the bottom of the page.
  • Annotation colours are set per annotation. The Color struct accepts RGB values in the 0.0 to 1.0 range.
  • Some PDF viewers display annotation author names in the review pane. Use .author() to set a meaningful value.
  • To add multiple annotations on the same page, call add_annotation() multiple times before saving.

Why PDFluent for this

Pure Rust

No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.

Memory safe

Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.

Runs anywhere

Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.

Frequently asked questions