How to Add PDF Bookmarks and Outline in Java

Including PDF bookmarks in your Java project can significantly improve the usability and navigation of your PDFs. Outlines in PDFs provide an easy method of navigation within the PDF itself, so you can easily navigate to key pages within the document much like how you would with a table of contents.

IronPDF is a powerful PDF tool that makes working with PDF files a breeze. Its bookmarking tool gives you a concise, easy-to-use method for creating custom bookmarks for your PDF files.

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer

Before We Get Started

You will also need to make sure you have your license key set up correctly, as IronPDF must be licensed for development.

Add Outline & Bookmarks Example

For today's example, I will use this sample PDF to apply the outline and bookmarks.

Add Single Layer of Bookmarks

After loading this PDF from the specified file path using the PdfDocument class, we can start adding bookmarks to the document by retrieving the BookmarkManager object. You can add the bookmark to the start or end of the bookmark collection using the addBookMarkAtEnd and addBookMarkAtStart methods.

TipsRemember that all page indexes follow zero-based indexing.

import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.bookmark.BookmarkManager;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load the PDF file
        PdfDocument pdf = PdfDocument.fromFile(Path.of("NovelSample.pdf"));

        // Get BookmarkManager object to manage bookmarks
        BookmarkManager bookmarks = pdf.getBookmark();

        // Add bookmarks at the end of the bookmark collection
        bookmarks.addBookMarkAtEnd("Title Page", 0);
        bookmarks.addBookMarkAtEnd("Table of Contents", 1);
        bookmarks.addBookMarkAtEnd("Dedication Page", 2);
        bookmarks.addBookMarkAtEnd("First Page", 3);
        bookmarks.addBookMarkAtStart("Page 4", 6);

        // Save the modified PDF with bookmarks
        pdf.saveAs(Path.of("bookmarked.pdf"));
    }
}
import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.bookmark.BookmarkManager;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load the PDF file
        PdfDocument pdf = PdfDocument.fromFile(Path.of("NovelSample.pdf"));

        // Get BookmarkManager object to manage bookmarks
        BookmarkManager bookmarks = pdf.getBookmark();

        // Add bookmarks at the end of the bookmark collection
        bookmarks.addBookMarkAtEnd("Title Page", 0);
        bookmarks.addBookMarkAtEnd("Table of Contents", 1);
        bookmarks.addBookMarkAtEnd("Dedication Page", 2);
        bookmarks.addBookMarkAtEnd("First Page", 3);
        bookmarks.addBookMarkAtStart("Page 4", 6);

        // Save the modified PDF with bookmarks
        pdf.saveAs(Path.of("bookmarked.pdf"));
    }
}
JAVA

With the PDF viewer above, you can check the table of contents, which is located at the top left corner of most browsers, to see all the bookmarks that we have added.

Add Multiple Layers of Bookmarks

In this example, we'll start by adding bookmarks, just like we did when creating a single layer of bookmarks. Next, we'll use the insertBookmark method to add a new bookmark on a new layer and give it a name using the method's first parameter. The second parameter specifies the page the new bookmark links to. To create a new layer, we make the new bookmark a "child" of an existing bookmark, which is done using the method's third parameter.

import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.bookmark.BookmarkManager;

public class Main {
    public static void main(String[] args) throws IOException {
        // Load the PDF file
        PdfDocument pdf = PdfDocument.fromFile(Path.of("NovelSample.pdf"));

        // Get BookmarkManager object
        BookmarkManager bookmarks = pdf.getBookmark();

        // Add bookmarks at the end
        bookmarks.addBookMarkAtEnd("Title Page", 0);
        bookmarks.addBookMarkAtEnd("Table of Contents", 1);
        bookmarks.addBookMarkAtEnd("Dedication", 2);

        // Insert second layer bookmarks
        bookmarks.insertBookmark("First Page", 3, "Table of Contents", null);
        bookmarks.insertBookmark("Second Page", 4, "Table of Contents", "First Page");
        bookmarks.insertBookmark("End of Sample", 7, "Title Page", null);
        bookmarks.insertBookmark("Fourth page", 6, "Table of Contents", "Second Page");

        // Save the modified PDF with multiple layer bookmarks
        pdf.saveAs(Path.of("multiLayer.pdf"));
    }
}
import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.bookmark.BookmarkManager;

