Unable to apply shade using Aspose word

Hi Team,

I am trying to apply shade on the bookmark. Bookmarkstart and bookmarkend range is different. Which has wide range. But, the shading is applied only small area. Please find the code snippet.

if (resultWordDoc.getRange().getBookmarks().get("sample_bookmark") != null)
{
    Bookmark bookmark = resultWordDoc.getRange().getBookmarks().get("sample_bookmark");
    docBuilder.moveToBookmark(bookmark.getName());
    docBuilder.getParagraphFormat().getShading().setBackgroundPatternColor(Color.GRAY);
}

Could you please help with above issue.

@forasposeissues The provided code adds shading to the current document builder’s paragraph. Could you please attach you input, output and expected output documents here for testing? We will check the documents and provide you more information.

Hi Team,

Please find the attached file which consists of input file, output file (which have issue) and expected_sample file. Please use bookmark “bkm_summary” which mentioned in file.
I am using this code snippet to apply the shading for bookmark.

if (resultWordDoc.getRange().getBookmarks().get("bkm_summary") != null)
{
    Bookmark bookmark = resultWordDoc.getRange().getBookmarks().get("bkm_summary");
    docBuilder.moveToBookmark(bookmark.getName());
    docBuilder.getParagraphFormat().getShading().setBackgroundPatternColor(Color.GRAY);
}

We are using aspose-words version 23.7-jdk17.
Please help to resolve the issue.
Regards

FILES.zip (30.1 KB)

@forasposeissues In your case you should highlight Run nodes between bookmark start and end. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");

// Get bookmark
Bookmark bk = doc.getRange().getBookmarks().get("bkm_summary");
Node curNode = bk.getBookmarkStart().nextPreOrder(doc);
while (curNode != null && !curNode.equals(bk.getBookmarkEnd()))
{
    Node nextNode = curNode.nextPreOrder(doc);

    if (curNode.getNodeType() == NodeType.RUN)
    {
        Run r = (Run)curNode;
        r.getFont().setHighlightColor(Color.GRAY);
    }

    curNode = nextNode;
}

doc.save("C:\\Temp\\out.docx");

Thanks for the quick response. But, I have to apply shading on bookmark range.

@forasposeissues The provided code does exactly this. Bookmark range is actually represented by nodes between bookmark start and bookmark end: