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.
How to Add PDF Bookmarks and Outline
- Install Java library to add bookmarks to PDFs
- Utilize the PdfDocument class to load an existing PDF file in Java
- Create and customize the bookmarks with the BookmarkManager class
- Use
addBookMarkAtEnd
to add bookmarks to specific pages of the PDF - Save the PDF document containing the new outline and bookmarks
Start using IronPDF in your project today with a free trial.
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.
Tips
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 {
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
PdfDocument pdf = PdfDocument.fromFile(Path.of("NovelSample.pdf"));
// Get BookmarkManager object
BookmarkManager bookmarks = pdf.getBookmark();
// Add bookmarks
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);
pdf.saveAs(Path.of("bookmarked.pdf"));
}
}
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 {
PdfDocument pdf = PdfDocument.fromFile(Path.of("NovelSample.pdf"));
// Get BookmarkManager object
BookmarkManager bookmarks = pdf.getBookmark();
// Add bookmarks
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");
pdf.saveAs(Path.of("multiLayer.pdf"));
}
}
Here you can see the PDF with our new tree structure of bookmarks. Check out the outline for youself 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 {
PdfDocument pdf = PdfDocument.fromFile(Path.of("bookmarked.pdf"));
BookmarkManager bookmarks = pdf.getBookmark();
// Retrieve bookmarks list
List<Bookmark> bookmarkList = bookmarks.getBookmarks();
Bookmark bookmark = bookmarkList.get(2);
}
}
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 {
PdfDocument pdf = PdfDocument.fromFile(Path.of("multiLayer.pdf"));
BookmarkManager bookmarks = pdf.getBookmark();
List<Bookmark> bookmarkList = bookmarks.getBookmarks();
Bookmark bookmark = bookmarkList.get(5);
bookmark.addNextBookmark("Fourth Page", 6);
// Add another layer to 'Third page' bookmark
bookmark.AddChildBookmark("Section 1", 7);
pdf.saveAs(Path.of("specificIndex.pdf"));
}
}