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 when the bookmark is selected.
bookmark.setPage(document.getPages().getFirst());
// Display the entire page in the document window.
bookmark.setDestination(new FitDestination());
// Add the bookmark to the document.
document.getBookmarks().add(bookmark);
Create a Bookmark Destination
Call the setDestination(Destination value) method to specify how the target page appears when the user selects a bookmark. 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 how the page appears.
bookmark.setDestination(destination);
bookmark.setPage(document.getPages().get(1));
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.setPage(document.getPages().get(1));
chapter1.setDestination(new FitDestination());
Bookmark chapter2 = new Bookmark("Chapter 2");
chapter2.setPage(document.getPages().get(2));
chapter2.setDestination(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.setPage(document.getPages().get(1));
chapter1.setDestination(new FitDestination());
Bookmark chapter2 = new Bookmark("Chapter 2");
chapter2.setPage(document.getPages().get(2));
chapter2.setDestination(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.setDestination(new FitDestination());
bookmark.setPage(document.getPages().get(1));
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.setAction(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. |
| setPage(Page value) | Sets the page associated with the bookmark. |
| setDestination(Destination value) | Sets the destination that specifies how the target page appears when the bookmark is selected. |
| setAction(InteractiveAction value) | Sets the interactive action executed when the bookmark is selected. |
| 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. |