PdfiumPage

Struct PdfiumPage 

Source
pub struct PdfiumPage { /* private fields */ }
Expand description

§Rust interface to FPDF_PAGE

Implementations§

Source§

impl PdfiumPage

Source

pub fn render(&self, config: &PdfiumRenderConfig) -> PdfiumResult<PdfiumBitmap>

Renders this PdfiumPage into a new PdfiumBitmap using the specified configuration.

This method handles the complex logic of translating the configuration parameters into the appropriate PDFium rendering calls, including dimension calculations, transformation matrix setup, and bitmap creation.

§Arguments
  • config - The rendering configuration to use
§Returns
  • Ok(PdfiumBitmap) - The rendered bitmap on success
  • Err(PdfiumError) - An error describing what went wrong
§Errors

This method can return errors for:

  • Invalid configuration parameters
  • PDFium rendering failures
  • Memory allocation failures
  • Page boundary calculation errors
Examples found in repository?
examples/rotations.rs (line 51)
34pub fn example_rotations() -> PdfiumResult<()> {
35    // Load the PDF document from the specified file path
36    // Parameters:
37    // - "resources/groningen.pdf": Path to the PDF file (relative to current working directory)
38    // - None: No password required for this PDF (use Some("password") if needed)
39    let document = PdfiumDocument::new_from_path("resources/groningen.pdf", None)?;
40
41    let config = PdfiumRenderConfig::new().with_height(1080);
42
43    // Method 1: Rotate page before rendering
44
45    let page_1 = document.page(0)?;
46
47    assert!(!page_1.is_landscape());
48    assert!(page_1.is_portrait());
49
50    page_1.set_rotation(PdfiumRotation::Cw90);
51    let bitmap = page_1.render(&config)?;
52    bitmap.save("groningen-rotation-90-1.jpg", image::ImageFormat::Jpeg)?;
53
54    assert!(page_1.is_landscape());
55
56    page_1.set_rotation(PdfiumRotation::Cw180);
57    let bitmap = page_1.render(&config)?;
58    bitmap.save("groningen-rotation-180-1.jpg", image::ImageFormat::Jpeg)?;
59
60    page_1.set_rotation(PdfiumRotation::Cw270);
61    let bitmap = page_1.render(&config)?;
62    bitmap.save("groningen-rotation-270-1.jpg", image::ImageFormat::Jpeg)?;
63
64    // Method 2 (better): Use `with_rotation` when creating the `PdfiumRenderConfig`
65
66    let page_2 = document.page(1)?;
67
68    let bitmap = page_2.render(&config.clone().with_rotation(PdfiumRotation::Cw90))?;
69    bitmap.save("groningen-rotation-90-2.jpg", image::ImageFormat::Jpeg)?;
70
71    let bitmap = page_2.render(&config.clone().with_rotation(PdfiumRotation::Cw180))?;
72    bitmap.save("groningen-rotation-180-2.jpg", image::ImageFormat::Jpeg)?;
73
74    let bitmap = page_2.render(&config.clone().with_rotation(PdfiumRotation::Cw270))?;
75    bitmap.save("groningen-rotation-270-2.jpg", image::ImageFormat::Jpeg)?;
76
77    // Return success - all operations were successful
78    Ok(())
79}
More examples
Hide additional examples
examples/render_pages.rs (line 52)
34pub fn example_export_pages_to_images() -> PdfiumResult<()> {
35    // Load the PDF document from the specified file path
36    // Parameters:
37    // - "resources/groningen.pdf": Path to the PDF file (relative to current working directory)
38    // - None: No password required for this PDF (use Some("password") if needed)
39    let document = PdfiumDocument::new_from_path("resources/groningen.pdf", None)?;
40
41    // Iterate through all pages in the document
42    // document.pages() returns an iterator over all pages
43    // enumerate() adds an index counter starting from 0
44    // This gives us both the page object and its 0-based index
45    for (index, page) in document.pages().enumerate() {
46        // Render the current page as a bitmap image
47        // This is where the PDF content gets converted to a raster image
48        //
49        // In the configuration we only specify the height in pixels. The width will be calculated
50        // automatically to maintain aspect ratio.
51        let config = PdfiumRenderConfig::new().with_height(1080);
52        let bitmap = page?.render(&config)?;
53
54        // Verify that the bitmap was rendered at the requested height
55        // This assertion ensures the rendering process worked as expected
56        // If this fails, it indicates a bug in the rendering logic
57        assert_eq!(bitmap.height(), 1080);
58
59        // Generate a unique filename for this page
60        // Format: "groningen-page-{page_number}.jpg"
61        // - index + 1 converts from 0-based index to 1-based page numbers
62        //   * Page 0 becomes "groningen-page-1.jpg"
63        //   * Page 1 becomes "groningen-page-2.jpg", etc.
64        // - The .jpg extension indicates JPEG format will be used
65        let filename = format!("groningen-page-{}.jpg", index + 1);
66
67        // Save the rendered bitmap to disk as a JPEG image
68        // Parameters:
69        // - &filename: Reference to the generated filename string
70        // - image::ImageFormat::Jpeg: Specifies JPEG compression format
71        //   * Alternative format: Png (lossless)
72        //   * JPEG provides good compression but is lossy (some quality loss)
73        //
74        // The save operation handles:
75        // - Converting from BGRA format to JPEG-compatible format
76        // - Applying JPEG compression
77        // - Writing the file to disk
78        bitmap.save(&filename, image::ImageFormat::Jpeg)?;
79
80        // Note: No explicit cleanup needed - Rust's ownership system automatically
81        // deallocates the bitmap memory when it goes out of scope at the end of this iteration
82    }
83
84    // Return success - all pages have been successfully exported
85    Ok(())
86}
Source§

