This article demonstrates how to edit the existing bookmarks in a PDF file, for example, change the bookmark title, font color and text style using Spire.PDF for Java.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.bookmarks.PdfBookmark;
import com.spire.pdf.bookmarks.PdfTextStyle;
import com.spire.pdf.graphics.PdfRGBColor;
import java.awt.*;
public class EditBookmarks {
public static void main(String[] args) {
//Create a PdfDocument instance
PdfDocument doc = new PdfDocument();
//Load the PDF file
doc.loadFromFile("Bookmarks.pdf");
//Get the first bookmark
PdfBookmark bookmark = doc.getBookmarks().get(0);
//Change the title of the bookmark
bookmark.setTitle("New Title");
//Change the font color of the bookmark
bookmark.setColor(new PdfRGBColor(new Color(255,0,0)));
//Change the outline text style of the bookmark
bookmark.setDisplayStyle(PdfTextStyle.Italic);
//Edit child bookmarks of the first bookmark
for (PdfBookmark childBookmark : (Iterable<PdfBookmark>) bookmark {
childBookmark.setColor(new PdfRGBColor(new Color(0,0,255)));
childBookmark.setDisplayStyle(PdfTextStyle.Bold);
for (PdfBookmark childBookmark2 : (Iterable<PdfBookmark>) bookmark {
childBookmark2.setColor(new PdfRGBColor(new Color(160,160,122)) );
childBookmark2.setDisplayStyle(PdfTextStyle.Bold);
}
}
//Save the result file
doc.saveToFile("EditBookmarks.pdf");
doc.close();
}
}
Output:

