rotations/rotations.rs
1// PDFium-rs -- Modern Rust interface to PDFium, the PDF library from Google
2//
3// Copyright (c) 2025 Martin van der Werff <github (at) newinnovations.nl>
4//
5// This file is part of PDFium-rs.
6//
7// PDFium-rs is free software: you can redistribute it and/or modify it under the terms of
8// the GNU General Public License as published by the Free Software Foundation, either version 3
9// of the License, or (at your option) any later version.
10//
11// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
12// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
13// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
14// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
15// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
16// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
17// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20// Import all necessary items from the pdfium crate
21// This provides access to PDF document handling, rendering, and bitmap operations
22use pdfium::*;
23
24/// Demonstrate rotation operations.
25///
26/// This example demonstrates how to:
27/// - Load a PDF document from disk
28/// - Rotating a page
29/// - Rendering with rotation
30/// - Save each rendered page as an image file
31///
32/// The example uses proper error handling with Result types, allowing
33/// errors to propagate up the call stack rather than panicking.
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}
80
81fn main() -> PdfiumResult<()> {
82 example_rotations()?;
83 Ok(())
84}