Java: Add or Remove Bookmarks in Word

Bookmarks in Microsoft Word can mark text, pictures, and places in a document, allowing you to jump straight to the desired text, picture, or place without scrolling through several paragraphs or pages. This is especially useful for navigating some research papers or contracts that contain a lot of pages. In this article, you will learn how to programmatically add or remove a bookmark in a Word document using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.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.doc</artifactId>
        <version>12.11.0</version>
    </dependency>
</dependencies>
    

Add a Bookmarks to an Existing Word Document

The detailed steps are as follows:

  • Create a Document instance.
  • Load a sample Word document using Document.loadFromFile() method.
  • Get the first section using Document.getSections().get() method.
  • Get a specified paragraph using Section.getParagraphs().get() method.
  • Append the start of the bookmark with specified name to the specified paragraph using Paragraph.appendBookmarkStart(java.lang.String name) method.
  • Append the end of the bookmark with specified name to the specified paragraph using Paragraph.appendBookmarkEnd(java.lang.String name) method.
  • Save the document to another file using Document. saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;

public class InsertBookmark {

    public static void main(String[] args) {

        //Create a Document instance
        Document doc = new Document();

        //Load a sample Word file
        doc.loadFromFile("sample.docx");

        //Get the first section
        Section section = doc.getSections().get(0);

        //Insert a bookmark with specified name into the specified paragraphs
        section.getParagraphs().get(7).appendBookmarkStart("ConversionFunction");
        section.getParagraphs().get(16).appendBookmarkEnd("ConversionFunction");


        //Save the document
        doc.saveToFile("AddBookmark.docx", FileFormat.Docx_2013);
    }
}

Java: Add or Remove Bookmarks in Word

Remove an Existing Bookmark in a Word Document

The detailed steps are as follows:

  • Create a Document instance.
  • Load a sample Word document using Document.loadFromFile() method.
  • Get a specified bookmark by its index using Document.getBookmarks().get() method.
  • Remove the specified bookmark using Document.getBookmarks().remove() method.
  • Save the document to another file using Document.saveToFile() method.
  • Java
import com.spire.doc.Bookmark;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class RemoveBookmark {

    public static void main(String[] args) {

        //Create a Document instance
        Document doc = new Document();

        //Load a sample Word file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\AddBookmark.docx");

        //Get bookmark by its index
        Bookmark bookmark = doc.getBookmarks().get(0);

        //Remove the bookmark
        doc.getBookmarks().remove(bookmark);

        //Save the document.
        doc.saveToFile("RemoveBookmark.docx", FileFormat.Docx);
    }
}

Java: Add or Remove Bookmarks in Word

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.