Create and Manage PDF Bookmarks
- 3 minutes to read
Use the Bookmark class to create bookmark hierarchies, customize bookmark appearance, and associate bookmarks with destinations or actions.
Create a Bookmark
The following code snippet creates a bookmark that scrolls to the first page:
// Create a bookmark with the specified title.
Bookmark bookmark = new Bookmark("Introduction");
// Navigate to the first page and display the entire page in the document window.
bookmark.setActionTarget(new ActionTarget(document.getPages().getFirst(), new FitDestination()));
// Add the bookmark to the document.
document.getBookmarks().add(bookmark);
Create a Bookmark Destination
To specify how the target page appears when the user selects a bookmark, pass the page and a Destination object to the ActionTarget(page, destination) constructor. Pass the created ActionTarget object to the setActionTarget(ActionTarget value) method. A destination defines view parameters, such as the visible page area, scroll position, or zoom level.
The following code snippet creates a bookmark that navigates to the second page, fits it horizontally in the document window, and scrolls to the specified vertical position:
Bookmark bookmark = new Bookmark("Chapter 1");
// Create a destination that fits the page horizontally.
FitHorizontallyDestination destination = new FitHorizontallyDestination();
destination.setTop(100.0);
// Specify the target page and how the page appears.
bookmark.setActionTarget(new ActionTarget(document.getPages().get(1), destination));
document.getBookmarks().add(bookmark);
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. |
Create Nested Bookmarks
Use the Bookmark.getChildren() method to access a bookmark’s child bookmarks and build a bookmark hierarchy.
The following code snippet creates nested bookmarks. The document outline displays a collapsible bookmark tree.
Bookmark chapters = new Bookmark("Chapters");
// Create child bookmarks.
Bookmark chapter1 = new Bookmark("Chapter 1");
chapter1.setActionTarget(new ActionTarget(document.getPages().get(1), new FitDestination()));
Bookmark chapter2 = new Bookmark("Chapter 2");
chapter2.setActionTarget(new ActionTarget(document.getPages().get(2), new FitDestination()));
// Add child bookmarks.
chapters.getChildren().add(chapter1);
chapters.getChildren().add(chapter2);
document.getBookmarks().add(chapters);
Collapse Child Bookmarks
Call the Bookmark.setInitiallyClosed method to set the initial expansion state of a bookmark.
The following code snippet collapse child bookmarks when the document opens:
Bookmark chapters = new Bookmark("Chapters");
chapters.setInitiallyClosed(true);
Bookmark chapter1 = new Bookmark("Chapter 1");
chapter1.setActionTarget(new ActionTarget(document.getPages().get(1), new FitDestination()));
Bookmark chapter2 = new Bookmark("Chapter 2");
chapter2.setActionTarget(new ActionTarget(document.getPages().get(2), new FitDestination()));
chapters.getChildren().add(chapter1);
chapters.getChildren().add(chapter2);
document.getBookmarks().add(chapters);
Customize Bookmark Appearance
Use bold, italic, and text color to emphasize important bookmarks.
Bookmark bookmark = new Bookmark("Important Section");
bookmark.setActionTarget(new ActionTarget(document.getPages().get(1), new FitDestination()));
bookmark.setBold(true);
bookmark.setItalic(true);
bookmark.setTextColor(PdfColor.getOrange());
document.getBookmarks().add(bookmark);
Associate an Interactive Action
The following code snippet opens a web page when the bookmark is selected:
Bookmark website = new Bookmark("Visit DevExpress");
website.setActionTarget(new ActionTarget(new UriAction(){{
setUri("https://www.devexpress.com");
}}));
document.getBookmarks().add(website);
Bookmark Settings
The following table lists methods that specify bookmark settings:
| Method | Description |
|---|---|
| setTitle(String value) | Sets the bookmark text displayed in the document outline. |
| setActionTarget(ActionTarget value) | Sets the bookmark target: a page with a destination that specifies how the page appears, or an interactive action. |
| setInitiallyClosed(boolean value) | Sets whether child bookmarks appear collapsed when the document opens. |
| setBold(boolean value) | Sets whether to display the bookmark title in bold. |
| setItalic(boolean value) | Sets whether to display the bookmark title in italic. |
| setTextColor(PdfColor value) | Sets the bookmark text color. |
| getChildren() | Gets the collection of child bookmarks. |