Recursive AI

Add bookmarks to a new PDF in C#

OnePDF is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Add an outline link that jumps to a page.

Terminal
dotnet add package RecursiveAI.OnePdf

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

C#
using OnePdf;
 
using var file = File.Create("report.pdf");
using var writer = new PdfDocumentWriter(file);
writer.BeginPage(300, 400).Commit();
writer.AddBookmark("Summary",
    level: 0, pageIndex: 0, top: 400);
writer.Finish();
Console.WriteLine("Wrote report.pdf");

Unzip the example, then dotnet run.

Text
Wrote report.pdf

top is measured in points up from the bottom of the page. Open the outline panel in report.pdf and select Summary to jump to the first page.

OnePDF 1.25.0 has no bookmark API for editing an existing PDF; AddBookmark works only while creating a PDF.

The full program

The download draws simple shapes on two pages so you can tell them apart. It adds Results, then reopens the PDF and prints the bookmark’s saved destination as a one-based page number.

C#
using OnePdf;
 
using (var output = File.Create("report.pdf"))
using (var writer = new PdfDocumentWriter(output))
{
    using var cover = writer.BeginPage(300, 400);
    cover.Rectangle(36, 280, 228, 60);
    cover.Stroke();
    cover.Commit();
 
    using var results = writer.BeginPage(300, 400);
    results.Rectangle(36, 240, 228, 100);
    results.Fill();
    results.Commit();
 
    writer.AddBookmark("Results", 0, 1, 400);
    writer.Finish();
}
 
using var doc = PdfDocument.Open("report.pdf");
var entry = doc.Outline[0];
var target = entry.Destination ?? throw new InvalidDataException(
    "Bookmark has no destination.");
Console.WriteLine($"{entry.Title} -> page {target.PageIndex + 1}");
Text
Results -> page 2

If you pass a page index for a page the writer hasn’t begun, AddBookmark throws ArgumentOutOfRangeException.

Variations

Nest a bookmark. In the full program, replace its AddBookmark call with these two lines before Finish:

C#
writer.AddBookmark("Report", 0, 0, 400);
writer.AddBookmark("Results", 1, 1, 400);

Jump lower on the page. Replace the bookmark in the full program with this one to target the top edge of the filled rectangle instead of the top edge of the page:

C#
writer.AddBookmark("Results", 0, 1, 340);

The short answer

To add bookmarks to a PDF you generate in C#, call PdfDocumentWriter.AddBookmark(title, level, pageIndex, top) before Finish(). title is the label the reader sees, level: 0 creates a top-level entry, and pageIndex: 0 targets the first page. top is the y coordinate to jump to. A destination doesn’t draw anything on the page: the blank page in the snippet still has a working bookmark.

Can I add bookmarks to an existing PDF?

Not with PdfDocumentWriter.AddBookmark. It attaches outline entries to pages begun by that writer, not to pages in a file opened with PdfDocument.Open. When you control how a report is generated, add the entry after beginning its destination page and before finishing the file. To inspect an existing PDF, use PdfDocument.Outline to read the top-level items, Children to walk down the outline, and Destination to read the jump target. The full program uses the same reading API on the file it just wrote.

How do I create nested bookmarks in a PDF?

Write the bookmarks in the order you want to see them. In the nested variation, Report at level 0 is the parent and Results at level 1 is its child. Another level 1 after Results would be a sibling; another level 0 would start a new top-level entry. You can’t jump from level 0 straight to level 2. The content creation reference explains how the writer builds the outline.

The nesting level and the destination are independent. You can put a parent and child on the same page and give them different y coordinates. For a 400-point-high page created with BeginPage(300, 400), top: 400 targets the top. If you draw a heading at y: 300, a top a little higher than 300 puts the jump above the heading, not at a fixed distance from the previous bookmark. Use the y coordinate from your page drawing code, not a measurement down from the top of the viewer.

How do I combine PDF files and keep bookmarks?

OnePDF’s page import options import outlines by default when you copy pages between documents. That’s separate from AddBookmark: page import carries over outline entries for the pages you copy; the writer call creates new entries. If you copy only part of a document, don’t assume all its bookmark targets will be present in the result. Reopen the merged file and check the destination pages by name or page number with Outline.

All onepdf recipes