Recursive AI

Keep only H1 and H2 in HTML-to-PDF bookmarks in C#

OneRender is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

The PDF outline is the viewer’s list of clickable bookmarks. Set MinLevel = 1 and MaxLevel = 2 to keep H1 and H2; deeper headings still appear on the pages.

Terminal
dotnet add package RecursiveAI.OneRender.Pdf

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneRender.Pdf;
 
var options = new HtmlPdfOptions
{
    Outline = new PdfOutlineOptions
    {
        MinLevel = 1, MaxLevel = 2
    }
};
string html = """
<h1>Handbook</h1>
<h2>Setup</h2>
<h3>Install the app</h3>
<h2>Support</h2>
""";
using var file =
    File.Create("handbook.pdf");
var pdf = HtmlPdf.ToPdf(
    html, file, options);
Console.WriteLine(
    $"Pages: {pdf.PageCount}");

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Text
Pages: 1

In handbook.pdf, “Setup” and “Support” appear under “Handbook” in the bookmarks panel. “Install the app” is still printed but isn’t a bookmark.

A level outside 1–6, or a maximum below the minimum, throws ArgumentOutOfRangeException.

Variations

Bookmark only H2 headings. Replace the Outline setting:

C#
Outline = new PdfOutlineOptions
{
    MinLevel = 2, MaxLevel = 2
}

“Setup” and “Support” are now both top-level entries; neither is nested under “Handbook.”

The short answer

Pass MinLevel = 1, MaxLevel = 2 in HtmlPdfOptions.Outline. The range is inclusive: OneRender takes rendered h1 and h2 headings and skips h3 through h6 when it builds the bookmark list. It still lays out those deeper headings on the PDF pages. Use this when the panel should get readers to chapters and sections without listing every small topic.

How do I stop a heading level from appearing in the PDF bookmarks?

Set MaxLevel to the deepest HTML heading you want in the panel. The number refers to the tag name, not the indentation you see in the viewer. MaxLevel = 2 keeps an h2 even if it’s the first heading in the document and has no parent bookmark. It skips h3 even if that heading would otherwise appear at the top level. Don’t use display: none to trim the bookmarks if readers still need those headings in the body: that changes the print layout, not just the outline.

The filter is a range, not a list of tags. A range of 1–3 includes h2 as well as h1 and h3. It doesn’t provide a way to keep h1 and h3 while dropping h2. Likewise, MaxEntries isn’t a substitute for MaxLevel: capping entries can drop a later chapter while keeping an early subtopic.

How do I make H2 bookmarks top-level entries?

Set both ends of the range to 2, as in the variation. None of the h1 headings become bookmarks, so there’s no parent entry to nest an h2 under. With a range of 1–2, an h2 under a retained h1 can instead appear as a child. Check the outline after choosing the range rather than assuming the numbers set a fixed indent for each tag.

Filtering bookmarks also doesn’t delete links to the skipped headings. For example, a contents link to an h3 with a matching id can still jump to it when internal links are enabled. The outline range chooses the viewer’s entries; clickable links on the page and the printed text are separate.

All onerender recipes