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);
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.
@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.
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>");
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");
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);