Add, modify, delete JAVA PDF bookmarks

If you want to read a longer PDF file, insert a bookmark so that you can reach the previous position quickly in the previous case. In addition, existing bookmarks can be changed or deleted as needed. The text chapter will show you how to attach, modify and delete PDF bookmarks through Java programming.

** Using tools: **

--Free Spire.Pdf for Java 2.4.4 (no cost version)

** Jar package introduced: ** : clap: ** Method 1: ** First, get Free Spire.PDF for Java from Official Site After that, release the compression. The following is a method to easily open the Project Structure interface in IDEA. As shown in the figure below: image.png After that, follow the procedure below. (1) Select "Modules"-"Dependencies" and add the external jar package. (2) Enter the "Attach File or Directores" screen, select the path of the jar file, and click "OK". As shown in the following figure: image.png image.png : clap: ** Method 2: ** Place the jar package using Maven. You can refer to Installation Method. I will. ** Test documentation: ** image.png

JAVA code reference

1. Add bookmark

import com.spire.pdf.*;
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 bookmark {
    public static void main(String[] args) throws Exception{
             //Create a pdf document. 
            PdfDocument doc = new PdfDocument();
            //Load PDF file from the disk
            doc.loadFromFile("data/SampleEn.pdf");
            //Get the third page 
            PdfPageBase page = doc.getPages().get(2);
            //Add the bookmark 
            PdfBookmark bookmark = doc.getBookmarks().add("3rd page");
            //Set the position-At the start of the page 
            PdfDestination bookmarkLocation = new PdfDestination(page, new Point2D.Float(0 , 0));
            bookmark.setAction(new PdfGoToAction(bookmarkLocation)); 
            //Set the style of bookmark
            bookmark.setColor(new PdfRGBColor(Color.BLUE));
            bookmark.setDisplayStyle(PdfTextStyle.Bold);
            PdfPageBase page1 = doc.getPages().get(3);
            PdfBookmark bookmark1 = doc.getBookmarks().add("4th page");
            PdfDestination bookmarkLocation1 = new PdfDestination(page1, new Point2D.Float(0 , 0));
            bookmark1.setAction(new PdfGoToAction(bookmarkLocation1));
            bookmark1.setColor(new PdfRGBColor(Color.RED));
            bookmark1.setDisplayStyle(PdfTextStyle.Bold);
            //Save pdf file. 
        doc.saveToFile("output/bookmarks.pdf");
        doc.close();
        }
    }

** Bookmark addition effect: ** image.png

2. Rewrite the bookmark

import com.spire.pdf.PdfDocument;
import com.spire.pdf.bookmarks.*;
import com.spire.pdf.graphics.PdfRGBColor;
import java.awt.*;

public class updateBookmark {
    public static void main(String[] args) {
        //Create the PDF
        PdfDocument doc = new PdfDocument();
        //Load PDF file from the disk
        doc.loadFromFile("output/bookmarks.pdf");
        //Get the first bookmark 
        PdfBookmark bookmark = doc.getBookmarks().get(0);
        //Change the title of the bookmark 
        bookmark.setTitle("Modified bookmarks");
        //Set the color of the bookmark 
        bookmark.setColor(new PdfRGBColor(Color.black));
        //Set the outline text style of the bookmark 
        bookmark.setDisplayStyle(PdfTextStyle.Bold);
        //Save the file 
        doc.saveToFile("output/updateBookmark.pdf");
        doc.close();
    }
}

** Change bookmark effect: ** image.png

3. Delete the bookmark

import com.spire.pdf.PdfDocument;

public class deleteBookmark {
    public static void main(String[] args) {
        //Create the PDF
        PdfDocument doc = new PdfDocument();
        //Load PDF file from the disk
        doc.loadFromFile("output/bookmarks.pdf");
        //removeAt() to delete the bookmark
        doc.getBookmarks().removeAt(0);
        //Save the file
        doc.saveToFile("output/deleteBookmark.pdf");
        doc.close();
    }
}

** Bookmark deletion effect: ** image.png ** (The whole sentence ends) **

Recommended Posts

Add, modify, delete JAVA PDF bookmarks
Add, replace, delete Java PDF images
Java (add2)
Java (add)
Add watermark to Java to PDF document
Add, read, and delete Excel comments in Java
[Java] Convert PDF version
[Java] PDF viewing settings
[Java] Initialize, add, get
java: Add date [Note]
Java adds table to PDF
Save Java PDF in Excel
Java encryption and decryption PDF
Paging PDF with Java + PDFBox.jar
Java turns Excel into PDF
Save Java HTML as PDF