public class Main {
    public static void main(String[] args) throws IOException {
        // Load the PDF file
        PdfDocument pdf = PdfDocument.fromFile(Path.of("NovelSample.pdf"));

        // Get BookmarkManager object
        BookmarkManager bookmarks = pdf.getBookmark();

        // Add bookmarks at the end
        bookmarks.addBookMarkAtEnd("Title Page", 0);
        bookmarks.addBookMarkAtEnd("Table of Contents", 1);
        bookmarks.addBookMarkAtEnd("Dedication", 2);

        // Insert second layer bookmarks
        bookmarks.insertBookmark("First Page", 3, "Table of Contents", null);
        bookmarks.insertBookmark("Second Page", 4, "Table of Contents", "First Page");
        bookmarks.insertBookmark("End of Sample", 7, "Title Page", null);
        bookmarks.insertBookmark("Fourth page", 6, "Table of Contents", "Second Page");

        // Save the modified PDF with multiple layer bookmarks
        pdf.saveAs(Path.of("multiLayer.pdf"));
    }
}
JAVA

Here you can see the PDF with our new tree structure of bookmarks. Check out the outline for yourself to see how the insertBookmark feature has added a new layer of bookmarks.


Retrieving Bookmarks

IronPDF's bookmarking tool not only adds new bookmarks but also retrieves and views existing ones. To navigate through bookmarks, first load the PDF using the PdfDocument.fromFile method. Then, access the BookmarkManager object and use the getBookmarks method to retrieve all bookmarks, including child bookmarks. Finally, use the get method to retrieve a bookmark by its index in the list.

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.bookmark.Bookmark;
import com.ironsoftware.ironpdf.bookmark.BookmarkManager;

public class Main {
    public static void main(String[] args) throws IOException {
        // Load the PDF file with bookmarks
        PdfDocument pdf = PdfDocument.fromFile(Path.of("bookmarked.pdf"));

        // Retrieve the bookmark manager
        BookmarkManager bookmarks = pdf.getBookmark();

        // Retrieve list of all bookmarks
        List<Bookmark> bookmarkList = bookmarks.getBookmarks();

        // Retrieve a specific bookmark by its index
        Bookmark bookmark = bookmarkList.get(2);

        // Example usage of the retrieved bookmark could be added here
    }
}
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.bookmark.Bookmark;
import com.ironsoftware.ironpdf.bookmark.BookmarkManager;

public class Main {
    public static void main(String[] args) throws IOException {
        // Load the PDF file with bookmarks
        PdfDocument pdf = PdfDocument.fromFile(Path.of("bookmarked.pdf"));

        // Retrieve the bookmark manager
        BookmarkManager bookmarks = pdf.getBookmark();

        // Retrieve list of all bookmarks
        List<Bookmark> bookmarkList = bookmarks.getBookmarks();

        // Retrieve a specific bookmark by its index
        Bookmark bookmark = bookmarkList.get(2);

        // Example usage of the retrieved bookmark could be added here
    }
}
JAVA

Insert Bookmark at Specific Index

With the retrieved bookmarks, you have the option to add new bookmarks at specific indexes within the document. To do this, select the targeted bookmark and use the addNextBookmark method to add a new bookmark after it. For example, we take the PDF from the 'Add Multiple Layers of Bookmarks' section and add a bookmark after the 'Third Page' bookmark. You can also add a child bookmark as a deeper layer using the addChildBookmark method.

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.bookmark.Bookmark;
import com.ironsoftware.ironpdf.bookmark.BookmarkManager;

