This guide explains how to remove bookmark in Word using Java. It It covers IDE setup, step-by-step instructions, and sample code that demonstrates how to delete a bookmark in Word using Java. You will get suggestions to enhance the given sample code to remove bookmarks using different filters and methods.
Steps to Remove Bookmark from Word DOC using Java
- Set up the environment to use Aspose.Words for Java to remove bookmarks
- Load a Word file containing bookmarks using the Document object
- Create a collection of all the bookmark names in the loaded document
- Create a list of all the hyperlinks in the document
- Find the links that refer to any bookmarks collected above
- Unlink the hyperlinks from the bookmarks, keeping the plain link text as it is
- Create a separate list of bookmarks in the document
- Remove each bookmark while keeping its text in place
- Save the updated Word file after removing the bookmarks and the relevant hyperlinks
These steps summarize how to remove all bookmarks in Word using Java. Load the source Word document, create a collection of bookmark names in the file, create a list of all the hyperlinks in the file, find and remove the links that point to the bookmarks within the document while keeping the link text as it is. Next, create a collection of bookmarks, remove them while keeping their text in place, and save the output Word file.
Code to Delete Bookmark in Word using Java
This code demonstrates how to remove bookmark in Word using Java. You can clear all the bookmarks at once using the clear() method; however, it does not provide any filtering option and also removes the table of contents and GoBack bookmarks, which are normally kept. To keep the hidden bookmarks whose names start with “_Toc…” and “GoBack…”, apply a filter for these strings in the current sample code.
This article assists you in working with bookmarks. To remove hyperlinks from a Word file, refer to the article Remove Hyperlinks from Word using Java.