Optical character recognition (OCR) turns scanned letters into searchable text.
dotnet add package RecursiveAI.OnePdf.Html.OcrRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OnePdf.Html; using OnePdf.Html.Ocr; using var ocr = new OcrTextSource(); PdfHtml.Convert("scan.pdf", "scan.html", new() { TextSource = ocr });
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
Writes scan.html. The zip includes scan.pdf, an image-only PDF made from this text:
Library hours The library opens at nine. Return books at the front desk.
The full program
using OnePdf.Html; using OnePdf.Html.Ocr; using var ocr = new OcrTextSource(); var options = new PdfHtmlOptions { TextSource = ocr }; var result = PdfHtml.Convert("scan.pdf", "scan.html", options); Console.WriteLine($"Converted {result.PageCount} page(s)."); foreach (var item in result.Diagnostics) Console.WriteLine($"{item.Code}: {item.Message}");
Converted 1 page(s). ocr-page: 13 words recognised from a 300 dpi picture (the page has no text layer over a picture with ink on it)
Open scan.html to see the recognized text. The heading and both sentences came back; the two sentences share a paragraph:
<h2>Library hours</h2> <p>The library opens at nine. Return books at the front desk.</p>
The .Ocr package pulls in the Latin, Greek, and Cyrillic models through RecursiveAI.OneOcr and its RecursiveAI.OneOcr.Models dependency. No separate model install is needed for this English sample. If you request a language whose script model isn’t installed, the converter reports ocr-language-unsupported on pages that need OCR.
Variations
Read an English scan but keep its page layout. Keep the using directives from the snippet at the top and replace the remaining lines with:
using var ocr = new OcrTextSource(new() { Languages = ["en"] }); PdfHtml.Convert("scan.pdf", "fixed.html", new() { Mode = PdfHtmlMode.FixedLayout, TextSource = ocr });
This puts selectable text over the page image instead of turning the scan into a reflowing article.
Related recipes
The short answer
Install RecursiveAI.OnePdf.Html.Ocr and pass an OcrTextSource to PdfHtml.Convert. The default Auto mode keeps usable text from the PDF and recognizes pages where the text layer is missing or can’t be trusted. Recognition is local. The result is an HTML file, not a new searchable PDF.
How do I tell if a PDF page is scanned or contains text?
Try selecting and copying a sentence from the PDF. A scan without a text layer won’t give you text; a scan that already had OCR may let you copy it, even if what you paste is wrong. A document can also mix scanned pages and pages created from digital text. For that reason, Auto makes the decision page by page. Its checks distinguish a blank page from an image with ink, and can flag garbled text or invisible text that doesn’t line up with the ink on the page. Check for ocr-page in the conversion diagnostics to see where OCR actually ran.
How do I convert a scanned PDF to HTML without flattening the text?
Give the converter an OCR text source and choose how the words should appear. The default semantic mode makes them part of the HTML reading order. Use it when the scan is headed for a website, a content review, or indexing. Fixed layout retains the page picture and puts the recognized text over it. Use that when the position on the scan matters, for example when readers need to find a note in the margin.
Whichever mode you use, compare the HTML against the scan if spelling, names, or numbers matter. OCR reads the shapes it sees; it doesn’t have an original text layer to copy. For non-Latin scans beyond Greek and Cyrillic, install the script-family model for your language, such as RecursiveAI.OneOcr.Models.Cjk for Japanese, before running the conversion.