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

Free Support Forum - aspose.com

Add Comment to Text String Paragraph in Word DOCX Document using Java

Dear Team,
I Want to insert the comment for particular string in word document
i’m using java version1.8, please to the needful

@rjnick,

Aspose.Words for Java supports adding Comments to Word document. Comments of the document are represented by the Comment class. Use CommentRangeStart and CommentRangeEnd classes to specify a region of text that is to be commented. Please refer to the following article for more details:

1 Like

@awais.hafeez please give one example

@rjnick,

Please try running the following Java code to be able to add comment to text in Word document:

Document doc = new Document();

Paragraph para1 = new Paragraph(doc);
Run run1 = new Run(doc, "Some ");
Run run2 = new Run(doc, "text ");
para1.appendChild(run1);
para1.appendChild(run2);
doc.getFirstSection().getBody().appendChild(para1);

Paragraph para2 = new Paragraph(doc);
Run run3 = new Run(doc, "is ");
Run run4 = new Run(doc, "added ");
para2.appendChild(run3);
para2.appendChild(run4);
doc.getFirstSection().getBody().appendChild(para2);

Comment comment = new Comment(doc, "Awais Hafeez", "AH", new Date());
comment.getParagraphs().add(new Paragraph(doc));
comment.getFirstParagraph().getRuns().add(new Run(doc, "Comment text."));

CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.getId());
CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.getId());

run1.getParentNode().insertAfter(commentRangeStart, run1);
run3.getParentNode().insertAfter(commentRangeEnd, run3);
commentRangeEnd.getParentNode().insertAfter(comment, commentRangeEnd);

doc.save("E:\\temp\\awjava-20.4.docx"); 

Hope, this helps.

1 Like