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
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) {
-
-
- Document doc = new Document("C:\\Users\\Administrator\\Desktop\\template.docx");
-
-
- BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
- bookmarkNavigator.moveToBookmark("bookmark_name");
-
-
- bookmarkNavigator.replaceBookmarkContent("Daniel Asus", false);
-
-
- doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
-
-
- bookmarkNavigator = new BookmarksNavigator(doc);
- bookmarkNavigator.moveToBookmark("bookmark_tel");
- bookmarkNavigator.replaceBookmarkContent("698-911-2316", false);
- doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
-
-
- bookmarkNavigator = new BookmarksNavigator(doc);
- bookmarkNavigator.moveToBookmark("bookmark_email");
- bookmarkNavigator.replaceBookmarkContent("[email protected]", false);
- doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
-
-
- bookmarkNavigator = new BookmarksNavigator(doc);
- bookmarkNavigator.moveToBookmark("bookmark_add");
- bookmarkNavigator.replaceBookmarkContent("102 INC Road, St. Paul, NY 15585", false);
- doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
-
-
- 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 {
-
-
- Document doc = new Document("ReplaceBookmarkWithText.docx");
-
-
- BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
- bookmarkNavigator.moveToBookmark("bookmark_photo");
-
-
- bookmarkNavigator.deleteBookmarkContent(false);
-
-
- Paragraph para = new Paragraph(doc);
-
-
- String picPath = "C:\\Users\\Administrator\\Desktop\\portrait.jpeg";
- InputStream inputStream = new FileInputStream(picPath);
- DocPicture picture = para.appendPicture(inputStream);
-
-
- picture.setHeight(120f);
- picture.setWidth(90f);
-
-
- picture.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
- picture.setVerticalAlignment(ShapeVerticalAlignment.Center);
-
-
- picture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
-
-
- bookmarkNavigator.insertParagraph(para);
-
-
- doc.getBookmarks().remove(bookmarkNavigator.getCurrentBookmark());
-
-
- 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!