Insert a comment to a region of text using Java

Hi!

I have problems to add comments to my document. I found the class Comment and something like CommentRangeStart but I do not know how to work with it because the api documentation is insufficient and incomplete for this part and no Howto is available. I’d like to insert a comment to a region of text (“My text”). I only find out how to insert it after the text.

builder.writeln("My text");
Comment comment = new Comment(builder.getDocument());
Comment.setText("This is a comment");
builder.getCurrentParagraph().appendChild(comment);

A code example would be nice.

Best regards,

Hi Petra,

Thanks for your query. Please use the following code snippet to add comments to your document. Let us know, If you have any more queries.

// 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("D:\\out.doc");

Thank you for your fast reply! This was the code snippet I need.
Maybe relevant to say in this context: The commentId has to be a sequential number. This was at least my first mistake after use the snippet.

Best regards!

Hi Petra,

Thanks for your feedback. Please let us know, If you have any more queries.

Hi Petra,

Thanks for pointing out this gap in our documentation. We will be sure to add further documentation for this topic as soon as possible.

Thanks,