impl PdfiumPage

Source

pub fn boundaries(&self) -> PdfiumPageBoundaries<'_>

Rust interface to the boundary boxes of a page

Source

pub fn object_count(&self) -> i32

Get number of page objects inside this PdfiumPage.

Source

pub fn object(&self, index: i32) -> PdfiumResult<PdfiumPageObject>

Returns the PdfiumPageObject indicated by index from this PdfiumPage.

Source

pub fn objects(&self) -> PdfiumPageObjects<'_>

Return an Iterator for the ojects in this PdfiumPage.

Source

pub fn text(&self) -> PdfiumResult<PdfiumTextPage>

Get text page information structure

Contains information about all characters in a page.

Examples found in repository?
examples/text_extract_search.rs (line 36)
23pub fn example_extract_text() -> PdfiumResult<()> {
24    // Load the PDF document from the specified file path
25    // The second parameter (None) indicates no password is required
26    let document = PdfiumDocument::new_from_path("resources/chapter1.pdf", None)?;
27
28    // Iterate through all pages in the document
29    // enumerate() provides both the index and the page object
30    for (index, page) in document.pages().enumerate() {
31        // Extract the full text content from the current page
32        // The ?. operators handle potential errors at each step:
33        // - page? ensures the page loaded successfully
34        // - .text()? extracts text objects from the page
35        // - .full() gets the complete text content as a string
36        let text = page?.text()?.full();
37
38        // Print formatted output for each page
39        println!("Page {}", index + 1); // Pages are 1-indexed for user display
40        println!("------");
41        println!("{text}");
42        println!() // Empty line for separation between pages
43    }
44
45    // Expected output:
46    //
47    // Page 1
48    // ------
49    //
50    // Page 2
51    // ------
52    // Ruskin
53    // House.
54    // 156. Charing
55    // Cross Road.
56    // London
57    // George Allen.
58    //
59    // Page 3
60    // ------
61    //
62    // Page 4
63    // ------
64    // I
65    // Chapter I.
66    // T is a truth universally acknowledged, that a single man in possession of a good
67    // fortune must be in want of a wife.
68    // However little known the feelings or views of such a man may be on his first
69    // entering a neighbourhood, this truth is so well fixed in the minds of the surrounding
70    // families, that he is considered as the rightful property of some one or other of their
71    // daughters.
72    // “My dear Mr. Bennet,” said his lady to him one day, “have you heard that
73    // Netherfield Park is let at last?”
74    // ...
75
76    Ok(())
77}
78
79/// Demonstrates text search functionality within a PDF document
80pub fn example_search() -> PdfiumResult<()> {
81    // Load the PDF document to search within
82    let document = PdfiumDocument::new_from_path("resources/groningen.pdf", None)?;
83
84    // Get the first page (index 0) for searching
85    let page = document.page(0)?;
86
87    // Extract text objects from the page for searching
88    let text = page.text()?;
89
90    // Search for "amsterdam" with case-insensitive matching
91    // PdfiumSearchFlags::empty() means no special search flags (case-insensitive by default)
92    // The last parameter (0) is the starting position for the search
93    let search = text.find("amsterdam", PdfiumSearchFlags::empty(), 0);
94    println!("Found amsterdam {} times", search.count());
95
96    // Search for "groningen" with case-insensitive matching
97    let search = text.find("groningen", PdfiumSearchFlags::empty(), 0);
98    println!(
99        "Found groningen {} times (case insensitive)",
100        search.count()
101    );
102
103    // Search for "Groningen" with case-sensitive matching
104    // MATCH_CASE flag enforces exact case matching
105    let search = text.find("Groningen", PdfiumSearchFlags::MATCH_CASE, 0);
106    println!("Found Groningen {} times (case sensitive)", search.count());
107
108    // Perform another case-insensitive search to iterate through results
109    let search = text.find("groningen", PdfiumSearchFlags::empty(), 0);
110
111    // Iterate through each search result to extract detailed information
112    for result in search {
113        // Extract the text fragment at the found position
114        // result.index() gives the character position where the match starts
115        // result.count() gives the length of the matched text
116        let fragment = text.extract(result.index(), result.count());
117        println!(
118            "Found groningen (case insensitive) at {}, fragment = '{fragment}'",
119            result.index()
120        );
121    }
122
123    // Expected output:
124    //
125    // Found amsterdam 0 times
126    // Found groningen 5 times (case insensitive)
127    // Found Groningen 5 times (case sensitive)
128    // Found groningen (case insensitive) at 14, fragment = 'Groningen'
129    // Found groningen (case insensitive) at 232, fragment = 'Groningen'
130    // Found groningen (case insensitive) at 475, fragment = 'Groningen'
131    // Found groningen (case insensitive) at 920, fragment = 'Groningen'
132    // Found groningen (case insensitive) at 1050, fragment = 'Groningen'
133
134    Ok(())
135}
Source

