dotnet add package RecursiveAI.OnePdf.HtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OnePdf.Html; PdfHtml.Convert("sign.pdf", "fixed.html", new() { Mode = PdfHtmlMode.FixedLayout });
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 fixed.html. The zip includes sign.pdf, a one-page schedule with two colored boxes side by side:
Room schedule East room West room 9:00 AM 2:00 PM
The full program
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).");
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:
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
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 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.