Create clickable link annotations that open URLs or jump to other pages within the same document.
use pdfluent::{Document, Rect, annotation::{LinkAnnotation, LinkTarget}};
fn main() -> pdfluent::Result<()> {
let mut doc = Document::open("input.pdf")?;
// Add a URL link on the first page
let rect = Rect::new(72.0, 700.0, 250.0, 720.0);
let link = LinkAnnotation::url(rect, "https://pdfluent.com");
doc.page_mut(0)?.add_annotation(link);
doc.save("with_links.pdf")?;
Ok(())
}Annotations are stored per-page. Open the file with a mutable Document.
let mut doc = Document::open("input.pdf")?;A link annotation needs a bounding box (the clickable area) in page coordinates (points, origin at bottom-left).
use pdfluent::Rect;
// Clickable area: x1=72, y1=700, x2=250, y2=720
let rect = Rect::new(72.0, 700.0, 250.0, 720.0);LinkAnnotation::url builds a URI action annotation. The URL must be a valid absolute URI.
use pdfluent::annotation::{LinkAnnotation, LinkBorder};
let link = LinkAnnotation::url(rect, "https://example.com")
.border(LinkBorder::none()); // hide the default blue borderUse LinkTarget::Page to link to a specific page number within the same document. Page numbers are zero-based.
use pdfluent::annotation::LinkTarget;
let dest_rect = Rect::new(72.0, 600.0, 300.0, 620.0);
let internal_link = LinkAnnotation::destination(
dest_rect,
LinkTarget::page(4, None), // jump to page 5 (0-indexed = 4)
);Call add_annotation for each link. Multiple annotations can be added to the same page.
let page = doc.page_mut(0)?;
page.add_annotation(link);
page.add_annotation(internal_link);
doc.save("with_links.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.