How-to guides/Document Info

Get page width, height, and media box in Rust

Read the MediaBox, CropBox, and BleedBox from any PDF page. Convert between points and millimetres or inches for printing and layout workflows.

rust
use pdfluent::PdfDocument;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let doc = PdfDocument::open("drawing.pdf")?;

    for (i, page) in doc.pages().enumerate() {
        let size = page.media_box();
        println!(
            "Page {}: {:.1} x {:.1} pt  ({:.1} x {:.1} mm)",
            i + 1,
            size.width(),
            size.height(),
            size.width() * 25.4 / 72.0,
            size.height() * 25.4 / 72.0,
        );
    }
    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 PDF and access pages

Open the document and iterate or index pages directly.

rust
use pdfluent::PdfDocument;

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

Read the MediaBox

The MediaBox defines the full physical extent of the page in PDF user units (points). 1 point = 1/72 inch.

rust
let media = page.media_box();
println!("Width:  {:.2} pt", media.width());
println!("Height: {:.2} pt", media.height());
println!("Origin: ({:.2}, {:.2})", media.x1, media.y1);
4

Read CropBox and BleedBox

CropBox is the visible page area. BleedBox is for print bleed. Both fall back to the MediaBox if not set.

rust
let crop = page.crop_box();
println!("CropBox: {:.1} x {:.1} pt", crop.width(), crop.height());

let bleed = page.bleed_box();
println!("BleedBox: {:.1} x {:.1} pt", bleed.width(), bleed.height());
5

Convert to millimetres and inches

PDF points convert to mm with factor 25.4/72 and to inches with factor 1/72.

rust
fn pt_to_mm(pt: f32) -> f32 { pt * 25.4 / 72.0 }
fn pt_to_inch(pt: f32) -> f32 { pt / 72.0 }

let media = page.media_box();
println!(
    "Page size: {:.1} x {:.1} mm  ({:.3} x {:.3} in)",
    pt_to_mm(media.width()),
    pt_to_mm(media.height()),
    pt_to_inch(media.width()),
    pt_to_inch(media.height()),
);

Notes and tips

  • PDF units are always points (1/72 inch). There is no concept of DPI at the page dimension level.
  • A standard A4 page is 595 x 842 pt. Letter is 612 x 792 pt.
  • Pages in the same document can have different sizes. Always read dimensions per-page, not once for the document.
  • Rotate() entry affects display orientation. Use page.rotation() to read it and adjust width/height for display purposes.

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