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);
- }
- }
Output

Part 2. Replace bookmark content with an image
- import com.spire.doc.Document;
- import com.spire.doc.documents.*;
- import com.spire.doc.FileFormat;
- import com.spire.doc.ShapeHorizontalAlignment;
- import com.spire.doc.ShapeVerticalAlignment;
- import com.spire.doc.fields.DocPicture;
- import com.spire.doc.interfaces.IParagraphBase;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.InputStream;
- public class ReplaceBookmarkWithImage {
- public static void main(String[] args) throws FileNotFoundException {
- //Load the document generated in part 1
- Document doc = new Document("ReplaceBookmarkWithText.docx");
- //Locate the bookmark "bookmark_photo"
- BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
- bookmarkNavigator.moveToBookmark("bookmark_photo");
- //Delete the bookmark content
- bookmarkNavigator.deleteBookmarkContent(false);
- //Create a Paragraph object
- Paragraph para = new Paragraph(doc);
- //Append a picture to the paragraph
- String picPath = "C:\\Users\\Administrator\\Desktop\\portrait.jpeg";
- InputStream inputStream = new FileInputStream(picPath);
- DocPicture picture = para.appendPicture(inputStream);
- //Set the height and width of the picture
- picture.setHeight(120f);
- picture.setWidth(90f);
- //Set the horizontal and vertical alignment
- picture.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
- picture.setVerticalAlignment(ShapeVerticalAlignment.Center);
- //Set text wrapping style
- picture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
- //Insert the paragraph to the bookmark
- bookmarkNavigator.insertParagraph(para);
- //Remove the current bookmark so that the bookmark symbol won't be displayed in the resulting document
- doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
- //Save the document
- doc.saveToFile("ReplaceBookmarkWithImage.docx", FileFormat.Docx);
- }
- }
Output

Conclusion
If you have a requirement to dynamically fill in data in a Word document, replacing bookmark content is an easy and efficient way. With the help of Spire.Doc library, not only can you modify an existing Word document, but you can also send the resulting document to a printer or save as popular file formats like PDF, image, etc.
Happy coding!

Join the conversation! Your thoughts help the community grow.