How to Read Bookmarks in PDF using Java

This short tutorial describes how to read bookmarks in PDF using Java which is used to find some content quickly. With the help of this tutorial, you can not only retrieve bookmarks in PDF using Java but also fetch the child bookmarks of any other bookmark. You will also learn to display different properties of the bookmark like its title and formatting information like color, is bold, and is italic.

Steps to Read Bookmarks in PDF using Java

  1. Install Aspose.PDF from the Maven repository to read the bookmarks in PDF
  2. Load the target PDF file using the Document class object to fetch the bookmarks
  3. Iterate through all the bookmarks collection using OutlineItemCollection
  4. For each entry in the collection, display bookmark properties
  5. Iterate through each individual bookmark to access the child bookmarks in it
  6. Display each child bookmark properties

In this quick step-by-step procedure, we first load the target PDF file and then get the reference to its bookmarks collection to extract bookmarks in pdf using Java. We can also get access to the child bookmarks in each bookmark.

Code to Fetch Bookmarks in PDF using Java

import com.aspose.pdf.Document;
import com.aspose.pdf.License;
import com.aspose.pdf.OutlineItemCollection;
public class HowToReadBookmarksInPdfUsingJava {
public static void main() throws Exception { //main() function for HowToReadBookmarksInPdfUsingJava
// Instantiate Aspose.PDF license to remove trial version limitations while fetching bookmarks in PDF
License BookmarksPDFlicense = new License();
BookmarksPDFlicense.setLicense("Aspose.PDF.lic");
// Load the target PDF file to read the bookmarks
Document samplePDFWithBookmarks = new Document("PDFWithBookmarks.pdf");
// Iterate through all the bookmarks and fetch the bookmark text and formatting information
for (OutlineItemCollection Bookmark : (Iterable<OutlineItemCollection>) samplePDFWithBookmarks.getOutlines()) {
System.out.println("Title :- " + Bookmark.getTitle());
System.out.println("Is Italic :- " + Bookmark.getItalic());
System.out.println("Is Bold :- " + Bookmark.getBold());
System.out.println("Color :- " + Bookmark.getColor());
// Check if there is any child bookmarks
if(Bookmark.size() > 0)
{
System.out.println("Reading Child bookmarks...");
// Iterate through each bookmark to fetch child bookmark properties
for (OutlineItemCollection childBookmark : (Iterable<OutlineItemCollection>) Bookmark)
{
System.out.println("Title :- " + childBookmark.getTitle());
System.out.println("Is Italic :- " + childBookmark.getItalic());
System.out.println("Is Bold :- " + childBookmark.getBold());
System.out.println("Color :- " + childBookmark.getColor());
}
}
}
}
}

This sample code demonstrates the process to fetch bookmarks in PDF using Java by loading the target PDF file from the disc. Then it gets access to its bookmarks list using the iterable interface implementation for OutlineItemCollection. This same collection of outline items can be used to get the child bookmarks from each bookmark.

In this tutorial, we learned how to read bookmarks in PDF using Java without installing any other third-party tool. If you want to perform the same process in some other language like C-Sharp, refer to the article on How to Read Bookmarks in PDF using C#.

 English