Adding comments using CommentRangeStart / CommentRangeEnd
Works correctly when paragraph has trailing space but space is NOT selected
Throws exception when trailing space itself is included in comment range
Key Observation
Paragraph contains space → no issue
Space explicitly included in comment selection → exception
var ccs = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>().ToArray();
var cc = ccs.FirstOrDefault(s => s.Tag == "22d-aed-52f-ee3-5c31");
Comment cmtObj = new Comment(doc, "Ayush", "Ayush", "12:03:04");
StructuredDocumentTag cc = new StructuredDocumentTag()
CommentRangeStart start = new CommentRangeStart(doc, cmtObj.Id);
CommentRangeEnd end = new CommentRangeEnd(doc, cmtObj.Id);
cc.InsertBefore(start, cc.FirstChild);
cc.InsertAfter(end, cc.LastChild);
builder.MoveTo(cc.LastChild);
builder.InsertNode(cmtObj);
Source Document :
b7b38b9bfde8f539.docx (27.5 KB)
Aspose.Words Version : 25.5.0
@AlpeshChaudhariDev
You’re getting an exception when the comment range you create with CommentRangeStart / CommentRangeEnd includes the trailing space of a paragraph. This situation has been reported by a user as a possible bug in Aspose.Words .NET v25.5.0 .
Next steps
- Provide a minimal reproducible sample – the code you posted together with a small .docx that contains a paragraph ending with a space and where the comment range includes that space.
- Share the full exception details (stack‑trace) so we can see exactly where the failure occurs.
- As a temporary workaround, avoid selecting the trailing space when defining the comment range (e.g., trim the range or insert the comment before the space).
With the sample and exception information we can investigate the issue further.
@AlpeshChaudhariDev Please try modifying your code like the following:
Document doc = new Document(@"C:\Temp\in.docx");
StructuredDocumentTag[] ccs = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>().ToArray();
StructuredDocumentTag cc = ccs.FirstOrDefault(s => s.Tag == "22d-aed-52f-ee3-5c31");
Comment cmtObj = new Comment(doc, "Ayush", "Ayush", DateTime.Now);
CommentRangeStart start = new CommentRangeStart(doc, cmtObj.Id);
CommentRangeEnd end = new CommentRangeEnd(doc, cmtObj.Id);
// Insert commet range start at the beginning of the SDT, and range end and comment at the end of SDT.
if (cc.Level == MarkupLevel.Inline)
{
cc.InsertBefore(start, cc.FirstChild);
cc.InsertAfter(end, cc.LastChild);
cc.InsertAfter(cmtObj, cc.LastChild);
}
else
{
Paragraph firstParagraph = cc.GetChild(NodeType.Paragraph, 0, true) as Paragraph;
if (firstParagraph != null)
firstParagraph.PrependChild(start);
Paragraph lastParagraph = cc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().LastOrDefault();
if (lastParagraph != null)
{
lastParagraph.AppendChild(end);
lastParagraph.AppendChild(cmtObj);
}
}
doc.Save(@"C:\Temp\out.docx");
In your case SDT is block level, though it is allowed to insert comment range start/end at block level, MS Word interpret them incorrectly, so I would suggest to insert these nodes on inline level to make them be handled properly in MS Word.