How-to guides/Document Structure

Rotate one or all pages of a PDF in Rust

Set page rotation to 90, 180, or 270 degrees. Rotate a single page, a range, or the whole document in a few lines of Rust.

rust
use pdfluent::{PdfDocument, Rotation};

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

    // Rotate all pages 90 degrees clockwise
    for i in 0..doc.page_count() {
        doc.page_mut(i)?.set_rotation(Rotation::Clockwise90);
    }

    doc.save("scan_rotated.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 PDF

Load the file you want to rotate.

rust
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open("scanned_contract.pdf")?;
3

Rotate a single page

Get a mutable reference to the page and call set_rotation(). The Rotation enum has variants for 0, 90, 180, and 270 degrees.

rust
use pdfluent::Rotation;

// Rotate page 3 (index 2) 90 degrees clockwise
doc.page_mut(2)?.set_rotation(Rotation::Clockwise90);

// Rotate page 4 upside-down
doc.page_mut(3)?.set_rotation(Rotation::Degrees180);
4

Rotate a page range

Loop over a range of page indices to rotate a contiguous section.

rust
// Rotate pages 5 through 8 (indices 4-7)
for i in 4..8 {
    doc.page_mut(i)?.set_rotation(Rotation::CounterClockwise90);
}
5

Rotate all pages and save

Iterate over all pages and apply the same rotation, then write the output.

rust
let count = doc.page_count();
for i in 0..count {
    doc.page_mut(i)?.set_rotation(Rotation::Clockwise90);
}

doc.save("document_rotated.pdf")?;
println!("Rotated {} pages", count);

Notes and tips

  • set_rotation() sets the /Rotate entry in the page dictionary. This is the standard PDF rotation mechanism.
  • Rotation is additive if you call it multiple times on the same page. Use Rotation::None to reset to zero degrees.
  • Page content streams are not modified. The rotation is stored as metadata and applied by the PDF viewer.
  • To get the current rotation before changing it, call page.rotation() which returns a Rotation value.

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