Create and edit PDF bookmarks using PDF4NET
A PDF document may contain a document outline that a PDF viewer application may display on the screen, allowing the user to navigate interactively from one part of the document to another. The outline consists of a tree-structured hierarchy of outline items (sometimes called PDF bookmarks), which serve as a visual table of contents to display the document’s structure to the user. The user may interactively expand and collapse individual bookmarks by clicking them with the mouse. Clicking the text of any visible bookmark activates the bookmark, causing the PDF viewer to jump to a destination or trigger an action associated with the bookmark.
The standard PDF4NET library includes support for working with the document outline from code. New bookmarks can be created, existing bookmarks can be updated or removed from the document.
The document outline is available through the document's Outline property. It is a collection of bookmarks that represent the root bookmarks in the document outline.
Add a new bookmark
A bookmark in the document outline hierarchy is represented by the PDFOutlineItem class. The child bookmarks are accessible through the Items property. The text displayed in the PDF viewer is accessible through the Title property.
The code below shows how to create a root bookmark.
| C# | |
|---|---|
Child bookmarks are created the same way, only they are added to the Items collection of a bookmark.
| C# | |
|---|---|
The visual appearance of a bookmark can be customized through the VisualStyle and Color properties. The VisualStyle sets the style of the font used to display the bookmark title while the Color sets the foreground color of the bookmark title.
| C# | |
|---|---|
A bookmark can be associated with either a destination or an action (they are mutually exclusive). When the bookmark is clicked, the PDF viewer executes the associated destination or action.
The code below shows how to setup the bookmark so that when the user clicks it, the PDF viewer application displays the first page of the document.
A bookmark can be associated with any PDF action, it is not limited to going to a specific page in the document (although this is the most common usage scenario for bookmarks).
The code below shows how to setup a bookmark to open a webpage when it is clicked.
Edit an existing bookmark
When a PDF file is loaded in a PDFFixedDocument object, the Outline property is populated automatically with the bookmarks that exist in the PDF file. Each bookmark in the PDF file is available as a PDFOutlineItem object loaded with the bookmark properties from the PDF file.
The code below shows how to print the document outline from an existing PDF file, printing the target page number for each bookmark .
Delete an existing bookmark
A bookmark is deleted when it is removed from the parent collection. When a bookmark is removed, all its children bookmarks are also removed.
The code below shows how to remove the document outline from a PDF file.
| C# | |
|---|---|
All the changes above are performed on the in-memory copy of the PDF file, they are not performed on the source PDF file. The changes are persisted when the document is saved.
Read-only bookmark inspection
If you need to read the bookmarks in a PDF file and you do no need to modify them then the PDFFile class provides a lighter alternative to PDFFixedDocument.
The ExtractOutlines method returns the collection of bookmarks from the PDF file. The original destinations in the PDF file are replaced with PDFPageNumberDestination objects so that you have access to target page number of each bookmark.
Samples
The Outlines sample available online on GitHub shows how to create the document outline for a PDF file.