Rust Requires Chilkat v11.7.0+
Rust
Extract PDF Text to a StringBuilder and Save to a File
See more PDF Text Extraction Examples
Demonstrates ExtractTextSb, which writes the extracted text to a StringBuilder instead of returning it as a JSON string. Use it for large documents. The JSON output still receives the quality report.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at long_report.pdf
let pdf = chilkat::Pdf::new();
if pdf.load_file("qa_data/pdf/long_report.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("Number of pages: {}", pdf.num_pages());
// ExtractTextSb takes the same operation and options as ExtractText, but the text
// goes to a StringBuilder. This avoids building a very large JSON string for
// documents with many pages.
let json = chilkat::JsonObject::new();
let sb_text = chilkat::StringBuilder::new();
let json_out = chilkat::JsonObject::new();
if pdf.extract_text_sb("text", &json, &sb_text, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("Extracted {} characters.", sb_text.length());
// Save the text to a file.
if sb_text.write_file("qa_output/long_report.txt", "utf-8", false).is_err() {
println!("{}", sb_text.last_error_text());
return;
}
// The JSON output does not contain the text, but it still has the quality report.
println!("Quality: {}", json_out.string_of("quality.rating").unwrap_or_default());
println!("Pages with no text (need OCR): [{}]", json_out.string_of("quality.pagesNeedingOcr").unwrap_or_default());
// Sample output:
// Number of pages: 3
// Extracted 4522 characters.
// Quality: good
// Pages with no text (need OCR): []