How to Create Bookmark in Word using Java

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

  1. Establish the environment to add Aspose.Words for Java to insert the bookmarks
  2. Create a new Word file using the Document class object and instantiate the DocumentBuilder class object
  3. Create a bookmark and set its name
  4. Add some sample text
  5. Create another nested bookmark having the specified name and add some text to the document under it
  6. 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

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.License;
public class AsposeTest {
public static void main(String[] args) throws Exception {//Main function to insert bookmark using Java
// Instantiate the license
License lic = new License();
lic.setLicense("Aspose.Total.lic");
// Create the Document object
Document doc = new Document();
// Create a DocumentBuilder object
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a bookmark
builder.startBookmark("Outer Bookmark");
// Add some text under this bookmark
builder.writeln("Text inside the outer bookmark.");
// Start another bookmark
builder.startBookmark("Nested Inner Bookmark");
// Add some text to the document under this nested bookmark
builder.writeln("Text inside a NestedBookmark.");
// End the nested bookmark
builder.endBookmark("Nested Inner Bookmark");
// Write text after the nested bookmark
builder.writeln("Text after Nested Bookmark.");
// End the outer bookmark
builder.endBookmark("Outer Bookmark");
// Save the resultant Word file with bookmarks
doc.save("Output.docx");
System.out.println("Done");
}
}

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.

 English