Foxit PDF SDK for Android

How to Apply and Manage Bookmarks with Foxit PDF SDK for Android

Bookmark (Outline)

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 bookmark is also called outline, and each bookmark contains a destination or actions to describe where it links to. It is a tree-structured hierarchy, so function PDFDoc.getRootBookmark must be called first to get the root of the whole bookmark tree before accessing the bookmark tree. Here, “root bookmark” is an abstract object which can only have child bookmarks without next sibling bookmarks and 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, following functions can be called to access other bookmarks:

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

Example:

How to go through a PDF’s bookmarks in ascending order

import com.foxit.sdk.PDFException;
import com.foxit.sdk.PDFViewCtrl;
import com.foxit.sdk.common.Constants;
import com.foxit.sdk.common.fxcrt.RectF;
import com.foxit.sdk.common.fxcrt.RectFArray;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.TextPage;
import com.foxit.sdk.common.fxcrt.PointF;
import com.foxit.sdk.pdf.TextSearch;
...

try {
 String pdfpath = "XXX/Sample.pdf";
 PDFDoc doc = new PDFDoc(pdfpath);
 // Create a text search handler for searching in PDF document.
 TextSearch textSearch = new TextSearch(doc, null);
 // Set the start page index which searching will begin. By default, end page will be the last page.
 textSearch.setStartPage(0);
 // Set the text to be searched.
 textSearch.setPattern("foxit");
 // Set the search flags to be matching case and matching whole word.
 textSearch.setSearchFlags(TextSearch.e_SearchMatchCase|TextSearch.e_SearchMatchWholeWord);
 while (textSearch.findNext()) {
 // If true, then we found a matched result.
 // Get the found page index.
 int pageIndx = textSearch.getMatchPageIndex();
 // Get the start character index of the matched text on the found page.
 int startCharIndex = textSearch.getMatchStartCharIndex();
 // Get the end character index of the matched text on the found page.
 int endCharIndex = textSearch.getMatchEndCharIndex();
 // Get the rectangular region of the matched text on the found page.
 RectFArray matchRects = textSearch.getMatchRects();
 }
}
catch (Exception e) {}
...

Reading Bookmark

A reading bookmark is not a PDF bookmark. In other words, it does not have PDF outlines. It is the bookmark for an applicable level. It is stored in the metadata (XML format) of the catalog. It allows user to add or remove a reading bookmark according to their reading preferences and navigate one PDF page easily by selecting a reading bookmark.

In order to retrieve a reading bookmark, function PDFDoc.getReadingBookmarkCount could be called to count the reading bookmarks, and function PDFDoc.getReadingBookmark could be called to get a reading bookmark by index.

This class offers several functions to get/set properties of reading bookmarks, such as the title, destination page index and creation/modified date time.

How to add a custom reading bookmark and enumerate all reading bookmarks

import com.foxit.sdk.PDFException;
import com.foxit.sdk.PDFViewCtrl;
import com.foxit.sdk.common.Constants;
import com.foxit.sdk.common.DateTime;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.ReadingBookmark;
...
// Add a new reading bookmark to pdf document, the returned bookmark stores the title and the page index.
ReadingBookmark addReadingBookmark(PDFDoc pdfDoc, String title, int pageIndex) {
 int count = pdfDoc.getReadingBookmarkCount();
 return pdfDoc.insertReadingBookmark(count,title,pageIndex);
}
// Enumerate all the reading bookmarks from the pdf document.
 void getReadingBookmark(PDFDoc pdfDoc) {
  try {
  int count = pdfDoc.getReadingBookmarkCount();
  for (int i = 0; i < count; i ++) {
  ReadingBookmark readingBookmark = pdfDoc.getReadingBookmark(i);
  if(readingBookmark.isEmpty()) continue;
  // Get bookmark title.
  String title = readingBookmark.getTitle();
  // Get the page index which associated with the bookmark.
  int pageIndex = readingBookmark.getPageIndex();
  // Get the creation date of the bookmark.
  DateTime creationTime = readingBookmark.getDateTime(true);
  // Get the modification date of the bookmark.
  DateTime modificationTime = readingBookmark.getDateTime(false);
  }
  } catch (PDFException e) {
  e.printStackTrace();
  }
 }

Updated on June 7, 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: