Knowledgebase (2311)
Children categories
A bookmark in a PDF document consists of formatted text linking to a specific section of the document. Readers can navigate through pages by simply clicking on the bookmarks displayed on the side of the page instead of scrolling up and down, which is very helpful for those huge documents. Moreover, well-organized bookmarks can also serve as contents. When you create a PDF document with a lot of pages, it’s better to add bookmarks to link to significant content. This article is going to show how to add, modify, and remove bookmarks in PDF documents using Spire.PDF for Java through programming.
- Add Bookmarks to a PDF Document
- Edit Bookmarks in a PDF Document
- Delete Bookmarks from a PDF Document
Install Spire.PDF for Java
First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.12.16</version>
</dependency>
</dependencies>
Add Bookmarks to a PDF Document
Spire.PDF for Java provides PdfDocument.getBookmarks().add() method to add bookmarks to a PDF document. In addition to adding primary bookmarks, we can use PdfBookmark.add() method to add a sub-bookmark to a primary bookmark. There are also many other methods under PdfBookmark class which are used to set the destination, text color, and text style of bookmarks. The detailed steps of adding bookmarks to a PDF document are as follows.
- Create a PdfDocument class instance.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Loop through the pages in the PDF document to add bookmarks and set their styles.
- Add a primary bookmark to the document using PdfDocument.getBookmarks().add() method.
- Create a PdfDestination class object and set the destination of the primary bookmark using PdfBookmark.setAction() method.
- Set the text color of the primary bookmark using PdfBookmark.setColor() method.
- Set the text style of the Primary bookmark using PdfBookmark.setDisplayStyle() method.
- Add a sub-bookmark to the primary bookmark using PdfBookmark.add() method.
- Use the above methods to set the destination, text color, and text style of the sub-bookmark.
- Save the document using PdfDocument.saveToFile() method.
- Java
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 class instance
PdfDocument pdf = new PdfDocument();
//Load a PDF file
pdf.loadFromFile("There's No Planet B.pdf");
//Loop through the pages in the PDF file
for(int i = 0; i< pdf.getPages().getCount();i++) {
PdfPageBase page = pdf.getPages().get(i);
//Add a bookmark
PdfBookmark bookmark = pdf.getBookmarks().add(String.format("Bookmark-%s", i + 1));
//Set the destination page and location
PdfDestination destination = new PdfDestination(page, new Point2D.Float(0, 0));
bookmark.setAction(new PdfGoToAction(destination));
//Set the text color
bookmark.setColor(new PdfRGBColor(new Color(139, 69, 19)));
//Set the text style
bookmark.setDisplayStyle(PdfTextStyle.Bold);
//Add a child bookmark
PdfBookmark childBookmark = bookmark.add(String.format("Sub-Bookmark-%s", i + 1));
//Set the destination page and location
PdfDestination childDestination = new PdfDestination(page, new Point2D.Float(0, 100));
childBookmark.setAction(new PdfGoToAction(childDestination));
//Set the text color
childBookmark.setColor(new PdfRGBColor(new Color(255, 127, 80)));
//Set the text style
childBookmark.setDisplayStyle(PdfTextStyle.Italic);
}
//Save the result file
pdf.saveToFile("AddBookmarks.pdf");
}
}

Edit Bookmarks in a PDF Document
We can also use methods of PdfBookmark class in Spire.PDF for Java to edit existing PDF bookmarks. The detailed steps are as follows.
- Create a PdfDocument class instance.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Get the first bookmark using PdfDocument.getBookmarks().get() method.
- Change the title of the bookmark using PdfBookmark.setTitle() method.
- Change the font color of the bookmark using PdfBookmark.setColor() method.
- Change the outline text style of the bookmark using PdfBookmark.setDisplayStyle() method.
- Change the text color and style of the sub-bookmark using the above methods.
- Save the document using PdfDocument.saveToFile() method.
- 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 class instance
PdfDocument doc = new PdfDocument();
//Load a PDF file
doc.loadFromFile("AddBookmarks.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 sub-bookmarks of the first bookmark
for (Object Bookmark : (Iterable) bookmark) {
PdfBookmark childBookmark=(PdfBookmark)Bookmark;
childBookmark.setColor(new PdfRGBColor(new Color(0,0,255)));
childBookmark.setDisplayStyle(PdfTextStyle.Bold);
}
//Save the result file
doc.saveToFile("EditBookmarks.pdf");
doc.close();
}
}

Delete Bookmarks from a PDF Document
We can use Spire.PDF for Java to delete any bookmark in a PDF document. PdfDocument.getBookmarks().removeAt() is used to remove a specific primary bookmark, PdfDocument.getBookmarks().clear() method is used to remove all bookmarks, and PdfBookmark.removeAt() method is used to remove a specific sub-bookmark of a primary bookmark. The detailed steps of removing bookmarks form a PDF document are as follows.
- Create PdfDocument class instance.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Get the first bookmark using PdfDocument.getBookmarks().get() method.
- Remove the sub-bookmark of the first bookmark using PdfBookmark.removeAt() method.
- Save the document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.bookmarks.PdfBookmark;
public class deleteBookmarks {
public static void main(String[] args) {
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load the PDF file
pdf.loadFromFile("AddBookmarks.pdf");
//Get the first bookmark
PdfBookmark pdfBookmark = pdf.getBookmarks().get(0);
//Delete the sub-bookmark of the first bookmark
pdfBookmark.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");
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
This article demonstrates how to apply a border around a set of charaters and how to apply a border around a whole paragraph, by using Spire.Doc for Java.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BorderStyle;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class AddBorders {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();
//Add a section
Section section = doc.addSection();
//Add a border to a set of characters
Paragraph para = section.addParagraph();
TextRange tr = para.appendText("Spire.Doc for Java");
tr.getCharacterFormat().getBorder().setBorderType(BorderStyle.Single);
tr.getCharacterFormat().getBorder().setColor(Color.BLACK);
String text = " is a professional Java library specifically designed for developers to create, read, " +
"write, convert and print Word document files on Java platform.";
para.appendText(text);
para.appendBreak(BreakType.Line_Break);
//Add a border to a paragraph
para = section.addParagraph();
String text2 = "A plenty of Word document processing tasks can be performed by Spire.Doc for Java, such as " +
"creating, reading, editing, converting and printing Word documents." ;
para.appendText(text2);
para.getFormat().getBorders().setBorderType(BorderStyle.Single);
para.getFormat().getBorders().setColor(Color.BLACK);
//Save the document
doc.saveToFile("AddBorder.docx", FileFormat.Docx_2013);
}
}

Form fields are often used in PDF documents to collect information from users. In some cases, you may need to flatten form fields in a PDF. For instance, when you want to prevent other viewers from editing the information you entered in the form fields of a PDF questionnaire. This article will introduce how to flatten form fields in PDF in Java using Spire.PDF for Java.
Install Spire.PDF for Java
First of all, you need to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.12.16</version>
</dependency>
</dependencies>
Flatten a Specific Form Field in PDF in Java
The following are the steps to flatten a specific form field in a PDF document using Spire.PDF for Java:
- Initialize an instance of PdfDocument class.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Get the form widget collection from the document.
- Get a specific form field from the widget collection by its name or index through PdfFormWidget.getFieldsWidget().get() method.
- Flatten the form field through PdfField.setFlatten() method.
- Save the result document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.PdfFormWidget;
public class FlattenSpecificFormField {
public static void main(String[] args){
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.loadFromFile("Form.pdf");
//Get the form widget collection
PdfFormWidget formWidget = (PdfFormWidget)pdf.getForm();
//Get a specific form field by its name
PdfField form = formWidget.getFieldsWidget().get("Address");
//Get a specific form field by its index
//PdfField form = formWidget.getFieldsWidget().get(2);
//Flatten the form
form.setFlatten(true);
//Save the result document
pdf.saveToFile("FlattenSpecific.pdf");
}
}

Flatten All Form Fields in PDF in Java
The following are the steps to flatten all the form fields in a PDF document using Spire.PDF for Java:
- Initialize an instance of PdfDocument class.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Flatten all the form fields in the document through PdfDocument.getForm().isFlatten() method.
- Save the result document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument;
public class FlattenAllFormFields {
public static void main(String[] args){
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.loadFromFile("Form.pdf");
//Flatten all the forms in the document
pdf.getForm().isFlatten(true);
//Save the result document
pdf.saveToFile("FlattenAll.pdf");
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.