Recursive AI

Make a scanned PDF searchable in C#

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

Add invisible text so you can search and copy words from 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();
SearchablePdf.MakeSearchable(
    "scan.pdf", "searchable.pdf", engine);

Writes searchable.pdf without printing anything. Use your own scan.pdf or the one in the zip.

Unzip the example, then dotnet run.

The full program

The example includes an image-only PDF. It shows “Harbor inventory” and “Canvas sails and brass lanterns.” The program also reports how much text it added.

C#
using OneOcr;
using OneOcr.Pdf;
 
var engine = OcrEngine.LoadBundled();
var result = SearchablePdf.MakeSearchable(
    "scan.pdf", "searchable.pdf", engine);
 
Console.WriteLine($"Added layers: {result.PagesWithTextLayer}");
Console.WriteLine($"Words written: {result.WordsWritten}");
foreach (string note in result.Diagnostics)
    Console.Error.WriteLine(note);
Text
Added layers: 1
Words written: 7

Open searchable.pdf in your PDF reader and search for lanterns. If the input PDF is damaged, MakeSearchable can throw OcrFormatException.

Variations

PDF/A is an archival PDF standard; use this variation when your records system requires PDF/A-2b. Replace the var result = SearchablePdf.MakeSearchable(...) statement in the full program with:

C#
var archive = new SearchablePdfOptions
{
    Conformance = SearchablePdfConformance.PdfA2b
};
var result = SearchablePdf.MakeSearchable(
    "scan.pdf", "archive.pdf", engine, archive);

If your scan is an image rather than a PDF, use ToSearchablePdf instead. The zip includes page.png, the same inventory as a PNG. Replace that same statement with the following. OneImage comes with the PDF package.

C#
var image = OneImage.Image.Load("page.png");
var result = image.ToSearchablePdf(
    engine, "from-image.pdf");

The short answer

Use SearchablePdf.MakeSearchable from RecursiveAI.OneOcr.Pdf when you already have a scanned PDF and want another PDF you can search. The bundled OCR (optical character recognition) engine reads the scan locally. It writes recognized words at the positions where they appear, as invisible text over the original page. You don’t need to export the PDF pages to image files first.

Why is a scanned PDF not searchable?

A scanner can save a photograph of each page inside a PDF. The letters are just pixels. A PDF reader can show the page, but its Find command has no text to look through. A PDF can also contain a mix: for example, a digital cover sheet with selectable text followed by scanned attachments. Try selecting a word on the page you care about, not just on the first page.

OneOcr adds a text layer rather than replacing the image with typeset text. In the searchable PDF specification, each recognized word has a position and a frame based on its line. That gives the PDF reader text to highlight over the corresponding words. The scan still shows the original printed text, even when OCR misreads it.

Can I make a scanned PDF searchable if some pages already have text?

Yes. In normal PDF output, MakeSearchable leaves pages with trusted text alone and adds a layer where it recognizes words from pixels. It doesn’t needlessly turn an already searchable cover sheet into an image. An invisible layer left by an earlier OCR run is normally replaced rather than stacked under a second copy of the same words. Set PdfOcrOptions.TrustExistingOcrLayer through SearchablePdfOptions.Pdf if you know you want to keep those layers.

PagesWithTextLayer counts pages that received new text; it doesn’t count pages that were searchable already. A zero count can therefore mean nothing needed adding, or that nothing was recognized. Check the result’s diagnostics and try searching the output before discarding or replacing an original. If you only need text and word positions for your application, PdfOcr.Read returns those without writing another PDF.

Why isn’t my PDF OCR recognizing text?

First check whether the words are legible in the original at normal zoom. Blurred letters and faint characters can still look like words to a person but be misread in the invisible layer. Search for a few distinctive words from different pages and copy a line into a text editor; they tell you more than the page count alone. The bundled OneOcr recognizer is for printed Latin, Greek and Cyrillic text. Don’t count on it to make handwritten notes or unsupported scripts searchable. If you have a clearer scan, run OCR on that copy rather than repeatedly recognizing the same poor image.

All oneocr recipes