Recursive AI

Convert a scanned PDF to Markdown 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 logbook.pdf has two scanned pages: “Morning check” and “Evening check.”

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 pages = PdfOcr.Read("logbook.pdf", engine);
var markdown = pages.ToMarkdown();
File.WriteAllText("logbook.md", markdown);

Unzip the example, then dotnet run.

Writes this to logbook.md (no console output):

MARKDOWN
# Morning check
 
## The pump is ready. The tank is full.
 
---
 
# Evening check
 
## The pump is off. The valve is closed.

OneOcr infers heading levels from text size, so it formats the body sentences in this scan as level-two headings.

The full program

The sample has “The pump is ready. The tank is full.” on page one and “The pump is off. The valve is closed.” on page two. The download saves the Markdown and prints it so you can see the page break in the terminal.

C#
using OneOcr;
using OneOcr.Pdf;
 
var engine = OcrEngine.LoadBundled();
var pages = PdfOcr.Read("logbook.pdf", engine);
var markdown = pages.ToMarkdown();
File.WriteAllText("logbook.md", markdown);
Console.Write(markdown);
MARKDOWN
# Morning check
 
## The pump is ready. The tank is full.
 
---
 
# Evening check
 
## The pump is off. The valve is closed.

OneOcr infers the heading levels here; it doesn’t recover styles from the scan. A page that can’t be fully read can have partial content and a diagnostic, so check OcrPage.Diagnostics if something looks missing.

Variations

Convert only the second page. Replace the Read line at the top. FirstPage is zero-based; the resulting file starts with # Evening check.

C#
var pages = PdfOcr.Read("logbook.pdf", engine,
    new() { FirstPage = 1, PageCount = 1 });

The short answer

Call PdfOcr.Read and pass the resulting page list to ToMarkdown(). Then write the string to a .md file. The list overload writes pages in the order supplied and adds a --- break between pages that produce content. You don’t need to export individual images or join their text by hand.

How can I convert a scanned PDF to a Markdown file?

Read the PDF first, then format the recognized page model. An image-only PDF has no stored text to turn into headings or paragraphs, so OneOcr has to read the pixels. If the PDF also contains usable text, the same reader can merge that text with OCR from image regions. The Markdown formatter works from the resulting blocks and their reading order, regardless of where the words came from. It adds structure without changing the input PDF.

ToText() is useful when you just want the words. Use ToMarkdown() if the next step can use headings, lists, or pipe tables. Keep the page results if your application also needs word positions, diagnostics, or a record of which page supplied a passage. The Markdown alone doesn’t carry those details: for instance, --- marks a break between pages with output and does not represent an empty page.

What are the disadvantages of using Markdown for a scanned PDF?

Markdown preserves some reading structure, not the page design. Headings can be inferred from text size, which is why body sentences became level-two headings in the example. A recognized table can be written as a pipe table, but merged cells cannot keep their visual span: only the top-left slot gets the cell’s text. The formatter does not reproduce pictures, links, or font styling, and it drops running headers and footers by default. The Markdown format notes describe which elements are kept. Check the output against the scan if the placement of a heading, a table cell, or a paragraph changes the meaning.

All oneocr recipes