Associate a comment to a specific word when inserting HTML string

Thanks @alexey.noskov will do that.

1 Like

Hi @alexey.noskov can you suggest how do I use
builder.InsertHtml("Test Comment");
instead of Comment.SetText("Test Comment");

Please suggest any convenient way to achieve the above things.

Thank you!

@cpai.sachin The code I have suggested earlier does exactly this:

// Move document builder inside the comment and insert some HTML.
builder.MoveTo(comment.FirstParagraph);
builder.InsertHtml("<b>Bold content </b><i>Italic content</i>");
1 Like

Hi @alexey.noskov I have been tried as below code but its won’t work for me could you please try on your end.

I am trying to placing the comment on start of document.

Document asposeDocument = new Document(documentPath);
DocumentBuilder builder = new DocumentBuilder(asposeDocument);
AsposeComment asposeComment = new AsposeComment(asposeDocument, "Test Author",string.Empty, DateTime.Today);
builder.MoveTo(asposeComment.FirstParagraph);
builder.InsertHtml("<b>Bold content </b><i>Italic content with having para</i>");
asposeDocument.Save(documentPath);

Thank you.

@cpai.sachin In your code you create a comment but does not put it into the document. Also, when you create a comment from scratch it does not have any child nodes, so asposeComment.FirstParagraph is null and code throws. It is required to add a paragraph into the comment to be able to move document builder cursor in it. If you need to add a comment at the beginning of the document you should use code like the following:

Document asposeDocument = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(asposeDocument);
Comment asposeComment = new Comment(asposeDocument, "Test Author", string.Empty, DateTime.Today);
// Put the create commnet at the begining of the document.
asposeDocument.FirstSection.Body.FirstParagraph.PrependChild(asposeComment);

// Put an empty paragraph into the comment.
asposeComment.AppendChild(new Paragraph(asposeDocument));
builder.MoveTo(asposeComment.FirstParagraph);
builder.InsertHtml("<b>Bold content </b><i>Italic content with having para</i>");

asposeDocument.Save(@"C:\Temp\out.docx");
1 Like

Thanks @alexey.noskov will check the provided solution
thanks once again.

1 Like

Hi @alexey.noskov its work for me.
thank you.

1 Like

Hi @alexey.noskov facing one more issue.
When I extract comments from document, if I put some formatting in the comment like made some text bold, undeline or italic.
Same formatted text I would not get while extracting the comment it gives me plain text.

Do we have any solution on it.

Thank you!

@cpai.sachin You can use Node.ToString method to get comment content as HTML with formatting:

string commentHtml = "";
foreach (Node child in comment.ChildNodes)
    commentHtml += child.ToString(SaveFormat.Html);
1 Like

Thanks @alexey.noskov its work for me.

1 Like

A post was split to a new topic: Generate a comments reply using insertHtml method