Bookmarks in PDF Document API
- 6 minutes to read
Bookmarks (outlines) allow users to quickly navigate from one part of a document to another. Bookmarks can target a document page or an external URI.
Use the Bookmark class to create bookmark hierarchies, customize bookmark appearance, and associate bookmarks with destinations or actions.
Create a Bookmark
The PdfDocument.Bookmarks property provides access to the collection of bookmarks in a PDF document. Use the BookmarkCollection.Add method to create a new bookmark and add it to the collection.
The following code snippet creates a bookmark and adds it to the PDF document:
using System.IO;
using DevExpress.Docs.Pdf;
using (PdfDocument pdfDocument =
new PdfDocument(File.OpenRead("Document.pdf")))
{
Bookmark bookmark = new Bookmark("Title");
bookmark.ActionTarget = new ActionTarget(pdfDocument.Pages[0], new FitDestination());
pdfDocument.Bookmarks.Add(bookmark);
Directory.CreateDirectory("output");
using (FileStream outputFileStream = File.Create(Path.Combine("output", "Document.pdf")))
{
pdfDocument.Save(outputFileStream);
}
}
Create a Bookmark that Targets a Destination
A destination is a reference to a page with specific view parameters, such as:
- The displayed document page
- A view mode or a specific point or region to display on the page
- The zoom factor
The following table lists available destination types:
| Destination | Description |
|---|---|
| FitDestination | Displays the entire page in the document window. |
| FitBBoxDestination | Fits the page’s bounding box in the document window. |
| FitHorizontallyDestination | Fits the page horizontally and scrolls to the specified top coordinate. |
| FitVerticallyDestination | Fits the page vertically and scrolls to the specified left coordinate. |
| FitBBoxHorizontallyDestination | Fits the page’s bounding box horizontally. |
| FitBBoxVerticallyDestination | Fits the page’s bounding box vertically. |
| FitRectangleDestination | Displays the specified page rectangle. |
| XyzDestination | Displays the page at a specific position and zoom level. |
Use the BookmarkCollection.Add method to create a bookmark and associate it with a destination in one step.
The following code snippet creates a bookmark that targets a specific page in the PDF document:
using System.IO;
using DevExpress.Docs.Pdf;
using (PdfDocument pdfDocument =
new PdfDocument(File.OpenRead("Document.pdf")))
{
// Create a bookmark and associate it with a destination in one step.
pdfDocument.Bookmarks.Add("Title", pdfDocument.Pages[0],
new FitBBoxDestination());
Directory.CreateDirectory("output");
using (FileStream outputFileStream = File.Create(Path.Combine("output", "Document.pdf")))
{
pdfDocument.Save(outputFileStream);
}
}
Create a Bookmark that Targets an Action
An action is an operation that is performed when a user clicks a bookmark. The PDF Document API supports the following action types:
| Action | Description |
|---|---|
| GoToAction | Navigates to a specific page in the document. |
| RemoteGoToAction | Navigates to a specific page in another PDF document. |
| UriAction | Opens a web page in the default browser. |
| JavaScriptAction | Executes a JavaScript script. |
| ResetFormAction | Resets the values of all fields in a form. |
Create an action and use the Bookmark.ActionTarget property to associate the bookmark with the action.
The following code snippet creates a bookmark that targets a specific page in the PDF document:
using System.IO;
using DevExpress.Docs.Pdf;
using (PdfDocument pdfDocument =
new PdfDocument(File.OpenRead("Document.pdf")))
{
// Create a bookmark.
Bookmark bookmark = new Bookmark("Title");
// Create a GoToAction to navigate to a specific location on the page.
GoToAction action = new GoToAction() {
Page = pdfDocument.Pages[0],
Destination = new XyzDestination() {
Left = 100,
Top = 200,
Zoom = 2.0
}
};
// Assign the action to the bookmark.
bookmark.ActionTarget = new ActionTarget(action);
// Add the bookmark to the PDF document.
pdfDocument.Bookmarks.Add(bookmark);
Directory.CreateDirectory("output");
using (FileStream outputFileStream = File.Create(Path.Combine("output", "Document.pdf")))
{
pdfDocument.Save(outputFileStream);
}
}
Create Nested Bookmarks
Use the Bookmark.Children property to access a bookmark’s child bookmarks and build a bookmark hierarchy.
The following code snippet creates nested bookmarks:
using System.IO;
using DevExpress.Docs.Pdf;
using (PdfDocument pdfDocument =
new PdfDocument(File.OpenRead("Document.pdf")))
{
// Create a parent bookmark
Bookmark parentBookmark = new Bookmark("Parent Bookmark");
parentBookmark.ActionTarget = new ActionTarget(pdfDocument.Pages[0], new FitDestination());
// Create a child bookmark
Bookmark childBookmark = new Bookmark("Child Bookmark");
childBookmark.ActionTarget = new ActionTarget(pdfDocument.Pages[1], new FitDestination());
// Add the child bookmark to the parent bookmark
parentBookmark.Children.Add(childBookmark);
// Add the parent bookmark to the PDF document
pdfDocument.Bookmarks.Add(parentBookmark);
Directory.CreateDirectory("output");
using (FileStream outputFileStream = File.Create(Path.Combine("output", "Document.pdf")))
{
pdfDocument.Save(outputFileStream);
}
}
The document outline displays a collapsible bookmark tree. Use Bookmark.InitiallyClosed to specify whether a bookmark is initially collapsed.
Customize Bookmark Appearance
You can customize bookmark text, color, and style in the document outline.
The following code snippet creates a bookmark with a customized appearance:
using System.IO;
using DevExpress.Docs.Pdf;
using (PdfDocument pdfDocument =
new PdfDocument(File.OpenRead("Document.pdf")))
{
// Create a bookmark
Bookmark bookmark = new Bookmark("Title");
bookmark.ActionTarget = new ActionTarget(pdfDocument.Pages[0], new FitDestination());
// Set the bookmark appearance
// in the navigator panel of the PDF viewer.
bookmark.TextColor = PdfColor.Red;
bookmark.Bold = true;
Directory.CreateDirectory("output");
using (FileStream outputFileStream = File.Create(Path.Combine("output", "Document.pdf")))
{
pdfDocument.Save(outputFileStream);
}
}