The bookmark feature in a Word document is similar to a bookmark we used while reading a book. When you have a long document and need to return to specific locations in the document later for reviewing or editing, you can use the Bookmark feature in Microsoft Word. In this article, I’ll use Java codes to manipulate bookmarks including add and remove them in a Word document without installing Microsoft Office.
Kindly note that you need a third-party library while using Java codes to finish the actions above. Here I recommend a free library called Free Spire.Doc for Java. The first thing you need to do is adding the jar file to IDEA. You can download the package from the link or directly reference it using the following Maven configurations.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Add a Bookmark for the specified paragraph in a Word document
Free Spire.Doc for Java supports adding a bookmark to the specified paragraph through two provides which are
appendBookmarkStart (string name) and appendBookmarkEnd (string name). BookmarkStart represents the start
point of the bookmark, while BookmarkEnd represents the end point of the bookmark.
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
public class AddBookmark {
public static void main(String[] args) {
//create a Document object
Document doc = new Document();
//load a Word file to test
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx");
//get the paragraph where you want to insert bookmark
Paragraph paragraph = doc.getSections().get(0).getParagraphs().get(4);
//Create a start bookmark and move it to the beginning of the paragraph
BookmarkStart start = paragraph.appendBookmarkStart("Bookmark1");
paragraph.getItems().insert(0,start);
//append a end bookmark at the end of the paragraph
paragraph.appendBookmarkEnd("Bookmark1");
//save the resulting file
doc.saveToFile("output/AddBookmark.docx", FileFormat.Docx_2013);
}
}Output
Add a bookmark for the specified String in a Word document
At the same time, Free Spire.Doc for Java provides the findString () method to find the specified words where
we want to add a bookmark. And then use BookmarkStart and BookmarkEnd methods to locate the start point
and end point of the bookmark.
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange;
public class AddBookmarkToCharacter {
public static void main(String[] args) {
//Load a Word document to test
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\AddBookmark.docx");
//Find the specified string
TextSelection textSelection = doc.findString("Before you fret too much over this",false,false);
TextRange range = textSelection.getAsOneRange();
Paragraph para = range.getOwnerParagraph();
int index = para.getChildObjects().indexOf(range);
//Add a bookmark to the specified string
BookmarkStart start = new BookmarkStart(doc,"Bookmark2");
BookmarkEnd end = new BookmarkEnd(doc, "Bookmark2");
para.getChildObjects().insert(index, start);
para.getChildObjects().insert(index + 2, end);
//Save the resulting document
doc.saveToFile("output/AppendBookmarkToCharacter.docx",FileFormat.Docx_2013);
doc.dispose();
}
}Output
Remove a specified bookmark in a Word documentEvery Word document contains a collection of bookmarks, which are accessible through the Bookmarks property
of Document class. We can find a specified bookmark by its index and remove it using the Bookmarks property.
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 object
Document doc = new Document();
//load a Word file to test
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\AppendBookmarkToCharacter.docx");
//get the first bookmark by its index
Bookmark bookmark = doc.getBookmarks().get(1);
//remove the bookmark
doc.getBookmarks().remove(bookmark);
//save the resulting file
doc.saveToFile("output/RemoveBookmark.docx", FileFormat.Docx);
}
}Output
No comments:
Post a Comment