Insert Comment in Word DOC using Java | Anchor Add a Comment to a Region of Text in Word Document

Hi Team,

sample.zip (2.3 MB)

I want to add comment to particular word using the word position, i have attached the samples . please advice .

Thanks in advance
Regards,
Kesavaraman

@keshav07,

The following example shows how to anchor a comment to a region of text.

String dataDir = Utils.getDataDir(AnchorComment.class);
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(dataDir + "output.doc");

Hope, this helps in achieving what you are looking for.

Thanks for the update