DEV Community

loading...

Add and Delete Bookmarks in PDF in Java

eiceblue profile image E-iceblue Product Family ・3 min read

A PDF bookmark is a type of link with representative text which allows users to quickly navigate to a specific place without scrolling the pages one by one. In this article, we will show you how to add and delete bookmarks in a PDF document in Java using Free Spire.PDF for Java library.

Add Dependencies

First of all, you need to add needed dependencies for including Free Spire.PDF for Java into your Java project. There are two ways to do that.

If you use maven, you need to add the following code to your project’s pom.xml file.

<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>2.6.3</version>  
    </dependency>  
</dependencies>

For non-maven projects, download Free Spire.PDF for Java pack from this website and add Spire.Pdf.jar in the lib folder into your project as a dependency.

Add bookmarks

Free Spire.PDF for Java allows adding bookmarks and child bookmarks to a new or an existing PDF file. The following example shows how to add a bookmark and 2 child bookmarks to an existing 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("Input.pdf");

        //Get the first page
        PdfPageBase page = pdf.getPages().get(0);
        //Add bookmark
        PdfBookmark bookmark = pdf.getBookmarks().add("1. Cacor corporation");
        //Set bookmark's 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 Direct Sighting Compass");
        //Set child bookmark's 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);

        //Add child bookmark
        childBookmark = bookmark.add("1.2 Dive Computer");
        //Set child bookmark's destination page and location
        childDestination = new PdfDestination(page, new Point2D.Float(0, 150));
        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("AddBookmarks.pdf");
    }
}

The output PDF file:
Add bookmarks

Delete bookmarks

When deleting bookmarks, you can either delete a specific child bookmark, a parent bookmark along with its child bookmark(s) or delete all the bookmarks from a PDF file, the example code is given below.

import com.spire.pdf.PdfDocument;

public class DeleteBookmark {
    public static void main(String[] args) {
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load the PDF file
        pdf.loadFromFile("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");
    }
}

The output PDF file after deleting the first child bookmark:
Delete bookmarks

Discussion (0)

pic
Editor guide