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 {
// 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"));
}
}
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"));
}
}
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
}
}
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"));
}
}
Please note
Frequently Asked Questions
What is the purpose of adding bookmarks and outlines to a PDF?
Adding bookmarks and outlines to a PDF improves usability and navigation, allowing users to easily navigate to key pages within the document similar to a table of contents.
How can I add bookmarks to a PDF using IronPDF in Java?
To add bookmarks to a PDF using IronPDF in Java, load the PDF with the PdfDocument class, use the BookmarkManager class to create and customize bookmarks, and then save the PDF document with the new bookmarks.
What is the role of the BookmarkManager class in IronPDF?
The BookmarkManager class in IronPDF is used to manage bookmarks in a PDF document. It allows you to add, customize, retrieve, and organize bookmarks within the document.
How do I set up a license for IronPDF in Java?
To set up a license for IronPDF in Java, use the License.setLicenseKey method with your specific license key before using any PDF functionalities.
Can I add multiple layers of bookmarks in a PDF using IronPDF?
Yes, you can add multiple layers of bookmarks in a PDF using IronPDF by using the insertBookmark method to create child bookmarks under existing bookmarks, forming a hierarchical structure.
How do I retrieve existing bookmarks from a PDF using IronPDF?
To retrieve existing bookmarks from a PDF using IronPDF, load the PDF with the PdfDocument class, access the BookmarkManager object, and use the getBookmarks method to obtain a list of all bookmarks.
What method should I use to add a bookmark at a specific index in a PDF?
Use the addNextBookmark method to add a new bookmark after a specific bookmark, and the addChildBookmark method to add a child bookmark as a deeper layer within the PDF.
What should I be cautious about when merging PDFs with bookmarks?
When merging PDFs with bookmarks, ensure that bookmark names are unique to avoid disruptions in the bookmark list.
What is zero-based indexing in the context of PDF bookmarks?
Zero-based indexing means that page numbers start from zero, so the first page of the PDF is index 0, the second page is index 1, and so on.
How can I save a PDF with modified bookmarks using IronPDF?
After modifying bookmarks in a PDF using IronPDF, use the saveAs method of the PdfDocument class to save the document with the changes.