Before running text extraction, check whether the PDF was digitally created or is a scan of a physical document.
use pdfluent::PdfDocument;
fn main() -> pdfluent::Result<()> {
let doc = PdfDocument::open("file.pdf")?;
let text = doc.extract_text()?;
if text.trim().len() < 20 {
println!("likely scanned (no text layer)");
}
Ok(())
}No additional features are required. Page inspection is part of the base crate.
# Cargo.toml
[dependencies]
pdfluent = "1.0.0-beta.8"Use doc.pages() to get an iterator over all pages. Each Page gives you access to content stream analysis.
use pdfluent::PdfDocument;
let doc = PdfDocument::open("document.pdf")?;
for i in 0..doc.page_count() {
let has_text = !doc.page(i)?.text()?.trim().is_empty();
println!("Page {}: selectable text = {}", i + 1, has_text);
}has_selectable_text() returns true if the page content stream contains any text operators. has_raster_images() returns true if the page contains XObject images.
for i in 0..doc.page_count() {
let text = doc.page(i)?.text()?;
if text.trim().is_empty() {
println!("Page {} appears to be a scan (no selectable text).", i + 1);
}
}Count pages without text. A score above 80% is a strong indicator that the document is a scan or a mix.
let total = doc.page_count() as f32;
let mut no_text = 0f32;
for i in 0..doc.page_count() {
if doc.page(i)?.text()?.trim().is_empty() {
no_text += 1.0;
}
}
let scan_ratio = no_text / total;
println!("Scan ratio: {:.0}%", scan_ratio * 100.0);
if scan_ratio > 0.8 {
println!("Likely a scanned document. Consider running OCR.");
}Some scanned PDFs have a hidden text layer added by OCR software. Use has_invisible_text() to detect this.
// An image-only PDF yields little or no extractable text
let extracted = doc.extract_text()?;
if extracted.trim().is_empty() {
println!("No extractable text layer - the document is image-only.");
}No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.
Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.
Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.