Make PDF text wrap in a browser. For image-only scans, use OCR.
dotnet add package RecursiveAI.OnePdf.HtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OnePdf.Html; PdfHtml.Convert("report.pdf", "report.html");
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 report.html. Use report.pdf from the zip or put your own PDF with that name next to Program.cs.
The full program
The sample PDF has a heading, “Garden notes,” and a paragraph, “The greenhouse opens at nine.”
using OnePdf.Html; var result = PdfHtml.Convert("report.pdf", "report.html"); Console.WriteLine($"Converted {result.PageCount} page(s).");
Converted 1 page(s).Open report.html in a browser or view its source. Classes like wf0 apply the generated web fonts; their CSS is in the <style> block in the HTML’s <head>. Here’s the heading and paragraph it wrote:
<h1><span class="wf0">Garden notes</span></h1> <p><span class="wf1">The greenhouse opens at nine.</span></p>
A file that isn’t a PDF throws PdfFormatException. If an individual page fails, check result.Diagnostics even when the converter writes a file.
Variations
Get a string instead of writing a file. In the first C# snippet (above “The full program”), replace the Convert call with:
string html = PdfHtml.Convert("report.pdf");Write one HTML file per page. In that same first snippet, replace the Convert call with:
var options = new PdfHtmlOptions { Layout = PdfHtmlOutputLayout.PagePerFile }; PdfHtml.Convert("report.pdf", "pages", options);
This writes pages/index.html and pages/page-0001.html for the sample. Open the index to browse the exported pages.
Related recipes
The short answer
Call PdfHtml.Convert with the input PDF path and the output HTML path. OnePdf.Html’s default mode, Semantic, writes headings, paragraphs, and other elements in reading order. That’s useful when you’re turning a report into a webpage: the browser can wrap the text to the window instead of fitting a whole PDF page into it. To keep the original page geometry instead, use FixedLayout. The conversion runs locally without an online service.
Can I turn a PDF into HTML code?
Yes. The one-argument overload returns a complete HTML document as a string, not plain text or a fragment to drop inside an existing page. Use that overload if your app needs to store the markup or send the whole document in a response. Use the two-path overload to write a file and get a PdfHtmlResult back with the page count and diagnostics. There are also overloads for a PDF you already have as a stream or bytes.
What does the converted HTML look like?
The sample’s heading and paragraph become h1 and p. In a tagged PDF, the structure tree tells the converter which pieces are headings, paragraphs, lists, and tables. In an untagged PDF like the sample, it has to infer structure. It can use an outline entry as a heading, join lines into paragraphs, and look for tables using ruling lines or the alignment of words. That inference isn’t the same as having the original document: review heading levels, reading order, and tables before publishing an untagged report.
How do I handle a multi-page PDF when converting to HTML?
By default, the output is one file and the converter can join a paragraph or table that continues across a PDF page break. It still puts a marker at each page start so links can target the original page number. Use PagePerFile if readers should open individual pages from an index. With the default inline assets, the HTML carries the images and fonts; if you opt for file assets, keep the generated asset folder with the HTML when you move it. The output guide gives the folder names for each option.