Bookmarks (3)
This article demonstrates how to get the text inside a bookmark in a Word document using Spire.Doc for Java.
import com.spire.doc.Document; import com.spire.doc.documents.BookmarksNavigator; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.TextBodyPart; import com.spire.doc.fields.TextRange; import java.io.FileNotFoundException; import java.io.PrintWriter; public class GetBookmarkText { public static void main(String[] args) throws FileNotFoundException { //create a Document object Document doc = new Document(); //load a sample Word file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx"); //get the specific bookmark BookmarksNavigator navigator = new BookmarksNavigator(doc); navigator.moveToBookmark("MyBookmark"); //get the bookmark content TextBodyPart textBodyPart = navigator.getBookmarkContent(); //declare a String variable String text = ""; //loop through body items for (Object item : textBodyPart.getBodyItems()) { //determine if an item is a paragraph if (item instanceof Paragraph) { Paragraph paragraph = (Paragraph) item; //loop through the child objects of the paragraph for (Object childObj : paragraph.getChildObjects()) { //determine if a child object is a text range if (childObj instanceof TextRange) { //get text from the text range TextRange textRange = (TextRange) childObj; text = text + textRange.getText(); } } } } //write the bookmark text to a .txt file PrintWriter printWriter = new PrintWriter("output/BookmarkText.txt"); printWriter.println(text); printWriter.close(); } }
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); } }
Bookmarks in Microsoft Word can mark text, pictures, and places in a document, allowing you to jump straight to the desired text, picture, or place without scrolling through several paragraphs or pages. This is especially useful for navigating some research papers or contracts that contain a lot of pages. In this article, you will learn how to programmatically add or remove a bookmark in a Word document using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.3.1</version> </dependency> </dependencies>
Add a Bookmarks to an Existing Word Document
The detailed steps are as follows:
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Get the first section using Document.getSections().get() method.
- Get a specified paragraph using Section.getParagraphs().get() method.
- Append the start of the bookmark with specified name to the specified paragraph using Paragraph.appendBookmarkStart(java.lang.String name) method.
- Append the end of the bookmark with specified name to the specified paragraph using Paragraph.appendBookmarkEnd(java.lang.String name) method.
- Save the document to another file using Document. saveToFile() method.
- Java
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; public class InsertBookmark { public static void main(String[] args) { //Create a Document instance Document doc = new Document(); //Load a sample Word file doc.loadFromFile("sample.docx"); //Get the first section Section section = doc.getSections().get(0); //Insert a bookmark with specified name into the specified paragraphs section.getParagraphs().get(7).appendBookmarkStart("ConversionFunction"); section.getParagraphs().get(16).appendBookmarkEnd("ConversionFunction"); //Save the document doc.saveToFile("AddBookmark.docx", FileFormat.Docx_2013); } }
Remove an Existing Bookmark in a Word Document
The detailed steps are as follows:
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Get a specified bookmark by its index using Document.getBookmarks().get() method.
- Remove the specified bookmark using Document.getBookmarks().remove() method.
- Save the document to another file using Document.saveToFile() method.
- Java
import com.spire.doc.Bookmark; import com.spire.doc.Document; import com.spire.doc.FileFormat; public class RemoveBookmark { public static void main(String[] args) { //Create a Document instance Document doc = new Document(); //Load a sample Word file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\AddBookmark.docx"); //Get bookmark by its index Bookmark bookmark = doc.getBookmarks().get(0); //Remove the bookmark doc.getBookmarks().remove(bookmark); //Save the document. doc.saveToFile("RemoveBookmark.docx", FileFormat.Docx); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.