---
title: "Convert PDF to HTML and keep the layout in C#"
url: https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html-keeping-layout/
description: "Convert PDF to HTML in C# while keeping the page layout. Use OnePdf.Html’s fixed-layout mode to preserve page size, text placement, and graphics."
version: "1.0.0"
---

# Convert PDF to HTML and keep the layout 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-keeping-layout/convert-pdf-to-html-keeping-layout.zip)

```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("sign.pdf", "fixed.html",
    new() { Mode = PdfHtmlMode.FixedLayout });
```

[The example](https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html-keeping-layout/convert-pdf-to-html-keeping-layout.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 `fixed.html`. The zip includes `sign.pdf`, a one-page schedule with two colored boxes side by side:

```text
Room schedule

East room        West room
9:00 AM          2:00 PM
```

## The full program

```csharp
using OnePdf.Html;

var options = new PdfHtmlOptions
{
    Mode = PdfHtmlMode.FixedLayout
};
var result = PdfHtml.Convert("sign.pdf", "fixed.html", options);
Console.WriteLine($"Converted {result.PageCount} page(s).");
```

```text
Converted 1 page(s).
```

Open `fixed.html` in a browser. The sample’s two boxes stay side by side, and you can select “East room” as text. The generated page box is 384 × 240 CSS pixels at the default zoom. If a page or part of it has to be drawn as a picture, look for `raster-fallback` in `result.Diagnostics`.

## Variations

**Scale the page down for a narrower preview.** Replace the `Convert` call at the top with:

```csharp
PdfHtml.Convert("sign.pdf", "small.html", new()
{
    Mode = PdfHtmlMode.FixedLayout,
    Zoom = 0.75
});
```

The sample page box becomes 288 × 180 CSS pixels. The content scales with the box; the two rooms don’t stack.

## Related recipes

- [Convert a PDF to reflowing HTML in C#](https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html/)
- [Convert a scanned PDF to HTML with OCR](https://recursiveai.org/onepdf-html/cookbook/convert-scanned-pdf-to-html-with-ocr/)

## The short answer

Set `Mode = PdfHtmlMode.FixedLayout` when calling `PdfHtml.Convert`. OnePdf.Html writes an HTML box for each PDF page. The sample schedule keeps its title above the two columns and its times inside their original boxes. The output is one HTML file by default, with images and fonts included in the file.

## How do I convert PDF to HTML without losing the formatting?

Use fixed layout when the position carries meaning: a time belongs to a room because it sits in that room’s box, not because of where it happens to land in reading order. The converter positions text on the page, emits vector graphics as SVG, and rebuilds web fonts from the glyph shapes it can get from the PDF. In the default mode, `Semantic`, the browser lays out the text without a fixed page size.

Fixed layout doesn’t promise an exact match for every PDF. If the browser can’t express a drawing operation, the converter can render that region as an image and place selectable text over it. Some print effects, such as overprint, aren’t simulated and are reported in diagnostics. See the [fixed-layout guide](https://recursiveai.org/onepdf-html/docs/pdf-to-html/#fixed-layout-output) for the drawing features and fallbacks, and compare a representative page with the PDF before relying on the result.

## Why does my converted HTML look different from the original PDF?

First check the mode. Semantic mode changes the layout by design: a browser can wrap the same paragraph to several different widths. In fixed layout, check whether the PDF embedded its fonts. For standard fonts that aren’t embedded, OnePdf.Html uses substitute outlines, so a letter can look different even though the layout is fixed.

If the page looks too wide, adjust `Zoom` as in the variation above. A fixed page is not responsive HTML: shrinking the page shrinks its text, too. `RasterDpi` is a separate setting that affects fallback pictures, not the size of the page or the resolution of its SVG paths. If the text needs to stay large and wrap on phones, use semantic mode instead.
