Java sample code for adding, modifying and deleting PDF bookmarks

Time:2020-2-15

When reading a long PDF document, in order to facilitate us to quickly locate the last reading position when we read it again, we can insert a bookmark to mark; in addition, for the existing bookmarks in the document, we can also modify or delete them as needed. This article uses Java programming to show how to add, modify, and delete PDF bookmarks.

Tool use:

• free flame.pdf for Java 2.4.4 (free version)
•IntelliJ IDEA

Jar package import:

Method 1: first, extract free fire.pdf for Java from the official website, and then extract it. Here are three ways to quickly open the project structure interface in idea, as shown in the following figure:

Then, follow the steps below: ① select “modules” – “dependencies” to add the external jar package; ② enter the “attach file or directories” interface to select the jar file path and click “OK”; ③ check the jar path option and click “OK” / “apply”; ④ import is completed. The following picture:

Mode 2: use Maven to configure the package. You can refer to the import method.

Test documentation:

Java code example reference:

1. 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 PDF instance
      PdfDocument doc = new PdfDocument();
      //Loading PDF files from the system
      doc.loadFromFile("data/Sample.pdf");
      PdfPageBase page = doc.getPages().get(2);
       //Add Bookmark
      Pdfbookmark bookmark = doc. Getbookmarks(). Add ("the third page");
      //Get position, beginning of page number
      PdfDestination bookmarkLocation = new PdfDestination(page, new Point2D.Float(0 , 0));
      bookmark.setAction(new PdfGoToAction(bookmarkLocation));
      //Bookmark style
      bookmark.setColor(new PdfRGBColor(Color.BLUE));
      bookmark.setDisplayStyle(PdfTextStyle.Bold);
      PdfPageBase page1 = doc.getPages().get(3);
      Pdfbookmark bookmark1 = doc. Getbookmarks(). Add ("fourth 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 adding effect:

2. Modify 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 PDF instance
    PdfDocument doc = new PdfDocument();
     //Loading PDF files from the system
    doc.loadFromFile("output/bookmarks.pdf");
    //Get first bookmark
    PdfBookmark bookmark = doc.getBookmarks().get(0);
    //Change bookmark
    Bookmark.settitle ("updated Bookmarks");
    //Set bookmark color
    bookmark.setColor(new PdfRGBColor(Color.black));
    //Set bookmark Text Bold
    bookmark.setDisplayStyle(PdfTextStyle.Bold);
    //Save file
    doc.saveToFile("output/updateBookmark.pdf");
    doc.close();
  }
}

Bookmark modification effect:

3. Delete bookmark

import com.spire.pdf.PdfDocument;
public class deleteBookmark {
  public static void main(String[] args) {
     //Create PDF instance
    PdfDocument doc = new PdfDocument();
     //Loading PDF files from the system
    doc.loadFromFile("output/bookmarks.pdf");
    //Removeat() method to remove bookmarks
    doc.getBookmarks().removeAt(0);
     //Save file
    doc.saveToFile("output/deleteBookmark.pdf");
    doc.close();
  }
}

Bookmark delete effect:

summary

The above is the sample code of adding, modifying and deleting PDF bookmarks for Java introduced by Xiaobian. I hope it can help you. If you have any questions, please leave a message to me, and Xiaobian will reply to you in time. Thank you very much for your support of the developepaer website!