pub fn rotation(&self) -> PdfiumRotation

Get the rotation of this PdfiumPage.

Returns a PdfiumRotation indicating the page rotation

Source

pub fn set_rotation(&self, rotate: PdfiumRotation)

Set rotation for this PdfiumPage.

rotate - the rotation value as PdfiumRotation

Examples found in repository?
examples/rotations.rs (line 50)
34pub fn example_rotations() -> PdfiumResult<()> {
35    // Load the PDF document from the specified file path
36    // Parameters:
37    // - "resources/groningen.pdf": Path to the PDF file (relative to current working directory)
38    // - None: No password required for this PDF (use Some("password") if needed)
39    let document = PdfiumDocument::new_from_path("resources/groningen.pdf", None)?;
40
41    let config = PdfiumRenderConfig::new().with_height(1080);
42
43    // Method 1: Rotate page before rendering
44
45    let page_1 = document.page(0)?;
46
47    assert!(!page_1.is_landscape());
48    assert!(page_1.is_portrait());
49
50    page_1.set_rotation(PdfiumRotation::Cw90);
51    let bitmap = page_1.render(&config)?;
52    bitmap.save("groningen-rotation-90-1.jpg", image::ImageFormat::Jpeg)?;
53
54    assert!(page_1.is_landscape());
55
56    page_1.set_rotation(PdfiumRotation::Cw180);
57    let bitmap = page_1.render(&config)?;
58    bitmap.save("groningen-rotation-180-1.jpg", image::ImageFormat::Jpeg)?;
59
60    page_1.set_rotation(PdfiumRotation::Cw270);
61    let bitmap = page_1.render(&config)?;
62    bitmap.save("groningen-rotation-270-1.jpg", image::ImageFormat::Jpeg)?;
63
64    // Method 2 (better): Use `with_rotation` when creating the `PdfiumRenderConfig`
65
66    let page_2 = document.page(1)?;
67
68    let bitmap = page_2.render(&config.clone().with_rotation(PdfiumRotation::Cw90))?;
69    bitmap.save("groningen-rotation-90-2.jpg", image::ImageFormat::Jpeg)?;
70
71    let bitmap = page_2.render(&config.clone().with_rotation(PdfiumRotation::Cw180))?;
72    bitmap.save("groningen-rotation-180-2.jpg", image::ImageFormat::Jpeg)?;
73
74    let bitmap = page_2.render(&config.clone().with_rotation(PdfiumRotation::Cw270))?;
75    bitmap.save("groningen-rotation-270-2.jpg", image::ImageFormat::Jpeg)?;
76
77    // Return success - all operations were successful
78    Ok(())
79}
Source

pub fn height(&self) -> f32

Get page height.

Return value:

  • Page height (excluding non-displayable area) measured in points. One point is 1/72 inch (around 0.3528 mm)

Comments:

  • Changing the rotation of |page| affects the return value.
Source

pub fn width(&self) -> f32

Get page width.

Return value:

  • Page width (excluding non-displayable area) measured in points. One point is 1/72 inch (around 0.3528 mm).

Comments:

  • Changing the rotation of |page| affects the return value.
Source

pub fn is_landscape(&self) -> bool

Check for landscape orientation.

Return value:

  • true if width is larger than height, otherwise false

Comments:

  • Square is neither landscape nor portrait
  • Changing the rotation of the page affects the return value.
