Recursive AI

Convert a PDF to HTML, one file per page, 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.

Terminal
dotnet add package RecursiveAI.OnePdf.Html

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OnePdf.Html;
 
PdfHtml.Convert("report.pdf", "pages",
    new() {
        Layout = PdfHtmlOutputLayout.PagePerFile
    });

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.

The overload that returns a string gives you one HTML document and ignores Layout.

The download includes report.pdf, a three-page field guide with one heading per page: “Trail map,” “Water stops,” and “Return route.” The snippet writes an HTML file for each page in pages/.

The full program

C#
using OnePdf.Html;
 
var options = new PdfHtmlOptions
{
    Layout = PdfHtmlOutputLayout.PagePerFile,
};
var result = PdfHtml.Convert("report.pdf", "pages", options);
Console.WriteLine($"Converted {result.PageCount} pages.");
Text
Converted 3 pages.

The pages/ directory contains:

Text
index.html
page-0001.html
page-0002.html
page-0003.html

Open index.html to choose a page, or open page-0002.html directly to read “Water stops.” If the count looks right but one file is empty, check result.Diagnostics: an unreadable page can produce a page-failed error without stopping the other pages.

Variations

Export only page 2. Replace the PdfHtml.Convert line in the full program, leaving the var result = assignment in place. It writes only-page-2/index.html and only-page-2/page-0002.html.

C#
PdfHtml.Convert("report.pdf", "only-page-2", new()
{
    Layout = PdfHtmlOutputLayout.PagePerFile,
    PageRanges = PdfHtmlPageRanges.Parse("2"),
});

Keep each page’s layout and put assets in separate files. This writes the three HTML pages and index again. Any images and fonts go in fixed-pages/assets/ instead of being embedded in the HTML.

C#
PdfHtml.Convert("report.pdf", "fixed-pages", new()
{
    Layout = PdfHtmlOutputLayout.PagePerFile,
    Mode = PdfHtmlMode.FixedLayout,
    Assets = PdfHtmlAssetMode.Files,
});

The short answer

Set Layout to PdfHtmlOutputLayout.PagePerFile and pass an output directory to PdfHtml.Convert. The converter names the page files page-0001.html, page-0002.html, and so on, and writes an index.html that links to them. Use the overload with an output path: the overload that returns a string gives you a single HTML document and ignores Layout.

Why does converting a PDF to separate HTML pages create an extra file?

index.html is the page list, not a converted PDF page. The numbered files are the converted pages. To link to a particular page from your site, use its numbered file. A PDF link to another page becomes a link to the corresponding HTML file if the destination page was included in the conversion. If you export a subset, the filenames keep the original page numbers: exporting only page 2 does not create a page-0001.html.

Can each HTML page have its own fonts and images embedded?

Yes. The default, PdfHtmlAssetMode.Inline, puts images and fonts into the HTML as data: URLs. Use it if you want to move an individual page file without carrying along an asset folder. Links to other pages still need their destination files. Set Assets to PdfHtmlAssetMode.Files when you want the converter to write images and fonts into assets/ instead. Keep that subdirectory beside the page files when you move or publish the output.

Can each HTML file keep the layout of the original PDF page?

Yes. The layout setting controls how many files you get. The mode setting controls how the contents of those files look. The default, semantic mode, writes headings and paragraphs that reflow to fit the browser. It can also join a table or paragraph that crosses a page break and put the whole block in the file for the page where it starts. Use fixed-layout mode when you want the HTML files to follow the source pages visually: it uses a page-sized box and positions content inside it instead of reflowing it to the browser’s width.

All onepdf-html recipes