Java - Replace Bookmark Content In Word

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


Similar Articles