Docs / Guides
Bookmarks and links
Bookmarks give readers a table of contents in the viewer's navigation pane; links make areas of a page clickable.
Bookmarks
document.Outline.Add(title, page) adds a top-level bookmark and returns it, so you can add children with
Add on the returned item, to any depth. Pass top (and optionally left) to scroll to
a position on the page; without them the bookmark shows the whole page. IsOpen decides whether a
bookmark's children start expanded. Titles can be any Unicode text.
using SoftZenPdf;
var document = new PdfDocument();
var intro = document.AddPage();
var details = document.AddPage();
intro.Content.DrawText("1. Introduction", 72, 770, StandardFont.HelveticaBold, 20);
details.Content.DrawText("2. Details", 72, 770, StandardFont.HelveticaBold, 20);
var chapter = document.Outline.Add("1. Introduction", intro);
chapter.Add("1.1 Background", intro, top: 500);
chapter.IsOpen = true;
document.Outline.Add("2. Details", details);
document.Save("bookmarks.pdf");
When a document has bookmarks, viewers open it with the bookmarks pane showing.
Links
A link target is either an absolute Uri (https:, mailto: and so on) or another page
of the same document, optionally scrolled to a top position. There are two ways to add one:
page.DrawLink(text, x, y, font, size, target)draws the text in link blue (or acoloryou choose), underlines it unless you passunderline: false, and makes it clickable. It returns the text width, so a sentence can continue after an inline link.page.AddLink(x, y, width, height, target)makes any rectangle clickable, for example a button you drew or an image. The area is invisible unless you passshowBorder: true(black) or aborderColor; the border is drawn by the link's own appearance, so it shows in every viewer.
using SoftZenPdf;
var document = new PdfDocument();
var first = document.AddPage();
var second = document.AddPage();
var font = StandardFont.Helvetica;
// DrawLink draws the text, underlines it and makes it clickable.
first.DrawLink("Jump to page 2", 72, 770, font, 14, second);
first.DrawLink("Visit example.com", 72, 745, font, 14, new Uri("https://example.com/"));
// It returns the text width, so a sentence can continue after an inline link.
var x = 72.0;
first.Content.DrawText("Questions? ", x, 715, font, 12);
x += font.MeasureText("Questions? ", 12);
x += first.DrawLink("Email us", x, 715, font, 12, new Uri("mailto:hello@example.com"));
first.Content.DrawText(" any time.", x, 715, font, 12);
// AddLink makes any area clickable, here with a red border.
first.Content.DrawRectangle(72, 620, 200, 60, fill: PdfColor.FromHex("#F4F2FE"));
first.AddLink(72, 620, 200, 60, new Uri("https://example.com/offer"), borderColor: PdfColor.Red);
document.Save("links.pdf");
Reading bookmarks and links
PdfReader.ReadOutline returns the bookmark tree with target page indices (named destinations are resolved),
and GetLinks lists a page's link areas with their URI or target page.
using SoftZenPdf;
foreach (var entry in PdfReader.Open("bookmarks.pdf").ReadOutline())
Console.WriteLine($"{entry.Title} → page {entry.PageIndex + 1}, {entry.Children.Count} child bookmark(s)");
foreach (var link in PdfReader.Open("links.pdf").GetLinks(0))
Console.WriteLine(link.Uri ?? $"link to page {link.TargetPageIndex + 1}");
1. Introduction → page 1, 1 child bookmark(s)
2. Details → page 2, 0 child bookmark(s)
link to page 2
https://example.com/
mailto:hello@example.com
https://example.com/offer
Limits
Bookmarks are not carried over when pages are merged or extracted, and links to pages that were not copied stop working. Links target URIs or pages of the same document: not named destinations, other PDF files or applications. Link borders are 1 point and solid.
API: PdfOutline, PdfOutlineItem, PdfPage.DrawLink, PdfPage.AddLink.