---
title: "Add PDF bookmarks from HTML headings in C#"
url: https://recursiveai.org/onepdf-html/cookbook/add-bookmarks-from-html-headings-to-pdf/
description: "Turn HTML headings into clickable PDF bookmarks in C#. Keep only the heading levels you want in the PDF outline, with a runnable .NET 10 example."
version: "1.0.0"
---

# Add PDF bookmarks from HTML headings 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.Render) · [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/add-bookmarks-from-html-headings-to-pdf/add-bookmarks-from-html-headings-to-pdf.zip)

Headings become bookmarks by default; set `MaxLevel = 2` to keep only `h1` and `h2`.

```shell
dotnet add package RecursiveAI.OnePdf.Html.Render
```

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

```csharp
using OnePdf.Html.Render;

var result = HtmlToPdf.ConvertFile(
    "guide.html", "guide.pdf", new()
    {
        Outline = new() { MaxLevel = 2 }
    });
Console.WriteLine($"Pages: {result.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/onepdf-html/cookbook/add-bookmarks-from-html-headings-to-pdf/add-bookmarks-from-html-headings-to-pdf.zip) and `dotnet run`.

`dotnet run` prints:

```text
Pages: 2
```

Writes `guide.pdf`. The input is `guide.html`, included in the example ZIP. If you’re pasting the C# into a new project, save this HTML as `guide.html` next to `Program.cs`:

```html
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Field guide</title>
<h1>Field guide</h1>
<h2>Preparation</h2>
<p>Bring a notebook.</p>
<h3>Checklist</h3>
<p>Pack a pencil and a spare battery.</p>
<h2 style="break-before: page">On site</h2>
<p>Record the location before taking notes.</p>
</html>
```

Open the PDF’s Bookmarks or Outline panel. “Preparation” and “On site” appear under “Field guide”; clicking “On site” jumps to page 2. “Checklist” is still printed on page 1, but it isn’t in the panel. If `guide.html` can’t be read, the converter throws `HtmlToPdfException` with `Failure` set to `Input`.

## Variations

With the same `using` and `guide.html`, replace the conversion and print lines with one of these:

**Include all heading levels.** Omit the options to bookmark rendered `h1` through `h6`. The “Checklist” bookmark now appears under “Preparation.”

```csharp
HtmlToPdf.ConvertFile("guide.html", "all.pdf");
```

**Skip the document heading.** Replace the `Outline` assignment in the snippet at the top to include `h2` through `h6`, but not `h1`:

```csharp
Outline = new() { MinLevel = 2 }
```

**Leave out the outline.** This keeps the headings and the PDF’s accessibility structure, but writes no bookmarks.

```csharp
var options = new HtmlToPdfOptions { Outline = null };
HtmlToPdf.ConvertFile("guide.html", "no-outline.pdf", options);
```

## Related recipes

- [Convert HTML to PDF/A-2a](https://recursiveai.org/onepdf-html/cookbook/convert-html-to-pdfa/)
- [Add page numbers, headers and footers to HTML-to-PDF output](https://recursiveai.org/onepdf-html/cookbook/add-page-numbers-header-footer-html-to-pdf/)

## The short answer

`HtmlToPdf.ConvertFile` builds a PDF outline from rendered HTML headings by default. Each bookmark jumps to the start of its heading. The converter keeps document order and nests entries by heading level. To shorten the list, set `Outline.MaxLevel` to the deepest heading you want. Use `MinLevel` to skip higher levels, such as an `h1` that just repeats the cover title. These options change the navigation, not the printed headings.

## How do I make bookmarks appear automatically in a PDF?

Use actual heading tags in the input HTML. Styling a paragraph to look like a heading doesn’t make it an `h2`, so it doesn’t make a bookmark. Use `h2` for sections and `h3` for subsections so the panel follows the hierarchy readers see on the page.

If you don’t see the entries, first open the viewer’s Bookmarks or Outline panel. Then check the HTML and the options. A heading must render in the PDF and fall between `MinLevel` and `MaxLevel`. `MaxEntries` caps the number of bookmarks (10,000 by default), and `Outline = null` turns them off altogether. See the [outline options](https://recursiveai.org/onepdf-html/docs/html-to-pdf/#links-outline-metadata-and-tags).

## Can a PDF have a table of contents as well as bookmarks?

Yes. Bookmarks live outside the page in the viewer’s panel. If you want a list that appears on the page and is visible in print, put it in the HTML itself. Link a contents entry to a heading by matching `href="#on-site"` to `id="on-site"`:

```html
<p><a href="#on-site">On site</a></p>
<h2 id="on-site">On site</h2>
<p>Record the location before taking notes.</p>
```

The default link policy keeps in-document anchors clickable. With the default outline as well, “On site” is reachable from both the page link and the Bookmarks panel. You don’t need an `id` for the automatic bookmark; it’s there for the link on the page.
