Foxit PDF SDK for Linux Foxit PDF SDK for Mac Foxit PDF SDK for Windows

How to Add Bookmarks with Foxit PDF SDK (Java)

Bookmark

Foxit PDF SDK provides navigational tools called Bookmarks to allow users to quickly locate and link their point of interest within a PDF document. PDF bookmarks are also called outline, and each bookmark contains a destination or actions to describe where it links to. The hierarchy is tree-structured. This means that function pdf.PDFDoc.getRootBookmark must be called first to get the root of the whole bookmark tree. Here, “root bookmark” is an abstract object which can only have some child bookmarks without sibling bookmarks and any data (including bookmark data, destination data and action data). It cannot be shown on the application UI since it has no data. Therefore, a root bookmark can only call the function Bookmark.getFirstChild.

After the root bookmark is retrieved, the following functions can be called to access other bookmarks:

  • To access the parent bookmark, use function Bookmark.getParent.
  • To access the first child bookmark, use function Bookmark.getFirstChild.
  • To access the next sibling bookmark, use function Bookmark.getNextSibling.
  • To insert a new bookmark, use function Bookmark.Insert.
  • To move a bookmark, use function Bookmark.moveTo.

Example:

How to find and list all bookmarks of a PDF

import com.foxit.sdk.pdf.Bookmark;
import com.foxit.sdk.pdf.PDFDoc;
...
// Assuming PDFDoc doc has been loaded.
...
Bookmark root = doc.getRootBookmark();
if (root.isEmpty()) {
root = doc.createRootBookmark();
}
String titleStr = "";
Bookmark iterBookmark = root.getFirstChild ();
if (iterBookmark.isEmpty())
return;
while (!iterBookmark.isEmpty())
{
titleStr = iterBookmark.getTitle();
if (iterBookmark.hasChild())
{
Bookmark childBookmark = iterBookmark.getFirstChild();
titleStr = childBookmark.getTitle();
}
iterBookmark = iterBookmark.getNextSibling();
}
...

Updated on April 28, 2019

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: