@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>");
@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);
Hello @awais.hafeez ,
I am back with a question related to the issue you helped me with 6 years ago. One scenario is recently failing with the fix you give me. I am currently using Aspose.Word 21.9 version. I am inserting an html content to a document and the comment reference is imbanded within the html content as you provided me with the solution previously. There is one scenario where the comment anchor position is misplaced. Let’s say we have two paragraphs, the first one is inserted in a table cell then we close the table. The second paragraph is inserted after the table and also contains a comment reference.
E.g:
String para1 = "<html><head></head><body><div><p>This is a paragraph in a table</p></div></body></html>";
String para2 = "<html><head></head><body><div><p>This is a
<a name="_cmntref1"><span>paragraph</span></a><span style="-aw-comment-end: _cmntref1"></span>
<a href="#_cmnt1"><span>[a1]</span></a> outside the table </p>
<div id="_cmnt1" style="-aw-comment-author:'tesfayemissy'; -aw-comment-datetime:'02/20/2025 01:33:09 AM'; ">
<a href="#_cmntref1"><span>[a1]</span></a><span><p>paragraph</p></span></div></div></body></html>";
builder.startTable();
builder.insertCell();
builder.insertHtml("Description");
builder.insertCell();
builder.insertHtml(para1);
builder.endRow();
builder.endTable();
builder.insertHtml(para2);
The above scenario misplace the comment position and place the comment anchor at the end of the paragraph. If the first paragraph is not in a table, the comment position is placed on the right location. I tried inserting an empty paragraph and a line break right after the table ends but noting seems to work. Will you be able to help me with a solution?
@tesfayemissy Could you please attach your output and expected output here for our reference? I tested your code on my side and as I can see the comment is placed correctly:
out.docx (8.0 KB)
I have attached my output. The expected output is the same as yours. Their is a css style appended in each html content for text formatting purpose which I didn’t include in the example I provided above. Let me know if it’s necessary and I will share it with you.
tesfayemissy-output.docx (6.9 MB)
@tesfayemissy Unfortunately, I cannot reproduce the problem with the provided HTML strings and your code:
String para1 = readHtmlFromFile("C:\\Temp\\in1.html");
String para2 = readHtmlFromFile("C:\\Temp\\in2.html");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startTable();
builder.insertCell();
builder.insertHtml("Description");
builder.insertCell();
builder.insertHtml(para1);
builder.endRow();
builder.endTable();
builder.insertHtml(para2);
doc.save("C:\\Temp\\out_java.docx");
The only difference I put the strings into the files for analysis convenience. So, please attach full HTML snippets that will allow us to reproduce the problem. We will check once again and provide you more information.
Hi @alexey.noskov , please find the complete html that is inserted in the word document below. I also uploaded the css file as well.
String inst1 = "<html><head><link type='text/css' rel='Stylesheet' href='/../word.css'/></head><body><style> .ck-indent-1{ margin-left:25px; } .ck-indent-2{ margin-left:50px; } .ck-indent-3{ margin-left:75px; } .ck-indent-4{ margin-left:100px; } .ck-indent-5{ margin-left:125px; } .ck-indent-6{ margin-left:150px; } .ck-indent-7{ margin-left:175px; } .ck-indent-8{ margin-left:200px; } .ck-indent-9{ margin-left:225px; } .ck-indent-10{ margin-left:250px; } .ck-indent-11{ margin-left:275px; } .ck-indent-12{ margin-left:300px; } .ck-indent-13{ margin-left:325px; } .ck-indent-14{ margin-left:350px; } .ck-indent-15{ margin-left:375px; } .ck-indent-16{ margin-left:400px; } .ck-indent-17{ margin-left:425px; } .ck-indent-18{ margin-left:450px; } .ck-indent-19{ margin-left:475px; } .ck-indent-20{ margin-left:500px; } </style><style type="text/css">a[href]{color: #0000ff; text-decoration: underline;}.h4{ margin-top: 7pt !important;} .highlight { background-color:#fff7d5; }h1.highlight, h2.highlight, h3.highlight, h4.highlight { background-color:#fff7d5 !important; }span[style*="background-color:"] u,span[style*="background-color:"] i,span[style*="background-color:"] strong { background-color: inherit; }table, th, td { border-width: 1px; border-style: solid; border-color: rgb(0, 0, 0); border-spacing: 0px; }.table[style*="width:"] > table { width: inherit; }.table[style*="height:"] > table { height: inherit; }.table[style*="float:"] > table { float: inherit; }.table > figcaption {text-align: center;}</style><p>This is an Instruction for paragraph in a table </p>";
String para1 = "<html><head><link type='text/css' rel='Stylesheet' href='/../word.css'/></head><body><style> .ck-indent-1{ margin-left:25px; } .ck-indent-2{ margin-left:50px; } .ck-indent-3{ margin-left:75px; } .ck-indent-4{ margin-left:100px; } .ck-indent-5{ margin-left:125px; } .ck-indent-6{ margin-left:150px; } .ck-indent-7{ margin-left:175px; } .ck-indent-8{ margin-left:200px; } .ck-indent-9{ margin-left:225px; } .ck-indent-10{ margin-left:250px; } .ck-indent-11{ margin-left:275px; } .ck-indent-12{ margin-left:300px; } .ck-indent-13{ margin-left:325px; } .ck-indent-14{ margin-left:350px; } .ck-indent-15{ margin-left:375px; } .ck-indent-16{ margin-left:400px; } .ck-indent-17{ margin-left:425px; } .ck-indent-18{ margin-left:450px; } .ck-indent-19{ margin-left:475px; } .ck-indent-20{ margin-left:500px; } </style><div><p>This is a paragraph in a table</p></div><style type="text/css">a[href]{color: #0000ff; text-decoration: underline;}.h4{ margin-top: 7pt !important;} .highlight { background-color:#fff7d5; }h1.highlight, h2.highlight, h3.highlight, h4.highlight { background-color:#fff7d5 !important; }span[style*="background-color:"] u,span[style*="background-color:"] i,span[style*="background-color:"] strong { background-color: inherit; }table, th, td { border-width: 1px; border-style: solid; border-color: rgb(0, 0, 0); border-spacing: 0px; }.table[style*="width:"] > table { width: inherit; }.table[style*="height:"] > table { height: inherit; }.table[style*="float:"] > table { float: inherit; }.table > figcaption {text-align: center;}</style></body></html>";
String inst2 = "<html><head><link type='text/css' rel='Stylesheet' href='/../word.css'/></head><body><style> .ck-indent-1{ margin-left:25px; } .ck-indent-2{ margin-left:50px; } .ck-indent-3{ margin-left:75px; } .ck-indent-4{ margin-left:100px; } .ck-indent-5{ margin-left:125px; } .ck-indent-6{ margin-left:150px; } .ck-indent-7{ margin-left:175px; } .ck-indent-8{ margin-left:200px; } .ck-indent-9{ margin-left:225px; } .ck-indent-10{ margin-left:250px; } .ck-indent-11{ margin-left:275px; } .ck-indent-12{ margin-left:300px; } .ck-indent-13{ margin-left:325px; } .ck-indent-14{ margin-left:350px; } .ck-indent-15{ margin-left:375px; } .ck-indent-16{ margin-left:400px; } .ck-indent-17{ margin-left:425px; } .ck-indent-18{ margin-left:450px; } .ck-indent-19{ margin-left:475px; } .ck-indent-20{ margin-left:500px; } </style><style type="text/css">a[href]{color: #0000ff; text-decoration: underline;}.h4{ margin-top: 7pt !important;} .highlight { background-color:#fff7d5; }h1.highlight, h2.highlight, h3.highlight, h4.highlight { background-color:#fff7d5 !important; }span[style*="background-color:"] u,span[style*="background-color:"] i,span[style*="background-color:"] strong { background-color: inherit; }table, th, td { border-width: 1px; border-style: solid; border-color: rgb(0, 0, 0); border-spacing: 0px; }.table[style*="width:"] > table { width: inherit; }.table[style*="height:"] > table { height: inherit; }.table[style*="float:"] > table { float: inherit; }.table > figcaption {text-align: center;}</style><p>This is an Instruction for paragraph outside the table </p>";
String style = "<html><head><link type='text/css' rel='Stylesheet' href='/../word.css'/></head><body><style> .ck-indent-1{ margin-left:25px; } .ck-indent-2{ margin-left:50px; } .ck-indent-3{ margin-left:75px; } .ck-indent-4{ margin-left:100px; } .ck-indent-5{ margin-left:125px; } .ck-indent-6{ margin-left:150px; } .ck-indent-7{ margin-left:175px; } .ck-indent-8{ margin-left:200px; } .ck-indent-9{ margin-left:225px; } .ck-indent-10{ margin-left:250px; } .ck-indent-11{ margin-left:275px; } .ck-indent-12{ margin-left:300px; } .ck-indent-13{ margin-left:325px; } .ck-indent-14{ margin-left:350px; } .ck-indent-15{ margin-left:375px; } .ck-indent-16{ margin-left:400px; } .ck-indent-17{ margin-left:425px; } .ck-indent-18{ margin-left:450px; } .ck-indent-19{ margin-left:475px; } .ck-indent-20{ margin-left:500px; } </style> <style type="text/css">a[href]{color: #0000ff; text-decoration: underline;}.h4{ margin-top: 7pt !important;} .highlight { background-color:#fff7d5; }h1.highlight, h2.highlight, h3.highlight, h4.highlight { background-color:#fff7d5 !important; }span[style*="background-color:"] u,span[style*="background-color:"] i,span[style*="background-color:"] strong { background-color: inherit; }table, th, td { border-width: 1px; border-style: solid; border-color: rgb(0, 0, 0); border-spacing: 0px; }.table[style*="width:"] > table { width: inherit; }.table[style*="height:"] > table { height: inherit; }.table[style*="float:"] > table { float: inherit; }.table > figcaption {text-align: center;}</style></body></html>";
String para2 = "<html><head><link type='text/css' rel='Stylesheet' href='/../word.css'/></head><body><style> .ck-indent-1{ margin-left:25px; } .ck-indent-2{ margin-left:50px; } .ck-indent-3{ margin-left:75px; } .ck-indent-4{ margin-left:100px; } .ck-indent-5{ margin-left:125px; } .ck-indent-6{ margin-left:150px; } .ck-indent-7{ margin-left:175px; } .ck-indent-8{ margin-left:200px; } .ck-indent-9{ margin-left:225px; } .ck-indent-10{ margin-left:250px; } .ck-indent-11{ margin-left:275px; } .ck-indent-12{ margin-left:300px; } .ck-indent-13{ margin-left:325px; } .ck-indent-14{ margin-left:350px; } .ck-indent-15{ margin-left:375px; } .ck-indent-16{ margin-left:400px; } .ck-indent-17{ margin-left:425px; } .ck-indent-18{ margin-left:450px; } .ck-indent-19{ margin-left:475px; } .ck-indent-20{ margin-left:500px; } </style><div><p>This is a <a name="_cmntref169294"><span>paragraph </span></a><span style="-aw-comment-end: _cmntref169294"></span><a href="#_cmnt169294"><span>[169294]</span></a>outside the table </p><div id="_cmnt169294" style="-aw-comment-author:'Mistire Tilahun'; -aw-comment-datetime:'02/25/2025 02:32:30 PM'; "><a href="#_cmntref169294"><span>[169294]</span></a><span><p>paragraph</p></span></div></div><style type="text/css">a[href]{color: #0000ff; text-decoration: underline;}.h4{ margin-top: 7pt !important;} .highlight { background-color:#fff7d5; }h1.highlight, h2.highlight, h3.highlight, h4.highlight { background-color:#fff7d5 !important; }span[style*="background-color:"] u,span[style*="background-color:"] i,span[style*="background-color:"] strong { background-color: inherit; }table, th, td { border-width: 1px; border-style: solid; border-color: rgb(0, 0, 0); border-spacing: 0px; }.table[style*="width:"] > table { width: inherit; }.table[style*="height:"] > table { height: inherit; }.table[style*="float:"] > table { float: inherit; }.table > figcaption {text-align: center;}</style></body></html>";
builder.startTable();
builder.insertCell();
builder.writeln("Description");
builder.insertCell();
builder.getFont().setHidden(true);
builder.insertHtml(inst1);
builder.getFont().setHidden(false);
builder.insertHtml(para1);
builder.endRow();
builder.endTable();
builder.getFont().setHidden(true);
builder.insertHtml(inst2);
builder.getFont().setHidden(false);
builder.insertHtml(style);
builder.insertHtml("<br/>");
builder.insertHtml(para2);
word.docx (15.1 KB)
@tesfayemissy Thank you for additional information. Unfortunately, the problem is still not repressible on my side. Here is the produced output: out.docx (8.2 KB)
If possible, could you please create s simple console application that we can compile and run on our side and reproduce the problem?
@alexey.noskov The instructions(inst1 and inst2) are hidden and only shows when we click the Paragraph icon. I see on your document it’s showing as Normal. Could you please try it hidden?
@tesfayemissy The problem is still not reproducible on my side:
String inst1 = readHtmlFromFile("C:\\Temp\\inst1.html");
String para1 = readHtmlFromFile("C:\\Temp\\para1.html");
String inst2 = readHtmlFromFile("C:\\Temp\\inst2.html");
String style = readHtmlFromFile("C:\\Temp\\style.html");
String para2 = readHtmlFromFile("C:\\Temp\\para2.html");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startTable();
builder.insertCell();
builder.writeln("Description");
builder.insertCell();
builder.getFont().setHidden(true);
builder.insertHtml(inst1, true);
builder.getFont().setHidden(false);
builder.insertHtml(para1, true);
builder.endRow();
builder.endTable();
builder.getFont().setHidden(true);
builder.insertHtml(inst2, true);
builder.getFont().setHidden(false);
builder.insertHtml(style, true);
builder.insertHtml("<br/>");
builder.insertHtml(para2);
doc.save("C:\\Temp\\out.docx");
@alexey.noskov I tried running a simple application with the above input and was not able to reproduce the issue as well but when I used a word document I use on my application for the output which has some styles applied to it, I was able to reproduce it. Would you try using the word document below instead and set it as a document for the Document Builder. I have uploaded it below.
Template.docx (6.7 MB)
@tesfayemissy Thank you for additional information.
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-27949
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
The issues you have found earlier (filed as WORDSNET-27949) have been fixed in this Aspose.Words for Java 25.4 update.