---
title: "Lazy-render PDF pages in an HTML export in C#"
url: https://recursiveai.org/onepdf-html/cookbook/lazy-render-pages-in-pdf-to-html-output/
description: "Defer rendering offscreen pages in a fixed-layout PDF-to-HTML export in C#. OnePdf.Html uses CSS content-visibility without JavaScript."
version: "1.0.0"
---

# Lazy-render PDF pages in an HTML export 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/lazy-render-pages-in-pdf-to-html-output/lazy-render-pages-in-pdf-to-html-output.zip)

For `report.pdf` in the example zip, CSS `content-visibility: auto` lets the browser skip drawing offscreen pages, without loading them on demand. Fixed layout only; no JavaScript.

```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", "lazy.html",
    new() { Mode = PdfHtmlMode.FixedLayout,
            LazyPages = true });
```

Writes `lazy.html` with CSS for lazy page rendering.

[The example](https://recursiveai.org/onepdf-html/cookbook/lazy-render-pages-in-pdf-to-html-output/lazy-render-pages-in-pdf-to-html-output.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`.

## The full program

The sample has six pages labeled **Report page 1** through **Report page 6**. Open `lazy.html` in a browser and scroll to the last page. The program also checks that the output contains the CSS rule and no script tag.

```csharp
using OnePdf.Html;

var options = new PdfHtmlOptions
{
    Mode = PdfHtmlMode.FixedLayout,
    LazyPages = true,
};
var result = PdfHtml.Convert("report.pdf", "lazy.html", options);
Console.WriteLine($"Wrote lazy.html ({result.PageCount} pages).");

string html = File.ReadAllText("lazy.html");
Console.WriteLine($"Lazy CSS: {html.Contains("content-visibility:auto")}");
Console.WriteLine($"Script tag: {html.Contains("<script", StringComparison.OrdinalIgnoreCase)}");
```

```text
Wrote lazy.html (6 pages).
Lazy CSS: True
Script tag: False
```

If the browser doesn’t support `content-visibility`, the optimization doesn’t apply; the page content is still in the HTML.

## Variations

If the reader only needs a subset, add this line inside the options initializer. The converter writes pages 2 and 3 only; the browser can still defer drawing them when they’re offscreen.

```csharp
PageRanges = PdfHtmlPageRanges.Parse("2-3"),
```

## Related recipes

- [Convert PDF to HTML and keep the layout](https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html-keeping-layout/)
- [Convert selected PDF pages to HTML](https://recursiveai.org/onepdf-html/cookbook/convert-selected-pdf-pages-to-html/)

## The short answer

Set `Mode = PdfHtmlMode.FixedLayout` and `LazyPages = true` when converting the PDF. OnePdf.Html writes each page into a sized HTML box and marks it for CSS `content-visibility: auto`. A browser that supports the property can skip rendering a page’s contents until it approaches the visible part of the scrolling window.

## How does `content-visibility: auto` defer offscreen rendering?

[CSS `content-visibility`](https://developer.mozilla.org/en-US/docs/Web/CSS/content-visibility) lets a browser skip work on an offscreen element’s contents. The exported page box still occupies space, so it isn’t the same as hiding the section with `display: none`. OnePdf.Html sets the width and height of the box and supplies a `contain-intrinsic-size` value for the same dimensions. You don’t need to observe scrolling or insert pages using JavaScript.

The option applies to the page as a whole, not just its pictures. A page can contain positioned text, graphics and links. The contents stay in the document, so this isn’t a way to hide information from readers or leave blank pages in the DOM until someone opens them.

## Does lazy rendering also load the PDF pages on demand?

No. The C# conversion still writes every selected page. With the default single-file, inline-asset export, the browser receives the whole HTML file; `LazyPages` changes rendering, not which bytes the converter writes or which pages the browser downloads. If you only need part of the PDF, use `PageRanges` to leave out the rest. If readers should open separate page documents, use `PdfHtmlOutputLayout.PagePerFile` to write an index and one HTML file per selected page.
