---
title: "Create PDF bookmarks from HTML headings in C#"
url: https://recursiveai.org/onerender/cookbook/create-pdf-bookmarks-from-html-headings/
description: "Convert HTML to PDF and turn H1–H6 headings into nested bookmarks in the PDF viewer. Use OneRender’s PdfOutlineOptions in a runnable C# example."
version: "1.6.4"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneRender.Pdf) · [GitHub](https://github.com/recursive-ai-oss/onerender-csharp) · [Docs](https://recursiveai.org/onerender/docs/) · [Download example (.zip)](https://recursiveai.org/onerender/cookbook/create-pdf-bookmarks-from-html-headings/create-pdf-bookmarks-from-html-headings.zip)

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

```shell
dotnet add package RecursiveAI.OneRender.Pdf
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onerender/cookbook/create-pdf-bookmarks-from-html-headings/create-pdf-bookmarks-from-html-headings.zip) 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:

```csharp
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:

```csharp
foreach (var note in pdf.Diagnostics)
    Console.WriteLine(note.Message);
```

```text
Pages: 2
The outline stops at 2 headings.
```

## Related recipes

- [Limit PDF bookmarks to H1 and H2](https://recursiveai.org/onerender/cookbook/limit-html-pdf-bookmarks-to-h1-h2/)
- [Convert HTML to PDF with a table of contents](https://recursiveai.org/onerender/cookbook/html-to-pdf-table-of-contents/)

## 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](https://recursiveai.org/onerender/docs/pdf/#outline-and-links).

## 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.
