Insert Comment for a selection

How to insert a comment for a selected range in the document.

I’m using Aspose.words JAVA API 2.4.1.0.

Thanks
Thangaraj

Hi
Thank you for your inquiry. You can use the following code for inserting comments into the document.

// Create new document
Document doc = new Document();
// Create DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert some text into the document
builder.write("Some text is added.");
// Create comment
Comment comment = new Comment(doc);
// Add comment to the current paragraph
builder.getCurrentParagraph().appendChild(comment);
// Add text into the comment
comment.getParagraphs().add(new Paragraph(doc));
comment.getFirstParagraph().getRuns().add(new Run(doc, "Comment text."));
// Save document
doc.save("out.doc");

Please see the following link for more information.
https://reference.aspose.com/words/net/aspose.words/comment/
I hope this could help you.
Best regards.

Thanks for your prompt reply

I have no problems in inserting a comment into document but I need to set the selection-text for the comment.

Hi
Thanks for your inquiry. Unfortunately this feature is not available in Aspose.Words. This is known issue # 1012 in our defect database. I will notify you as soon as this feature will be available.
Best regards.

We are happy to notify you that the latest version of Aspose.Words supports commented ranges. You can download the latest version fo Aspose.Words from here..
Here is a simple code example, which allows insertion commented range into the document:

// Create an empty document and DocumentBuilder object.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a Comment.
Comment comment = new Comment(doc);
// Insert some text into the comment.
Paragraph commentParagraph = new Paragraph(doc);
commentParagraph.AppendChild(new Run(doc, "This is comment!!!"));
comment.AppendChild(commentParagraph);
// Create CommentRangeStart and CommentRangeEnd.
int commentId = 0;
CommentRangeStart start = new CommentRangeStart(doc, commentId);
CommentRangeEnd end = new CommentRangeEnd(doc, commentId);
// Insert some text into the document.
builder.Write("This is text before comment ");
// Insert comment and comment range start.
builder.InsertNode(comment);
builder.InsertNode(start);
// Insert some more text.
builder.Write("This is commented text ");
// Insert end of comment range.
builder.InsertNode(end);
// And finaly insert some more text.
builder.Write("This is text aftr comment");
// Save output document.
doc.Save(@"Test001\out.doc");

Best regards,