OCR a scanned PDF in C#, two ways
96.2%
of scan words, 10 pages in 15.8 s
Same words, very different setup
Both on Linux: the .NET 10 SDK image, arm64, one 10-page scan.
| 10 pages | Local | Actor |
|---|---|---|
| Words found | 1,880 (96.2%) | 1,875 (95.9%) |
| Time | 15.8 s | 278.5 s |
| C# lines | 14 | 12 |
| Packages | 2 NuGet | None |
| Native setup | apt and 3 links | None |
| Price | Free | $0.06 |
One way is fast. The other never asks you about libdl.
Local: PDFtoImage, then Tesseract
PDFtoImage renders each page, and the Tesseract package reads it. The first argument is the PDF's path.
using PDFtoImage;
using SkiaSharp;
using Tesseract;
using var engine = new TesseractEngine("/usr/share/tesseract-ocr/5/tessdata", "eng", EngineMode.Default);
engine.SetVariable("user_defined_dpi", "300");
var pdf = File.ReadAllBytes(args[0]);
foreach (var bitmap in Conversion.ToImages(pdf, options: new RenderOptions(Dpi: 300)))
{
using (bitmap)
using (var png = bitmap.Encode(SKEncodedImageFormat.Png, 100))
using (var pix = Pix.LoadFromMemory(png.ToArray()))
using (var page = engine.Process(pix))
Console.Write(page.GetText());
}
The two package lines of the .csproj:
<PackageReference Include="PDFtoImage" Version="5.4.0" />
<PackageReference Include="Tesseract" Version="5.2.0" />
Linux: apt, then three links
Install tesseract-ocr with apt. The Tesseract package
then looks for fixed names next to the app, in an x64 folder even on
arm64.
| Link next to the app | Points to |
|---|---|
| x64/libtesseract50.so | libtesseract.so.5 |
| x64/libleptonica-1.82.0.so | liblept.so.5 |
| libdl.so | libdl.so.2 |
The targets are in /usr/lib/$(uname -m)-linux-gnu/. The
full Dockerfile is on
our Tesseract in C# guide.
Without the third link: a libdl error
With only the two x64 links, the TesseractEngine
constructor stopped with:
System.DllNotFoundException: Unable to load shared library 'libdl' or one of its dependencies.
The SDK image has libdl.so.2 but no
libdl.so. The third link fixed it.
Keep the default 300 dpi
PDFtoImage renders at 300 dpi by default. The code also tells Tesseract the dpi.
| Dpi | Words | Time, peak |
|---|---|---|
| 150 | 1,878 | 7.1 s, 105 MB |
| 200 | 1,883 | 9.5 s, 128 MB |
| 300, the default | 1,880 | 15.8 s, 214 MB |
| 600 | 1,882 | 46.5 s, 552 MB |
- 600 dpi took 2.9 times as long, for 2 more words.
-
Without the
user_defined_dpiline: 1,879 words, and no warning.
The Actor: one HTTP call, no packages
Our Apify Actor,
PDF to Markdown OCR, fetches the PDF from a URL and returns Markdown. Put your Apify
token in APIFY_TOKEN.
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using var http = new HttpClient { Timeout = TimeSpan.FromMinutes(5) };
http.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("APIFY_TOKEN"));
var res = await http.PostAsJsonAsync(
"https://api.apify.com/v2/acts/brasspage~pdf-ocr-to-markdown/run-sync-get-dataset-items",
new { documentUrls = new[] { args[0] } });
res.EnsureSuccessStatusCode();
var items = await res.Content.ReadFromJsonAsync<JsonElement>();
Console.Write(items[0].GetProperty("markdown").GetString());
It found 1,875 of 1,955 words, 5 fewer than local Tesseract.
The 100 s default: a retry pays twice
Without the Timeout line, new HttpClient() gave up after
100 s:
System.Threading.Tasks.TaskCanceledException: The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing.
The run went on, finished on Apify and charged the 10 pages. A retry pays for them again.
The fix is the Timeout line:
Timeout = TimeSpan.FromMinutes(5).
Over 300 s: start the run, then poll
Our 10-page runs took 275.9 s to 281.9 s on Apify.
Apify's sync endpoint waits at most 300 s. Past that, poll the run:
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using var http = new HttpClient { BaseAddress = new Uri("https://api.apify.com/v2/") };
http.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("APIFY_TOKEN"));
var start = await http.PostAsJsonAsync("acts/brasspage~pdf-ocr-to-markdown/runs",
new { documentUrls = new[] { args[0] } });
start.EnsureSuccessStatusCode();
var run = (await start.Content.ReadFromJsonAsync<JsonElement>()).GetProperty("data");
while (run.GetProperty("status").GetString() is "READY" or "RUNNING")
run = (await http.GetFromJsonAsync<JsonElement>(
$"actor-runs/{run.GetProperty("id")}?waitForFinish=60")).GetProperty("data");
if (run.GetProperty("status").GetString() != "SUCCEEDED")
throw new Exception("Run " + run.GetProperty("status"));
var items = await http.GetFromJsonAsync<JsonElement>(
$"datasets/{run.GetProperty("defaultDatasetId")}/items");
Console.Write(items[0].GetProperty("markdown").GetString());
Same text as the one call, in 276.9 s.
Local if you can ship native libraries
- Local: free and faster, if your app can carry tesseract-ocr and the three links.
- The Actor: when you cannot ship native libraries, or would rather not. $0.006 per OCR page. Pages with their own text layer are free.
Tested on Linux arm64, in Docker
Each run had its own container on an Apple M5 Max, with 2 CPUs and 2 GB.
Not tested: Windows, x64 Linux, Alpine, a born-digital PDF, a PDF over 10 pages.
How we ran it
- Image: mcr.microsoft.com/dotnet/sdk:10.0, Ubuntu 24.04.5, .NET SDK 10.0.404, arm64.
- apt tesseract-ocr: Tesseract 5.3.4, Leptonica 1.82.0, the Ubuntu package's eng.traineddata.
- NuGet: PDFtoImage 5.4.0 (MIT, with PDFium and SkiaSharp) and Tesseract 5.2.0 (charlesw, Apache-2.0), the newest stable on Nov 26.
- Time is the whole docker run, .NET start included. Peak is the container's memory peak: dotnet, PDFium and Tesseract together.
-
Tesseract threads:
OMP_THREAD_LIMIT=2. One run at a time. Other jobs used the Mac during the runs. - The PDF pages were made for 300 dpi, so 300 dpi gives back the 1920 px scan.
- The page's local code gave the same text, byte for byte, as the 300 dpi test run.
- The Actor: build 0.1.145 at its default 1,024 MB, about 28 s a page. Not tried: more memory.
- Each Actor run charged 10 pages and 1 start: $0.06005 for a user.
- Scan: the 10 proofread en.wikisource pages of our n8n guide, as one PDF. Score: true words found in order.
- Scripts and results stay in our private repo, in research/bench/csharp-pdf-ocr-2026-11-26/.
Questions people ask
How do I OCR a scanned PDF in C#?
Render each page to an image with PDFtoImage, then read it with the Tesseract NuGet package. On Linux that took 14 lines of C#, apt tesseract-ocr and 3 links, and read 96.2% of our scan words.
Why does Tesseract in C# fail with a libdl error on Linux?
The Tesseract package asks for the bare name libdl.so, and our Ubuntu 24.04 image has only libdl.so.2. A libdl.so link to it, next to your app, fixed it.
Why does HttpClient time out when I call the OCR Actor?
HttpClient gives up after 100 s by default. Our 10-page scan took the Actor about 280 s, and the run still charged the pages. Set a longer Timeout, or start the run and poll it.