Recursive AI

Read an existing PDF OCR text layer in C#

OneOcr is a free, open-source OCR library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

The zip’s archive.pdf already has “Archive copy” stored as invisible text over the scan.

Terminal
dotnet add package RecursiveAI.OneOcr.Pdf

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneOcr;
using OneOcr.Pdf;
var engine = OcrEngine.LoadBundled();
var options = new PdfOcrOptions
    { TrustExistingOcrLayer = true };
var pages = PdfOcr.Read(
    "archive.pdf", engine, options);
Console.WriteLine(pages[0].ToText());

Unzip the example, then dotnet run.

Prints the embedded text without changing the PDF:

Text
Archive copy

The full program

The download reads the same one-page PDF twice and prints where each word came from.

C#
using OneOcr;
using OneOcr.Pdf;
 
var engine = OcrEngine.LoadBundled();
var input = "archive.pdf";
ShowWords("Reuse the existing layer", PdfOcr.Read(input, engine,
    new() { TrustExistingOcrLayer = true }));
ShowWords("Default read", PdfOcr.Read(input, engine));
 
static void ShowWords(string label, IReadOnlyList<OcrPage> pages)
{
    Console.WriteLine(label);
    foreach (var word in pages[0].Words)
        Console.WriteLine($"{word.Text}: {word.Source}");
}
Text
Reuse the existing layer
Archive: TextLayer
copy: TextLayer
Default read
i: Ocr
Archive: Ocr
copy: Ocr

The first pass returns the stored words. The default pass reads the image and picks up a stray i. For pages that can’t be fully read, check OcrPage.Diagnostics: the default mode can return partial results.

Variations

Read stored text only. Replace the options declaration at the top to skip rendering and image recognition entirely. The sample still prints Archive copy.

C#
var options = new PdfOcrOptions
{
    TrustExistingOcrLayer = true,
    ReadImages = false
};

The short answer

Set PdfOcrOptions.TrustExistingOcrLayer to true when you call PdfOcr.Read. OneOcr keeps the stored invisible words. With the default ReadImages = true, it runs OCR on image regions the text layer doesn’t cover and can reread the page from pixels if the visible text fails its cross-check. Words kept from the PDF have OcrSource.TextLayer; words recognized from pixels have OcrSource.Ocr. This is a read option, not a way to add or repair a layer.

What is a text layer in a scanned PDF?

It is text stored in the file, separate from the picture you see in the viewer. A previous OCR pass can put invisible words over the scan so you can select, copy, or search them. A PDF made directly by a word processor can also have selectable text, but there the text may be what the viewer actually draws. The option here changes how OneOcr treats invisible text, not the normal handling of visible text.

How can I tell if a PDF has OCR?

Open an image-backed page and try copying a sentence into a plain text editor. If recognizable words come through, the page has selectable text; compare it with what you actually see. Copy and search are useful checks, but they don’t tell you whether the whole page is covered or whether the text is accurate. In the sample program, the change from TextLayer to Ocr between the two reads shows which path supplied the words. TextLayer by itself does not distinguish visible from invisible text.

Is the existing OCR text always accurate?

No. Reusing a layer also reuses its mistakes. Leave the default in place if you don’t want the previous pass to decide what the scan says. Keep ReadImages enabled if a mixed document may have image regions that its text does not cover. OneOcr can also cross-check visible text against the rendered page and fall back to reading pixels when sampled lines disagree. Use the text-only variation when you want only the stored content, even if that leaves an image-only page without any words. See the PDF bridge for how the sources are combined.

All oneocr recipes