Associate a comment to a specific word when inserting HTML string

Thanks @alexey.noskov.

Hi @alexey.noskov thanks for your reply.
Its work for me but Its adding empty line before the comment.
Can you suggest me the solution for it.

@cpai.sachin This depends on the HTML snippet. Could you please provide your HTML string here for testing? We will check the issue and provide you more information.

Hi @alexey.noskov, yes its because of my comment reply has p tag in its text. So that it adding one more line right?

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

Thank you!

@cpai.sachin Yes, when <P> tag is used in HTML, it is interpreted as a separate paragraph. You can specify HtmlInsertOptions.RemoveLastEmptyParagraph to remove empty paragraph after the inserted content:

builder.InsertHtml("<p><b>Bold content </b><i>Italic content</i></p>", HtmlInsertOptions.RemoveLastEmptyParagraph);
1 Like

Hi @alexey.noskov we have Aspose.Words, Version=21.6.0.0
As I checked HtmlInsertOptions.RemoveLastEmptyParagraph not available with this version.
can you tell in which version it is available.
Thank you.

Hi @alexey.noskov any updates on this please.

@cpai.sachin This option is available starting from 21.7 version of Aspose.Words. I would suggest you to update to the latest 23.5 version of Aspose.Words to get most recent API and features.

1 Like

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