We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Deleting commented nodes

Hi there,

How can I delete all nodes in between COMMENT_RANGE_START and COMMENT_RANGE_END?


Hi Corneliu,
Thanks your inquiry. Please read about Aspose.Words’ document object model from here:Aspose.Words Document Object Model
We suggest you please iterate through all nodes between COMMENT_RANGE_START and COMMENT_RANGE_END and remove them. Please use following code example to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
CommentRangeStart commentStart = (CommentRangeStart)doc.getChild(NodeType.COMMENT_RANGE_START, 0, true);
CommentRangeEnd commentEnd = (CommentRangeEnd)doc.getChild(NodeType.COMMENT_RANGE_END, 0, true);

Node currentNode = commentStart;
Boolean isRemoving = true;
while (currentNode != null && isRemoving)
    if (currentNode.getNodeType() == NodeType.COMMENT_RANGE_END)
        isRemoving = false;

    Node nextNode = currentNode.nextPreOrder(doc);
    currentNode = nextNode;

doc.save(MyDir + "Out.docx");

Thanks for reply Tahir,

It doesn’t reach any node of type COMMENT_RANGE_END and the while stops when current node is null. But I found another way to retrieve all nodes in between two marker nodes:

Now I’m facing a problem that I can’t find the corresponding COMMENT node for a COMMENT_RANGE_START, COMMENT_RANGE_END pair. I’ve try using methods like getNextSibling or getParent().getNextSibling() but it doesn’t cover all cases.

Is there any simple way to match comments with Comment_Range?


Hi Corneliu,

Thanks your inquiry. CommentRangeStart.Id property specifies the identifier of the comment to which this region is linked. Similarly, CommentRangeEnd.Id does the same. You can use these property to identify correct CommentRangeStart and CommentRangeEnd nodes.

Could you please share your input and expected output documents here for our reference? We will then provide you more information about your query along with code.


Thanks for the hint with id property it works(a java doc would be very helpful…I could not find one). Also the code that deletes the nodes between CommentRangeStart and CommentRangeEnd works(I did something wrong at the first time). I modified it a little to cover also the case of inner comments(commented text inside another commented text).

Thank again for your help


Hi Corneliu,

Thanks your inquiry. It is nice to hear from you that you have found the solution of your query. Please let us know if you face any issue while using Aspose.Words.

Unfortunately, there are no article about removing text between CommentRangeStart and CommentRangeEnd nodes. We will add this in Aspose.Words’ documentation.