Recursive AI

Create PDF bookmarks from HTML headings 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.

A PDF outline is the list of clickable bookmarks in the viewer. Set Outline to build it from the headings in the HTML below.

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()
};
string html = """
<h1>Field guide</h1>
<h2>Equipment</h2>
<h3>Camera</h3>
<h2 style="break-before: page">
  Routes
</h2>
""";
using var file =
    File.Create("guide.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: 2

Open guide.pdf and show the viewer’s bookmarks or outline panel. Select “Routes” to jump to the second page.

If the outline is empty, check that Outline is set and that your heading elements are visible in the print layout and contain text.

Variations

Limit the outline to the first two headings. Replace the Outline setting in the example:

C#
Outline = new PdfOutlineOptions
{
    MaxEntries = 2
}

The PDF still has both pages. The bookmarks panel retains “Field guide” and “Equipment.” Add this after the existing Console.WriteLine to print the renderer’s diagnostics:

C#
foreach (var note in pdf.Diagnostics)
    Console.WriteLine(note.Message);
Text
Pages: 2
The outline stops at 2 headings.

The short answer

Set HtmlPdfOptions.Outline to new PdfOutlineOptions() when you call HtmlPdf.ToPdf. By default the outline is off. With it enabled, OneRender walks the rendered h1 through h6 elements in document order, uses their text for the bookmark titles, and points each bookmark to where its heading starts in the laid-out PDF. You don’t need to give each heading an id or calculate page numbers yourself. See the outline and links docs.

How do I create nested bookmarks in a PDF?

Use real headings in order: h1 for the main section, h2 for a subsection, and h3 for a topic inside it. The example produces this outline:

Text
Field guide
  Equipment
    Camera
  Routes

“Routes” is a sibling of “Equipment,” not of “Camera,” because both are h2. If you skip a level, such as going straight from h1 to h3, the h3 can still be a child of the h1; OneRender doesn’t insert an empty entry for the missing level. CSS font size doesn’t control the outline: making a paragraph look like a heading doesn’t turn it into a bookmark.

Why are some headings missing from the PDF bookmarks?

An empty heading or one hidden by display: none doesn’t make a bookmark. Check print styles in particular: a heading that appears on a screen can be hidden when the PDF is laid out for print. The title comes from the heading’s text, with whitespace collapsed and a maximum of 256 characters. Check both the HTML element and its text if you see a title that looks different from the design.

If the outline starts correctly but ends early, check the result’s Diagnostics. MaxEntries caps the total number of entries in document order; it’s not a heading-depth setting. When the cap is reached and another eligible heading is found, later entries are omitted and OneRender records a diagnostic.

Can a PDF have both bookmarks and a table of contents?

Yes. Bookmarks live in the viewer’s navigation panel; a table of contents is visible on a document page and can appear on paper. PdfOutlineOptions adds the former, not the latter. For a visible contents page, add an HTML list of links to heading IDs and render it along with the rest of the document. That list and the outline can point to the same sections.

All onerender recipes