public class Main {
    public static void main(String[] args) throws IOException {
        // Load the PDF we modified earlier
        PdfDocument pdf = PdfDocument.fromFile(Path.of("multiLayer.pdf"));

        // Get the BookmarkManager
        BookmarkManager bookmarks = pdf.getBookmark();

        // Retrieve the list of bookmarks
        List<Bookmark> bookmarkList = bookmarks.getBookmarks();

        // Retrieve a specific bookmark by its index
        Bookmark bookmark = bookmarkList.get(5);

        // Add a new bookmark after the specified bookmark
        bookmark.addNextBookmark("Fourth Page", 6);

        // Add another layer to 'Third page' bookmark
        bookmark.addChildBookmark("Section 1", 7);

        // Save the modified PDF
        pdf.saveAs(Path.of("specificIndex.pdf"));
    }
}
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.bookmark.Bookmark;
import com.ironsoftware.ironpdf.bookmark.BookmarkManager;

public class Main {
    public static void main(String[] args) throws IOException {
        // Load the PDF we modified earlier
        PdfDocument pdf = PdfDocument.fromFile(Path.of("multiLayer.pdf"));

        // Get the BookmarkManager
        BookmarkManager bookmarks = pdf.getBookmark();

        // Retrieve the list of bookmarks
        List<Bookmark> bookmarkList = bookmarks.getBookmarks();

        // Retrieve a specific bookmark by its index
        Bookmark bookmark = bookmarkList.get(5);

        // Add a new bookmark after the specified bookmark
        bookmark.addNextBookmark("Fourth Page", 6);

        // Add another layer to 'Third page' bookmark
        bookmark.addChildBookmark("Section 1", 7);

        // Save the modified PDF
        pdf.saveAs(Path.of("specificIndex.pdf"));
    }
}
JAVA

Please noteIf you merge two PDF documents together whose bookmarks share identical names, this can lead to a disruption in the bookmark list itself.

Frequently Asked Questions

How can I add bookmarks to a PDF in Java?

To add bookmarks to a PDF in Java, use the IronPDF library. Load your PDF using the PdfDocument class, and then utilize the BookmarkManager class to insert and customize bookmarks. Finally, save the document to apply the changes.

What are the benefits of using bookmarks in PDF documents?

Bookmarks in PDF documents greatly enhance navigation and usability by allowing users to jump directly to important sections, similar to a table of contents.

How do I create hierarchical bookmarks in a PDF using Java?

To create hierarchical bookmarks in a PDF using Java, employ IronPDF's BookmarkManager class. Use the insertBookmark method to add child bookmarks under existing bookmarks, thereby creating multiple layers.

What is the recommended method for setting up IronPDF licensing in Java?

Set up your IronPDF license in Java by calling the License.setLicenseKey method with your license key before using any PDF features to ensure full functionality.

Can IronPDF handle the retrieval of existing PDF bookmarks?

Yes, IronPDF can retrieve existing PDF bookmarks. Load the PDF with the PdfDocument class, access the BookmarkManager, and use the getBookmarks method to list all bookmarks.

How can I add a bookmark to a specific page in a PDF using Java?

In Java, use IronPDF's addBookMarkAtEnd method to add a bookmark to a specific page within a PDF document. This allows you to pinpoint and highlight key sections.

What is the importance of zero-based indexing when working with PDF bookmarks?

Zero-based indexing is crucial when working with PDF bookmarks because page numbers start at zero. For instance, the first page is index 0, which is essential for accurate bookmark placement.

How do I save a modified PDF with new bookmarks using IronPDF in Java?

After adding or modifying bookmarks in a PDF using IronPDF, save the updated document using the saveAs method of the PdfDocument class to keep your changes.

What should I consider when merging PDFs with bookmarks using IronPDF?

When merging PDFs with bookmarks using IronPDF, ensure bookmarks have unique names to maintain a coherent and organized bookmark list without conflicts.

How can I add a child bookmark in a PDF using IronPDF?

To add a child bookmark in a PDF using IronPDF, employ the addChildBookmark method, which allows you to build a deeper, hierarchical bookmark structure within the document.

Darrius Serrant
Full Stack Software Engineer (WebOps)

Darrius Serrant holds a Bachelor’s degree in Computer Science from the University of Miami and works as a Full Stack WebOps Marketing Engineer at Iron Software. Drawn to coding from a young age, he saw computing as both mysterious and accessible, making it the perfect medium for creativity ...Read More

Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?