This article will show you have to replace the content of a bookmark in Word using Spire.Doc for Java.
Replace bookmark content with text
import com.spire.doc.*; import com.spire.doc.documents.*; public class ReplaceBookmark { public static void main(String[] args){ //load the Word document Document doc = new Document("Bookmark.docx"); //locate the bookmark."SimpleBookmark" BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc); bookmarkNavigator.moveToBookmark("SimpleBookmark"); //replace the content of the bookmark with text bookmarkNavigator.replaceBookmarkContent("This is new content", false); //save the resultant document doc.saveToFile("ReplaceWithText.docx", FileFormat.Docx); } }
Replace bookmark content with HTML string
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.ParagraphBase; public class ReplaceBookmark { public static void main(String[] args){ //load the Word document Document doc = new Document("Bookmark.docx"); //add a temp section Section tempSection = doc.addSection(); //add a paragraph and append Html string String html = "This Bookmark has been edited
"; tempSection.addParagraph().appendHTML(html); //Get the first item and the last item of the paragraph ParagraphBase firstItem = (ParagraphBase)tempSection.getParagraphs().get(0).getItems().getFirstItem(); ParagraphBase lastItem = (ParagraphBase)tempSection.getParagraphs().get(0).getItems().getLastItem(); //create a TextBodySelection object TextBodySelection selection = new TextBodySelection(firstItem, lastItem); //create a TextBodyPart object TextBodyPart bodyPart = new TextBodyPart(selection); //locate the bookmark "SimpleBookmark" BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc); bookmarkNavigator.moveToBookmark("SimpleBookmark"); //replace the content of the bookmark with Html string bookmarkNavigator.replaceBookmarkContent(bodyPart); //remove the temp section doc.getSections().remove(tempSection); //save the resultant document doc.saveToFile("ReplaceWithHTMLString.docx", FileFormat.Docx); } }
Replace bookmark content with table
import com.spire.doc.*; import com.spire.doc.documents.*; public class ReplaceBookmark { public static void main(String[] args){ //load the Word document Document doc = new Document("Bookmark.docx"); String[][] data = { new String[]{"Name", "Capital", "Continent", "Area"}, new String[]{"Bolivia", "La Paz", "South America", "1098575"}, new String[]{"Brazil", "Brasilia", "South America", "8511196"}, new String[]{"Canada", "Ottawa", "North America", "9976147"}, new String[]{"Chile", "Santiago", "South America", "756943"}, }; //create a table Table table = new Table(doc,true); table.resetCells(5, 4); for (int i = 0; i < data.length; i++) { TableRow dataRow = table.getRows().get(i); for (int j = 0; j < data[i].length; j++) { dataRow.getCells().get(j).addParagraph().appendText(data[i][j]); } } //create a TextBodyPart object TextBodyPart bodyPart= new TextBodyPart(doc); bodyPart.getBodyItems().add(table); //locate the bookmark "SimpleBookmark" BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc); bookmarkNavigator.moveToBookmark("SimpleBookmark"); //replace the content of the bookmark with table bookmarkNavigator.replaceBookmarkContent(bodyPart); //save the resultant document doc.saveToFile("ReplaceWithTable.docx", FileFormat.Docx); } }