I need to use OpenXML to add comments in to a word document. I need to add a comment to a location or word(or multiple words). Normally in a word document openxml return those text as run elements. But the words which I wanted to add a comment is coming
with different run elements. So I couldn't add a comment in to the document words which i actually wanted. It means that I couldn't add specific My current implementation is as below. |
foreach(var paragraph in document.MainDocumentPart.Document.Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>()){foreach(var run in paragraph.Elements<Run>()){var item = run.Elements<Text>().FirstOrDefault(b => b.Text.Trim()=="My words selection to add comment");if(item !=null){
run.InsertBefore(newCommentRangeStart(){Id= id }, item);var cmtEnd = run.InsertAfter(newCommentRangeEnd(){Id= id }, item);
run.InsertAfter(newRun(newCommentReference(){Id= id }), cmtEnd);}}}