Creating Bookmarks for PDF Document in Java with Apache PDFBox

Tags: Java Apache PDFBox PDF

Introduction

In this tutorial we will learn how to add bookmark items to a PDF document in Java using Apache PDFBox library. The post also show how to add bookmarks for new PDF document and existing PDF file.

Apache PDFBox Library Overview

The Apache PDFBox is an open source Java library for working with PDF documents. You can get more information about the project at pdfbox.apache.org

Adding Apache PDFBox Dependencies

Adding below dependencies to build.gradle file if you are using Gradle build tool.

compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.18'

Adding below XML to pom.xml file if you are using Maven build tool.

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.18</version>
</dependency>

Or you can download the pdfbox-2.0.18.jar file from pdfbox.apache.org/download.cgi

Step 1 - Creating empty PDF document and add 10 blank pages

try (PDDocument document = new PDDocument()) {
    for (int i = 0; i < 10; i++) {
        document.addPage(new PDPage());
    }

} catch (IOException e) {
    e.printStackTrace();
}

Step 2 - Creating Bookmark items and set its destination

Through this tutorial we are going to creating the bookmarks for 10 pages document as below.

Creating Bookmarks for PDF Document in Java with Apache PDFBox - Bookmarks

Firstly, creating bookmark item for “All Pages”

PDDocumentOutline documentOutline = new PDDocumentOutline();
document.getDocumentCatalog().setDocumentOutline(documentOutline);
PDOutlineItem pagesOutline = new PDOutlineItem();
pagesOutline.setTitle("All Pages");
documentOutline.addLast(pagesOutline);

Secondly, we loop through each page of the document and create bookmark item for each page.

for(int i = 0; i < document.getNumberOfPages(); i++) {
    PDPageDestination pageDestination = new PDPageFitWidthDestination();
    pageDestination.setPage(document.getPage(i));
				
    PDOutlineItem bookmark = new PDOutlineItem();
    bookmark.setDestination(pageDestination);
    bookmark.setTitle("Document Page " + (i + 1));
    pagesOutline.addLast(bookmark);
}

And call openNode() method to set the bookmark node be opened or expanded in PDF reader application when the document is opened.

pagesOutline.openNode();
documentOutline.openNode();

Then set the mode to allow PDF reader application showing bookmark panel when the PDF document is opened.

document.getDocumentCatalog().setPageMode(PageMode.USE_OUTLINES);

Step 3 - Save the PDF file to disk

document.save("D:\\SimpleSolution\\BookmarkDocument.pdf");

The complete Java application code

package dev.simplesolution;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PageMode;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;

public class AddBookmarkPdfDocument {

	public static void main(String[] args) {
		try (PDDocument document = new PDDocument()) {
			for (int i = 0; i < 10; i++) {
				document.addPage(new PDPage());
			}

			PDDocumentOutline documentOutline = new PDDocumentOutline();
			document.getDocumentCatalog().setDocumentOutline(documentOutline);
			PDOutlineItem pagesOutline = new PDOutlineItem();
			pagesOutline.setTitle("All Pages");
			documentOutline.addLast(pagesOutline);
			
			for(int i = 0; i < document.getNumberOfPages(); i++) {
				PDPageDestination pageDestination = new PDPageFitWidthDestination();
				pageDestination.setPage(document.getPage(i));
				
				PDOutlineItem bookmark = new PDOutlineItem();
				bookmark.setDestination(pageDestination);
				bookmark.setTitle("Document Page " + (i + 1));
				pagesOutline.addLast(bookmark);
			}

			pagesOutline.openNode();
			documentOutline.openNode();
			
			document.getDocumentCatalog().setPageMode(PageMode.USE_OUTLINES);

			document.save("D:\\SimpleSolution\\BookmarkDocument.pdf");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
Execute the Java application above then the PDF file be created at D:\SimpleSolution\BookmarkDocument.pdf

Creating Bookmarks for PDF Document in Java with Apache PDFBox - PDF File

Adding Bookmarks for existing PDF file

To adding bookmarks to existing PDF document firstly we use PDDocument.load() static method to load the document as below.

try (PDDocument document = PDDocument.load(new File("D:\\SimpleSolution\\Document.pdf"))) {

} catch (IOException e) {
    e.printStackTrace();
}

Below is the example Java application to create bookmarks for existing file that located at D:\SimpleSolution\Document.pdf

package dev.simplesolution;

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PageMode;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;

public class AddBookmarkExistingPdfDocument {
	
	public static void main(String[] args) {
		try (PDDocument document = PDDocument.load(new File("D:\\SimpleSolution\\Document.pdf"))) {
			PDDocumentOutline documentOutline = new PDDocumentOutline();
			document.getDocumentCatalog().setDocumentOutline(documentOutline);
			PDOutlineItem pagesOutline = new PDOutlineItem();
			pagesOutline.setTitle("All Pages");
			documentOutline.addLast(pagesOutline);
			
			for(int i = 0; i < document.getNumberOfPages(); i++) {
				PDPageDestination pageDestination = new PDPageFitWidthDestination();
				pageDestination.setPage(document.getPage(i));
				
				PDOutlineItem bookmark = new PDOutlineItem();
				bookmark.setDestination(pageDestination);
				bookmark.setTitle("Document Page " + (i + 1));
				pagesOutline.addLast(bookmark);
			}

			pagesOutline.openNode();
			documentOutline.openNode();
			
			document.getDocumentCatalog().setPageMode(PageMode.USE_OUTLINES);

			document.save("D:\\SimpleSolution\\Document.pdf");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

Download Source Code

The source code in this article can be found at: github.com/simplesolutiondev/ApachePDFBoxAddBookmarkPdfDocument

or download at:

Download Source Code

Happy Coding 😊

Creating PDF Document File in Java using Apache PDFBox

Insert Image to PDF Document in Java using Apache PDFBox

Apache PDFBox Adding Metadata to PDF Document in Java

Creating PDF Document Page Labels in Java with Apache PDFBox