iterate/iterate.rs
1//! This example shows how you can iterate over the content stream of all pages in a PDF.
2
3use hayro_syntax::Pdf;
4use std::path::PathBuf;
5use std::sync::Arc;
6
7fn main() {
8 // First load the data that constitutes the PDF file.
9 let data = std::fs::read(
10 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../hayro-tests/pdfs/text_with_rise.pdf"),
11 )
12 .unwrap();
13
14 // Then create a new PDF file from it.
15 //
16 // Here we are just unwrapping in case reading the file failed, but you
17 // might instead want to apply proper error handling.
18 let pdf = Pdf::new(Arc::new(data)).unwrap();
19
20 // First access all pages, and then iterate over the operators of each page's
21 // content stream and print them.
22 let pages = pdf.pages();
23 for page in pages.iter() {
24 for op in page.typed_operations() {
25 println!("{op:?}");
26 }
27 }
28}