How to Add/Delete Bookmarks in a PDF File Using Java

Andrew Wilson
2 min readSep 28, 2021

Adding bookmarks to PDF files is very useful especially when you are reading some research papers or eBooks that contain a large number of pages. After adding bookmarks, you can jump to a specific page or part of a PDF file quickly and easily. Now this article will demonstrate how to use add bookmarks to a PDF file and delete the existing PDF bookmarks in a PDF file using Free Spire.PDF for Java

Import JAR Dependency (2 Methods)

● Download the Free Spire.PDF for Java and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.

● Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>4.4.1</version>
</dependency>
</dependencies>

Add Bookmarks to PDF

The following sample code will demonstrate how to add a bookmark and a child bookmark to a PDF file.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.actions.PdfGoToAction;
import com.spire.pdf.bookmarks.PdfBookmark;
import com.spire.pdf.bookmarks.PdfTextStyle;
import com.spire.pdf.general.PdfDestination;
import com.spire.pdf.graphics.PdfRGBColor;

import java.awt.*;
import java.awt.geom.Point2D;

public class AddBookmark {
public static void main(String[] args) {
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a PDF file
pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\Travel.pdf");

//Get the 8th page
PdfPageBase page = pdf.getPages().get(7);
//Add bookmark
PdfBookmark bookmark = pdf.getBookmarks().add("1. DeadWood--a place for fun.");
//Set destination page and location
PdfDestination destination = new PdfDestination(page, new Point2D.Float(0, 0));
bookmark.setAction(new PdfGoToAction(destination));
//Set text color
bookmark.setColor(new PdfRGBColor(new Color(139, 69, 19)));
//Set text style
bookmark.setDisplayStyle(PdfTextStyle.Bold);

//Add child bookmark
PdfBookmark childBookmark = bookmark.add("1.1 Gaming halls, saloons, shops, live entertainment.");
//Set destination page and location
PdfDestination childDestination = new PdfDestination(page, new Point2D.Float(0, 100));
childBookmark.setAction(new PdfGoToAction(childDestination));
//Set text color
childBookmark.setColor(new PdfRGBColor(new Color(255, 127, 80)));
//Set text style
childBookmark.setDisplayStyle(PdfTextStyle.Italic);

//Save the result file
pdf.saveToFile("out/AddBookmarks.pdf");
}
}

Delete Bookmarks in PDF

The following sample code will demonstrate how to delete a specific child bookmark in a PDF file. Beyond that, Free Spire.PDF for Java also supports deleting a parent bookmark along with its child bookmark(s) or delete all the bookmarks from a PDF file.

import com.spire.pdf.PdfDocument;

public class DeleteBookmarks {
public static void main(String[] args) {
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load the PDF file
pdf.loadFromFile("out/AddBookmarks.pdf");

//Delete the first child bookmark
pdf.getBookmarks().get(0).removeAt(0);

//Delete the first bookmark along with its child bookmark
//pdf.getBookmarks().removeAt(0);

//Delete all the bookmarks
//pdf.getBookmarks().clear();

//Save the result file
pdf.saveToFile("DeleteBookmarks.pdf");
}
}

--

--

Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.