In this tutorial, you will learn how to create bookmark in Word using Java. It provides all the necessary information about the usage of the library, programming logic to write the application, and a runnable sample code to add bookmark in Word using Java. The explanation is also provided for adding multiple nested bookmarks within the same document and then saving it as DOCX or DOC as per the requirement.
Steps to Add Bookmark to Word Document using Java
- Establish the environment to add Aspose.Words for Java to insert the bookmarks
- Create a new Word file using the Document class object and instantiate the DocumentBuilder class object
- Create a bookmark and set its name
- Add some sample text
- Create another nested bookmark having the specified name and add some text to the document under it
- Close the nested bookmark and then the outer bookmark before saving it on the disk
These steps describe the procedure to insert a bookmark in Word using Java. All the necessary detail is available here including the environment settings, necessary classes, and methods to add bookmarks along with the description to add some sample text for testing the feature easily. The steps here are for nested bookmarks however you may add just a single bookmark as well if required.
Code to Automatically Create Bookmarks in Word using Java
This code demonstrates the process to create bookmark in Word using Java. The DocumentBuilder class object is used for inserting the text, and starting and ending bookmarks using the bookmark name. You can not only add the bookmarks based on rows of text but also add column-wise bookmarks using the startColumnBookmark() by providing the bookmark name.
This tutorial has taught us to add one or more bookmarks in a Word file. If you want to learn the process to add a watermark in a Word file, refer to the article on how to add watermark to Word using Java.