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

Added output_page fn #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Added output_page fn #63

wants to merge 1 commit into from

Conversation

JuniFruit
Copy link

Description

Added output_page pub function similar to output_doc, but now you can output specified page.

Reason

If you do page by page scanning and you need to save pages with a specified search term, you can use this function to output text on a given page.
Example:

pub fn find_pages_with_term(doc_file: &Document, term: &str) -> Vec<(u32, (u32, u16))> {
    let pages = doc_file.get_pages();
    let mut res = vec![];
    for (p, id) in pages {
        println!("Looking for {} in page {}", term, p);
        let text = page_to_text(&doc_file, &p).unwrap_or("".to_string());
        println!("{:?}", text);
        if text.find(term).is_some() {
            res.push((p, id))
        }
    }
    res
}



pub fn page_to_text(doc_file: &Document, page: &u32) -> Result<String, OutputError> {
    use pdf_extract::{output_page, OutputError, PlainTextOutput};
    let mut s = String::new();
    let mut output = PlainTextOutput::new(&mut s);
    output_page(doc_file, &mut output, page)?;
    Ok(s.clone())
}

Testing

All tests are passing, no breaking changes

@JuniFruit JuniFruit marked this pull request as ready for review July 14, 2023 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant