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.
dotnet add package RecursiveAI.OnePdf.HtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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 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.
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)}");
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.
PageRanges = PdfHtmlPageRanges.Parse("2-3"),Related recipes
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 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.