Correct way to apply a comment to a Run?

Hi there,

What is the correct way to apply a comment to an already-existing Run?

Thanks

Hi Alex,

Thanks for your inquiry. Please use the following code to add a comment to a paragraph in the document.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Some text is added.");
Comment comment = new Comment(doc, "Amy Lee", "AL", new Date());
builder.getCurrentParagraph().appendChild(comment);
comment.getParagraphs().add(new Paragraph(doc));
comment.getFirstParagraph().getRuns().add(new Run(doc, "Comment text."));
doc.save("C:\Temp\out.docx");

I hope, this helps.

Best regards,

There is no “Comment” class in your documentation. The only information I could find relating to comments was this page, which gives examples of how to read and remove comments, but not how to insert comments.

How do I set the comment range? I have attached a document created with MS Word that shows what I am trying to achieve.

Thanks

Hi Alex,

Thanks for the additional information. You can insert a comment field into the document using the code below:

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, "Comment text"));
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 the 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 after the comment");
doc.save("C:\\Temp\\out.docx");

I hope, this helps.

Best regards,

Thank you.

Do you have any documentation available for the Comment, CommentRangeStart and CommentRangeEnd classes?

Hi Alex,

Thanks for your request. We will be sure to improve that part of the online Java documentation shortly. In the meantime, you may please consult the offline documentation that is a part of a .NET MSI installer (This CHM file can be located in Help folder where you installed Aspose.Words for example C:\Program Files (x86)\Aspose\Aspose.Words for .NET\Help). We apologize for any inconvenience.

Best regards,