Guide
How to extract text from a scanned PDF in C#
Use ZingPDF.OCR when the page text is baked into scanned images instead of embedded in the PDF
text layer. The built-in Tesseract engine supports OCR-first workflows as well as workflows that keep
embedded text when it is already present.
This is a separate package because OCR has different runtime needs from normal PDF parsing. The core
ZingPDF package still handles PDFs that already contain selectable text.
Add the OCR package alongside the core package
OCR lives in ZingPDF.OCR. It builds on the main PDF API rather than replacing it.
The default engine implementation uses Tesseract, so you also need a tessdata folder for the
language you want to recognize. If you need the language files or runtime setup details, see the
official Tesseract documentation.
Extract text with OCR
This example extracts plain text from a scanned or image-based PDF with the built-in Tesseract engine.
using ZingPDF;
using ZingPDF.OCR;
using var pdf = Pdf.Load(File.OpenRead("scanned-input.pdf"));
var engine = new TesseractOcrEngine("./tessdata", "eng");
var text = await pdf.ExtractPlainTextWithOcrAsync(engine);
Console.WriteLine(text);
Keep embedded text when it already exists
Some workflows want OCR only for image-based pages, while others want OCR to be the main extraction path.
PdfOcrOptions lets you choose how embedded text is treated.
The default options prefer embedded text when it is present, but you can pass options explicitly if you want that behavior to be obvious in code.
using ZingPDF;
using ZingPDF.OCR;
using var pdf = Pdf.Load(File.OpenRead("input.pdf"));
var engine = new TesseractOcrEngine("./tessdata", "eng");
var text = await pdf.ExtractPlainTextWithOcrAsync(
engine,
new PdfOcrOptions
{
PreferEmbeddedText = true
});
OCR the visible page
Choose rendered-page mode for mixed PDF text and scans, tiled images or drawing commands. A digital header does not bypass OCR of the scanned body in this mode.
var result = await pdf.ExtractTextWithOcrAsync(1, engine,
new PdfOcrOptions
{
Mode = PdfOcrMode.RenderedPage,
Dpi = 300,
MaxPixelCount = 50_000_000
}, cancellationToken);
Console.WriteLine(result.Source);
Console.WriteLine(result.InputCoverage);
PDFium renders the visible crop/media intersection with page rotation and annotations to PNG. The selected OCR engine receives that image. PreferEmbeddedText is ignored in rendered-page mode.
InputCoverage reports the area supplied to OCR. It does not certify recognition accuracy or that every visible word was recovered.
Current limits
The default image-candidate mode returns nonblank embedded text first, otherwise sends the largest supported image XObject to OCR. It can omit a scanned body after a digital header or process only one scan tile.
Rendered-page mode requires the native PDFium dependencies and an OCR engine with installed language data. Resolution is 72–600 DPI; the defaults are 300 DPI and a 50-million-pixel limit. Validate the native dependencies on the deployed image.
Cancellation is passed through the workflow, but native rendering and in-process OCR may finish their current operation before stopping. Enforce hard deadlines with a separately supervised worker process.
Working with PDFs that already contain text?
Use the normal extraction API when the document already contains a reliable PDF text layer.