Skip to main content

Page

Struct Page 

Source
pub struct Page<'a> { /* private fields */ }
Expand description

A single page of a PDF document.

Implementations§

Source§

impl<'a> Page<'a>

Source

pub fn index(&self) -> usize

0-based page index.

Source

pub fn width(&self) -> f64

Page width in points (1 point = 1/72 inch).

Examples found in repository?
examples/render_page.rs (line 20)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let args: Vec<String> = std::env::args().collect();
9    if args.len() < 2 {
10        eprintln!("Usage: render_page <pdf-file> [page] [dpi] [output.png]");
11        std::process::exit(1);
12    }
13
14    let doc = Document::open(&args[1])?;
15    let page_idx: usize = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(0);
16    let dpi: f64 = args.get(3).and_then(|s| s.parse().ok()).unwrap_or(150.0);
17    let output = args.get(4).map(|s| s.as_str()).unwrap_or("output.png");
18
19    let page = doc.page(page_idx)?;
20    println!("Page {}: {:.0}x{:.0} pt, rotation {}", page_idx + 1, page.width(), page.height(), page.rotation());
21
22    let png = page.render_png(dpi)?;
23    std::fs::write(output, &png)?;
24    println!("Rendered to {output} ({} bytes, {dpi} DPI)", png.len());
25
26    Ok(())
27}
Source

pub fn height(&self) -> f64

Page height in points.

Examples found in repository?
examples/render_page.rs (line 20)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let args: Vec<String> = std::env::args().collect();
9    if args.len() < 2 {
10        eprintln!("Usage: render_page <pdf-file> [page] [dpi] [output.png]");
11        std::process::exit(1);
12    }
13
14    let doc = Document::open(&args[1])?;
15    let page_idx: usize = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(0);
16    let dpi: f64 = args.get(3).and_then(|s| s.parse().ok()).unwrap_or(150.0);
17    let output = args.get(4).map(|s| s.as_str()).unwrap_or("output.png");
18
19    let page = doc.page(page_idx)?;
20    println!("Page {}: {:.0}x{:.0} pt, rotation {}", page_idx + 1, page.width(), page.height(), page.rotation());
21
22    let png = page.render_png(dpi)?;
23    std::fs::write(output, &png)?;
24    println!("Rendered to {output} ({} bytes, {dpi} DPI)", png.len());
25
26    Ok(())
27}
Source

pub fn rotation(&self) -> i64

Page rotation in degrees (0, 90, 180, 270).

Examples found in repository?
examples/render_page.rs (line 20)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let args: Vec<String> = std::env::args().collect();
9    if args.len() < 2 {
10        eprintln!("Usage: render_page <pdf-file> [page] [dpi] [output.png]");
11        std::process::exit(1);
12    }
13
14    let doc = Document::open(&args[1])?;
15    let page_idx: usize = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(0);
16    let dpi: f64 = args.get(3).and_then(|s| s.parse().ok()).unwrap_or(150.0);
17    let output = args.get(4).map(|s| s.as_str()).unwrap_or("output.png");
18
19    let page = doc.page(page_idx)?;
20    println!("Page {}: {:.0}x{:.0} pt, rotation {}", page_idx + 1, page.width(), page.height(), page.rotation());
21
22    let png = page.render_png(dpi)?;
23    std::fs::write(output, &png)?;
24    println!("Rendered to {output} ({} bytes, {dpi} DPI)", png.len());
25
26    Ok(())
27}
Source

pub fn media_box(&self) -> Rect

MediaBox rectangle.

Source

pub fn crop_box(&self) -> Rect

CropBox rectangle (falls back to MediaBox).

Source

pub fn text(&self) -> Result<String>

Extract text from this page as a plain string.

Examples found in repository?
examples/basic_read.rs (line 30)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let args: Vec<String> = std::env::args().collect();
9    if args.len() < 2 {
10        eprintln!("Usage: basic_read <pdf-file>");
11        std::process::exit(1);
12    }
13
14    let doc = Document::open(&args[1])?;
15
16    println!("Version: {}", doc.version_string());
17    println!("Pages: {}", doc.page_count());
18    println!("Encrypted: {}", doc.is_encrypted());
19
20    if let Some(title) = doc.title() {
21        println!("Title: {title}");
22    }
23    if let Some(author) = doc.author() {
24        println!("Author: {author}");
25    }
26
27    // Extract text from first page
28    if let Ok(page) = doc.page(0) {
29        println!("\n--- Page 1 text ---");
30        match page.text() {
31            Ok(text) => println!("{}", &text[..text.len().min(500)]),
32            Err(e) => println!("(text extraction failed: {e})"),
33        }
34    }
35
36    Ok(())
37}
Source

pub fn text_structured(&self) -> Result<PageText>

Extract structured text (with positions, fonts, etc.).

Source

pub fn render_png(&self, dpi: f64) -> Result<Vec<u8>>

Render to PNG at the given DPI.

Examples found in repository?
examples/render_page.rs (line 22)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let args: Vec<String> = std::env::args().collect();
9    if args.len() < 2 {
10        eprintln!("Usage: render_page <pdf-file> [page] [dpi] [output.png]");
11        std::process::exit(1);
12    }
13
14    let doc = Document::open(&args[1])?;
15    let page_idx: usize = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(0);
16    let dpi: f64 = args.get(3).and_then(|s| s.parse().ok()).unwrap_or(150.0);
17    let output = args.get(4).map(|s| s.as_str()).unwrap_or("output.png");
18
19    let page = doc.page(page_idx)?;
20    println!("Page {}: {:.0}x{:.0} pt, rotation {}", page_idx + 1, page.width(), page.height(), page.rotation());
21
22    let png = page.render_png(dpi)?;
23    std::fs::write(output, &png)?;
24    println!("Rendered to {output} ({} bytes, {dpi} DPI)", png.len());
25
26    Ok(())
27}
Source

pub fn render_jpeg(&self, dpi: f64, quality: u8) -> Result<Vec<u8>>

Render to JPEG at the given DPI and quality (0-100).

Source

pub fn render_svg(&self) -> Result<String>

Render to SVG.

Source

pub fn render_raw(&self, dpi: f64) -> Result<RenderedPixmap>

Render to raw RGBA pixel data.

Source

pub fn render(&self, options: &RenderOptions) -> Result<Vec<u8>>

Render with custom options.

Source

pub fn render_to_file(&self, path: impl AsRef<Path>, dpi: f64) -> Result<()>

Render to a file (PNG format).

Source

pub fn search(&self, query: &str) -> Result<Vec<SearchResult>>

Search for text on this page.

Source

pub fn search_case_insensitive(&self, query: &str) -> Result<Vec<SearchResult>>

Search for text on this page (case-insensitive).

Source

pub fn info(&self) -> &PageInfo

Get the underlying PageInfo for low-level access.

Auto Trait Implementations§

§

impl<'a> Freeze for Page<'a>

§

impl<'a> RefUnwindSafe for Page<'a>

§

impl<'a> Send for Page<'a>

§

impl<'a> Sync for Page<'a>

§

impl<'a> Unpin for Page<'a>

§

impl<'a> UnsafeUnpin for Page<'a>

§

impl<'a> UnwindSafe for Page<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V