Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text and image examples #78

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions examples/extract_images.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::io::Write;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let filename: String = std::env::args()
.collect::<Vec<_>>()
.get(1)
.expect("missing filename")
.to_owned();
let document = mupdf::document::Document::open(&filename)?;

let mut image_num: u32 = 0;

for page in document.pages()? {
let text_page = page?.to_text_page(mupdf::text_page::TextPageOptions::PRESERVE_IMAGES)?;

for block in text_page.blocks() {
if let Some(image) = block.image() {
let pixmap = image.to_pixmap()?;
let mut bytes: Vec<u8> = vec![];
pixmap.write_to(&mut bytes, mupdf::pixmap::ImageFormat::PNG)?;

let mut output_file = std::fs::File::create(format!("output_{}.png", image_num))?;
output_file.write_all(&bytes)?;

image_num += 1;
}
}
}

Ok(())
}
21 changes: 21 additions & 0 deletions examples/extract_text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
let filename: String = std::env::args()
.collect::<Vec<_>>()
.get(1)
.expect("missing filename")
.to_owned();
let document = mupdf::document::Document::open(&filename)?;

for page in document.pages()? {
let text_page = page?.to_text_page(mupdf::text_page::TextPageOptions::empty())?;

for block in text_page.blocks() {
for line in block.lines() {
let chars: String = line.chars().map(|c| c.char().unwrap()).collect();
println!("line: {}", chars);
}
}
}

Ok(())
}
Loading