pub struct Document { /* private fields */ }Expand description
A high-level PDF document handle.
Provides ergonomic access to pages, text, rendering, metadata, annotations, forms, signatures, and modification.
Implementations§
Source§impl Document
impl Document
Sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
Open a PDF file from a path.
Examples found in repository?
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}More examples
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}Sourcepub fn from_bytes(data: Vec<u8>) -> Result<Self>
pub fn from_bytes(data: Vec<u8>) -> Result<Self>
Parse a PDF from in-memory bytes.
Sourcepub fn authenticate(&mut self, password: &[u8]) -> Result<()>
pub fn authenticate(&mut self, password: &[u8]) -> Result<()>
Authenticate an encrypted document with a password.
Sourcepub fn page_count(&self) -> usize
pub fn page_count(&self) -> usize
Number of pages in the document.
Examples found in repository?
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}Sourcepub fn page(&self, index: usize) -> Result<Page<'_>>
pub fn page(&self, index: usize) -> Result<Page<'_>>
Get a page by 0-based index.
Examples found in repository?
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}More examples
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}Sourcepub fn version_string(&self) -> String
pub fn version_string(&self) -> String
PDF version as a string (e.g. “1.7”).
Examples found in repository?
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}Sourcepub fn is_encrypted(&self) -> bool
pub fn is_encrypted(&self) -> bool
Whether the document is encrypted.
Examples found in repository?
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}Sourcepub fn is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Whether the document is authenticated (or not encrypted).
Sourcepub fn is_linearized(&self) -> bool
pub fn is_linearized(&self) -> bool
Whether the document is linearized (web-optimized).
Sourcepub fn title(&self) -> Option<String>
pub fn title(&self) -> Option<String>
Get document title from metadata.
Examples found in repository?
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}Get document author from metadata.
Examples found in repository?
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}Sourcepub fn creation_date(&self) -> Option<String>
pub fn creation_date(&self) -> Option<String>
Get creation date string from metadata.
Sourcepub fn modification_date(&self) -> Option<String>
pub fn modification_date(&self) -> Option<String>
Get modification date string from metadata.
Sourcepub fn search(&self, query: &str) -> Result<Vec<(usize, Vec<SearchResult>)>>
pub fn search(&self, query: &str) -> Result<Vec<(usize, Vec<SearchResult>)>>
Search for text across all pages. Returns (page_index, matches) pairs.
Sourcepub fn outlines(&self) -> Result<Vec<OutlineItem>>
pub fn outlines(&self) -> Result<Vec<OutlineItem>>
Get bookmarks/outlines.
Sourcepub fn page_labels(&self) -> Result<Vec<PageLabelRange>>
pub fn page_labels(&self) -> Result<Vec<PageLabelRange>>
Get page labels.
Sourcepub fn annotations(&self, page_index: usize) -> Result<Vec<Annotation>>
pub fn annotations(&self, page_index: usize) -> Result<Vec<Annotation>>
Get annotations for a specific page.
Sourcepub fn form_fields(&self) -> Result<Option<AcroForm>>
pub fn form_fields(&self) -> Result<Option<AcroForm>>
Get form fields (if any).
Sourcepub fn embedded_files(&self) -> Result<Vec<FileSpec>>
pub fn embedded_files(&self) -> Result<Vec<FileSpec>>
Get embedded files.
Sourcepub fn signatures(&self) -> Result<Vec<SignatureInfo>>
pub fn signatures(&self) -> Result<Vec<SignatureInfo>>
Get digital signature information.
Sourcepub fn modify(&self) -> Result<Modifier>
pub fn modify(&self) -> Result<Modifier>
Create a modifier for editing this document.
The modifier works on a copy of the raw PDF bytes, so the original
Document is not affected.
Sourcepub fn inner(&self) -> &PdfDocument
pub fn inner(&self) -> &PdfDocument
Get the underlying PdfDocument for low-level access.
Sourcepub fn inner_mut(&mut self) -> &mut PdfDocument
pub fn inner_mut(&mut self) -> &mut PdfDocument
Get a mutable reference to the underlying PdfDocument.
Sourcepub fn into_inner(self) -> PdfDocument
pub fn into_inner(self) -> PdfDocument
Consume this Document and return the underlying PdfDocument.
Auto Trait Implementations§
impl !Freeze for Document
impl RefUnwindSafe for Document
impl Send for Document
impl Sync for Document
impl Unpin for Document
impl UnsafeUnpin for Document
impl UnwindSafe for Document
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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