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.
Java - Replace Bookmark Content In Word
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
  1. import com.spire.doc.Document;
  2. import com.spire.doc.documents.*;
  3. import com.spire.doc.FileFormat;
  4. public class ReplaceBookmarkWithText {
  5. public static void main(String[] args) {
  6. //Load the template document
  7. Document doc = new Document("C:\\Users\\Administrator\\Desktop\\template.docx");
  8. //Locate the bookmark "bookmark_name"
  9. BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
  10. bookmarkNavigator.moveToBookmark("bookmark_name");
  11. //Replace the bookmark content with text
  12. bookmarkNavigator.replaceBookmarkContent("Daniel Asus", false);
  13. //Remove the current bookmark so that the bookmark symbol won't be displayed in the resulting document
  14. doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
  15. //Replace the content side bookmark "bookmark_tel"
  16. bookmarkNavigator = new BookmarksNavigator(doc);
  17. bookmarkNavigator.moveToBookmark("bookmark_tel");
  18. bookmarkNavigator.replaceBookmarkContent("698-911-2316", false);
  19. doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
  20. //Replace the content side bookmark "bookmark_email"
  21. bookmarkNavigator = new BookmarksNavigator(doc);
  22. bookmarkNavigator.moveToBookmark("bookmark_email");
  23. bookmarkNavigator.replaceBookmarkContent("[email protected]", false);
  24. doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
  25. //Replace the content side bookmark "bookmark_add"
  26. bookmarkNavigator = new BookmarksNavigator(doc);
  27. bookmarkNavigator.moveToBookmark("bookmark_add");
  28. bookmarkNavigator.replaceBookmarkContent("102 INC Road, St. Paul, NY 15585", false);
  29. doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
  30. //Save the document
  31. doc.saveToFile("ReplaceBookmarkWithText.docx", FileFormat.Docx);
  32. }
  33. }
Output
Java - Replace Bookmark Content In Word
Part 2. Replace bookmark content with an image
  1. import com.spire.doc.Document;
  2. import com.spire.doc.documents.*;
  3. import com.spire.doc.FileFormat;
  4. import com.spire.doc.ShapeHorizontalAlignment;
  5. import com.spire.doc.ShapeVerticalAlignment;
  6. import com.spire.doc.fields.DocPicture;
  7. import com.spire.doc.interfaces.IParagraphBase;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.InputStream;
  11. public class ReplaceBookmarkWithImage {
  12. public static void main(String[] args) throws FileNotFoundException {
  13. //Load the document generated in part 1
  14. Document doc = new Document("ReplaceBookmarkWithText.docx");
  15. //Locate the bookmark "bookmark_photo"
  16. BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
  17. bookmarkNavigator.moveToBookmark("bookmark_photo");
  18. //Delete the bookmark content
  19. bookmarkNavigator.deleteBookmarkContent(false);
  20. //Create a Paragraph object
  21. Paragraph para = new Paragraph(doc);
  22. //Append a picture to the paragraph
  23. String picPath = "C:\\Users\\Administrator\\Desktop\\portrait.jpeg";
  24. InputStream inputStream = new FileInputStream(picPath);
  25. DocPicture picture = para.appendPicture(inputStream);
  26. //Set the height and width of the picture
  27. picture.setHeight(120f);
  28. picture.setWidth(90f);
  29. //Set the horizontal and vertical alignment
  30. picture.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
  31. picture.setVerticalAlignment(ShapeVerticalAlignment.Center);
  32. //Set text wrapping style
  33. picture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
  34. //Insert the paragraph to the bookmark
  35. bookmarkNavigator.insertParagraph(para);
  36. //Remove the current bookmark so that the bookmark symbol won't be displayed in the resulting document
  37. doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
  38. //Save the document
  39. doc.saveToFile("ReplaceBookmarkWithImage.docx", FileFormat.Docx);
  40. }
  41. }
Output
Java - Replace Bookmark Content In Word

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!