Examples found in repository?
examples/rotations.rs (line 47)
34pub fn example_rotations() -> PdfiumResult<()> {
35    // Load the PDF document from the specified file path
36    // Parameters:
37    // - "resources/groningen.pdf": Path to the PDF file (relative to current working directory)
38    // - None: No password required for this PDF (use Some("password") if needed)
39    let document = PdfiumDocument::new_from_path("resources/groningen.pdf", None)?;
40
41    let config = PdfiumRenderConfig::new().with_height(1080);
42
43    // Method 1: Rotate page before rendering
44
45    let page_1 = document.page(0)?;
46
47    assert!(!page_1.is_landscape());
48    assert!(page_1.is_portrait());
49
50    page_1.set_rotation(PdfiumRotation::Cw90);
51    let bitmap = page_1.render(&config)?;
52    bitmap.save("groningen-rotation-90-1.jpg", image::ImageFormat::Jpeg)?;
53
54    assert!(page_1.is_landscape());
55
56    page_1.set_rotation(PdfiumRotation::Cw180);
57    let bitmap = page_1.render(&config)?;
58    bitmap.save("groningen-rotation-180-1.jpg", image::ImageFormat::Jpeg)?;
59
60    page_1.set_rotation(PdfiumRotation::Cw270);
61    let bitmap = page_1.render(&config)?;
62    bitmap.save("groningen-rotation-270-1.jpg", image::ImageFormat::Jpeg)?;
63
64    // Method 2 (better): Use `with_rotation` when creating the `PdfiumRenderConfig`
65
66    let page_2 = document.page(1)?;
67
68    let bitmap = page_2.render(&config.clone().with_rotation(PdfiumRotation::Cw90))?;
69    bitmap.save("groningen-rotation-90-2.jpg", image::ImageFormat::Jpeg)?;
70
71    let bitmap = page_2.render(&config.clone().with_rotation(PdfiumRotation::Cw180))?;
72    bitmap.save("groningen-rotation-180-2.jpg", image::ImageFormat::Jpeg)?;
73
74    let bitmap = page_2.render(&config.clone().with_rotation(PdfiumRotation::Cw270))?;
75    bitmap.save("groningen-rotation-270-2.jpg", image::ImageFormat::Jpeg)?;
76
77    // Return success - all operations were successful
78    Ok(())
79}
Source

pub fn is_portrait(&self) -> bool

Check for portrait orientation.

Return value:

  • true if width is smaller than height, otherwise false

Comments:

  • Square is neither landscape nor portrait
  • Changing the rotation of the page affects the return value.
Examples found in repository?
examples/rotations.rs (line 48)
34pub fn example_rotations() -> PdfiumResult<()> {
35    // Load the PDF document from the specified file path
36    // Parameters:
37    // - "resources/groningen.pdf": Path to the PDF file (relative to current working directory)
38    // - None: No password required for this PDF (use Some("password") if needed)
39    let document = PdfiumDocument::new_from_path("resources/groningen.pdf", None)?;
40
41    let config = PdfiumRenderConfig::new().with_height(1080);
42
43    // Method 1: Rotate page before rendering
44
45    let page_1 = document.page(0)?;
46
47    assert!(!page_1.is_landscape());
48    assert!(page_1.is_portrait());
49
50    page_1.set_rotation(PdfiumRotation::Cw90);
51    let bitmap = page_1.render(&config)?;
52    bitmap.save("groningen-rotation-90-1.jpg", image::ImageFormat::Jpeg)?;
53
54    assert!(page_1.is_landscape());
55
56    page_1.set_rotation(PdfiumRotation::Cw180);
57    let bitmap = page_1.render(&config)?;
58    bitmap.save("groningen-rotation-180-1.jpg", image::ImageFormat::Jpeg)?;
59
60    page_1.set_rotation(PdfiumRotation::Cw270);
61    let bitmap = page_1.render(&config)?;
62    bitmap.save("groningen-rotation-270-1.jpg", image::ImageFormat::Jpeg)?;
63
64    // Method 2 (better): Use `with_rotation` when creating the `PdfiumRenderConfig`
65
66    let page_2 = document.page(1)?;
67
68    let bitmap = page_2.render(&config.clone().with_rotation(PdfiumRotation::Cw90))?;
69    bitmap.save("groningen-rotation-90-2.jpg", image::ImageFormat::Jpeg)?;
70
71    let bitmap = page_2.render(&config.clone().with_rotation(PdfiumRotation::Cw180))?;
72    bitmap.save("groningen-rotation-180-2.jpg", image::ImageFormat::Jpeg)?;
73
74    let bitmap = page_2.render(&config.clone().with_rotation(PdfiumRotation::Cw270))?;
75    bitmap.save("groningen-rotation-270-2.jpg", image::ImageFormat::Jpeg)?;
76
77    // Return success - all operations were successful
78    Ok(())
79}
Source

pub fn has_transparency(&self) -> bool

Checks if this PdfiumPage contains transparency.

Returns true if this contains transparency.

Trait Implementations§

Source§

impl Clone for PdfiumPage

Source§

fn clone(&self) -> PdfiumPage

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PdfiumPage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&PdfiumPage> for FPDF_PAGE

Source§

fn from(page: &PdfiumPage) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.