Most of the time, we use bookmarks in Word to help us locate certain points or a part of text. For programmers, bookmarks can also be used as "placeholders". Simply we can update these "placeholders", that is to replace the bookmark content, to quickly generate documents based on a pre-designed Word template.
In this article, you’ll learn how to replace bookmark content with text or images by using Free Spire.Doc for Java, which is a library for processing Word files from Java code.
Adding Spire.Doc.jar as a dependency
First of all, donwload Free Spire.Doc for Java 2.7.3. Unzip it, and you’ll get the jar file from the ‘bin’ folder.
Then, create a Java application in your IDE. Import the jar file in your project as a dependency.
Creating a template
As is shown in the following screenshot, the Word template contains several bookmarks, namely ‘bookmark_name’, ‘bookmark_add’, ‘bookmark_photo’, etc. These bookmark names are essential for us to access the specific bookmark through code. Therefore, you'd better make these names meaningful and easy to distinguish.

Now, let’s start to code.
Using the code
Spire.Doc provides a BookmarksNavigator class, allowing programmers to easily locate a specific bookmark in a document and modify the content within it. The following code snippets demonstrate how to replace bookmark content with text or pictures.
Part 1. Replace bookmark content with text
- import com.spire.doc.Document;
- import com.spire.doc.documents.*;
- import com.spire.doc.FileFormat;
- public class ReplaceBookmarkWithText {
- public static void main(String[] args) {
- //Load the template document
- Document doc = new Document("C:\\Users\\Administrator\\Desktop\\template.docx");
- //Locate the bookmark "bookmark_name"
- BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
- bookmarkNavigator.moveToBookmark("bookmark_name");
- //Replace the bookmark content with text
- bookmarkNavigator.replaceBookmarkContent("Daniel Asus", false);
- //Remove the current bookmark so that the bookmark symbol won't be displayed in the resulting document
- doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
- //Replace the content side bookmark "bookmark_tel"
- bookmarkNavigator = new BookmarksNavigator(doc);
- bookmarkNavigator.moveToBookmark("bookmark_tel");
- bookmarkNavigator.replaceBookmarkContent("698-911-2316", false);
- doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
- //Replace the content side bookmark "bookmark_email"
- bookmarkNavigator = new BookmarksNavigator(doc);
- bookmarkNavigator.moveToBookmark("bookmark_email");
- bookmarkNavigator.replaceBookmarkContent("[email protected]", false);
- doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
- //Replace the content side bookmark "bookmark_add"
- bookmarkNavigator = new BookmarksNavigator(doc);
- bookmarkNavigator.moveToBookmark("bookmark_add");
- bookmarkNavigator.replaceBookmarkContent("102 INC Road, St. Paul, NY 15585", false);
- doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
- //Save the document
- doc.saveToFile("ReplaceBookmarkWithText.docx", FileFormat.Docx);
- }
- }



Join the conversation! Your thoughts help the community grow.