Changing Bookmark highlight color from java

Hi I am trying to change highlight color of all bookmarks in a document using java but I couldn’t find an appropriate method of changing the color
Can you help regarding this

This is the code i am using

private Document removeBookMarkHighLighting(Document document) {
	// TODO Auto-generated method stub

	BookmarkCollection bmCollection = document.getRange().getBookmarks();

	for (Bookmark book : bmCollection)
	{

		Node currentNode = book.getBookmarkStart();

		try
		{
			while (currentNode != book.getBookmarkEnd() && currentNode != null)

			{

				((Run) currentNode).getFont().setHighlightColor(Color.WHITE);
				;

				currentNode = currentNode.nextPreOrder(document);

			}
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	return document;
}

@Karthik_M

Thanks for your inquiry. Please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.

We will investigate the issue on our side and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

@tahir.manzoor
Thank you for your timely responseExpected output.zip (27.9 KB)

I have attached input and expected output

@Karthik_M

Thanks for sharing the detail. Your code is correct. You just need to add the If condition as shown below.

while (currentNode != book.getBookmarkEnd() && currentNode != null)
{
    if(currentNode.getNodeType() == NodeType.RUN)
    {
        ((Run) currentNode).getFont().setHighlightColor(Color.WHITE);
    }

    currentNode = currentNode.nextPreOrder(document);
}

Please make sure that you are using the same input document. We have attached the output document with this post for your kind reference. 19.1.zip (12.6 KB)

Thanks for the response

if(currentNode.getNodeType() == NodeType.RUN)
{
((Run) currentNode).getFont().setHighlightColor(Color.WHITE);
}

But here this if condition will never be true. Because “currentNode.getNodeType()” is a bookmark type and returning a value 9 and NodeType.RUN is run type ,so returning value as 21. So these two value will never be equal.
A little change in my code is while is to be replace with if because it is looping over one bookmark only and taking me to infinite loop.
So new code will be like as:

if (currentNode != book.getBookmarkEnd() && currentNode != null)
{
if(currentNode.getNodeType() == NodeType.RUN)
{
((Run) currentNode).getFont().setHighlightColor(Color.WHITE);
}
currentNode = currentNode.nextPreOrder(document);
}

But from this am not getting any output as the inner if condition as you suggested will never be true

@Karthik_M

Thanks for your inquiry. The currentNode.nextPreOrder(document) gets next node according to the pre-order tree traversal algorithm. This will make the if condition true. Please check the attached image for detail.

The following code examples works at our end. To make sure you are using the same document, we suggest you please rename your input document name e.g. bookmark.docx and execute the following code example. Hope this helps you.

Document document = new Document(MyDir + "bookmark.docx");
BookmarkCollection bmCollection = document.getRange().getBookmarks();

for (Bookmark book : bmCollection)
{
    Node currentNode = book.getBookmarkStart();
    while (currentNode != book.getBookmarkEnd() && currentNode != null)
    {
        if(currentNode.getNodeType() == NodeType.RUN)
        {
            System.out.println(currentNode);
            ((Run) currentNode).getFont().setHighlightColor(Color.WHITE);
        }

        currentNode = currentNode.nextPreOrder(document);
    }
}
document.save(MyDir + "19.1.docx");