---
title: "Convert a PDF to HTML in C#"
url: https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html/
description: "Convert PDF to HTML in C# with OnePdf.Html. Write a browser-ready file with headings and paragraphs that reflow, or get the HTML as a string."
version: "1.0.0"
---

# Convert a PDF to HTML 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) · [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-pdf-to-html/convert-pdf-to-html.zip)

Make PDF text wrap in a browser. For image-only scans, [use OCR](https://recursiveai.org/onepdf-html/cookbook/convert-scanned-pdf-to-html-with-ocr/).

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

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

```csharp
using OnePdf.Html;

PdfHtml.Convert("report.pdf", "report.html");
```

[The example](https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html/convert-pdf-to-html.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 `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.”

```csharp
using OnePdf.Html;

var result = PdfHtml.Convert("report.pdf", "report.html");
Console.WriteLine($"Converted {result.PageCount} page(s).");
```

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

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

```csharp
string html = PdfHtml.Convert("report.pdf");
```

**Write one HTML file per page.** In that same first snippet, replace the `Convert` call with:

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

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

## 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`](https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html-keeping-layout/). 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](https://recursiveai.org/onepdf-html/docs/pdf-to-html/#the-happy-path-pdfhtml).

## 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](https://recursiveai.org/onepdf-html/docs/pdf-to-html/#where-the-output-goes-files-assets-and-sinks) gives the folder names for each option.
