Efficiently manage PDF bookmarks on Android
Bookmarks provide a convenient way of marking where you last stopped in your PDF documents. The PDF spec doesn’t contain a standardized way to store bookmarks in PDF documents, so for this reason, Nutrient will store bookmarks as part of the XMP metadata in a way that’s compatible with Nutrient on all platforms, as well as with Apple Preview.
Working with bookmarks
For most use cases, you don’t need to interact with the bookmarks model yourself, as Nutrient 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 Nutrient 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.
Accessing bookmarks
To access bookmarks, you can use the bookmarks property (getBookmarks() in Java) or the suspend function getBookmarksAsync(). This will give you a list of all bookmarks sorted correctly.
Adding and removing bookmarks
To add bookmarks, call addBookmark() or the suspend function addBookmarkAsync() on the bookmark provider, passing in the bookmark to add. If you instead want to remove a bookmark, call removeBookmark() or the suspend function removeBookmarkAsync(). To store these changes, save the associated document, which makes the bookmark provider save its content. By default, Nutrient automatically saves changes to a document at specific points.
Updating bookmarks
If you want to make changes to a bookmark, you can modify the object you received from bookmarks. Any changes you make will automatically be synced to the bookmark provider.