---
title: "Convert a scanned PDF to HTML with OCR in C#"
url: https://recursiveai.org/onepdf-html/cookbook/convert-scanned-pdf-to-html-with-ocr/
description: "Convert a scanned PDF to searchable HTML in C# with local OCR. See the recognized text and use automatic OCR for PDFs with a mix of scans and text."
version: "1.0.0"
---

# Convert a scanned PDF to HTML with OCR in C#

**OnePdf.Html** is a free, open-source .NET library from Recursive AI that converts PDF to HTML and HTML to PDF. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePdf.Html.Ocr) · [GitHub](https://github.com/recursive-ai-oss/onepdf-html-csharp) · [Docs](https://recursiveai.org/onepdf-html/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf-html/cookbook/convert-scanned-pdf-to-html-with-ocr/convert-scanned-pdf-to-html-with-ocr.zip)

Optical character recognition (OCR) turns scanned letters into searchable text.

```shell
dotnet add package RecursiveAI.OnePdf.Html.Ocr
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OnePdf.Html;
using OnePdf.Html.Ocr;
using var ocr = new OcrTextSource();
PdfHtml.Convert("scan.pdf", "scan.html",
    new() { TextSource = ocr });
```

[The example](https://recursiveai.org/onepdf-html/cookbook/convert-scanned-pdf-to-html-with-ocr/convert-scanned-pdf-to-html-with-ocr.zip) 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:

```text
Library hours
The library opens at nine.
Return books at the front desk.
```

## The full program

```csharp
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}");
```

```text
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:

```html
<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:

```csharp
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

- [Convert a PDF with selectable text to HTML](https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html/)
- [Convert a PDF to HTML and keep the layout](https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html-keeping-layout/)

## 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](https://recursiveai.org/onepdf-html/docs/pdf-to-html/#the-ocr-package), such as `RecursiveAI.OneOcr.Models.Cjk` for Japanese, before running the conversion.
