Bookmarks provide a convenient way of marking where you last stopped in your PDF documents. The PDF spec does not contain a standardized way to store bookmarks in PDF documents, so for this reason, PSPDFKit will store bookmarks as part of the XMP metadata in a way that is compatible with PSPDFKit on all platforms, as well as with Apple Preview.

Work with Bookmarks

For most use cases, you do not need to interact with the bookmarks model yourself, as PSPDFKit provides a UI for users to add, remove, and sort bookmarks depending on the configuration in use. It then automatically stores them in the PDF document they belong to. However, when you want to provide either your own UI or your own bookmarks store, you need to access the model that PSPDFKit provides for you.

The bookmarks model essentially consists of two classes: Bookmark, which is the bookmark itself and contains information about its name and what to do if a user selects a bookmark; and BookmarkProvider, which is the container that keeps track of all the bookmarks of a document. You can access the bookmark provider through PdfDocument#getBookmarkProvider(). This can then be used to retrieve, sort, add, and remove bookmarks.

Access Bookmarks

To access bookmarks, you can use getBookmarks() or getBookmarksAsync(). This will give you a list of all bookmarks sorted correctly.

Add and Remove Bookmarks

To add bookmarks, simply call addBookmark() or addBookmarkAsync() on the bookmark provider, passing in the bookmark that you want to add to the bookmark provider. If you instead want to remove a bookmark, call removeBookmark() or removeBookmarkAsync(). If you want these changes to be stored, you need to save the associated document, which in turn will make sure the bookmark provider saves its content. By default, PSPDFKit automatically saves changes to a document at specific points.

Update Bookmarks

If you want to make changes to a bookmark, you can just modify the object you received by calling getBookmarks(). Any changes you make will automatically be synced to the bookmark provider.