0
votes

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 CommentRangeStart and CommentRangeEnd objects.

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(new CommentRangeStart() { Id = id }, item);
            var cmtEnd = run.InsertAfter(new CommentRangeEnd() { Id = id }, item);
            run.InsertAfter(new Run(new CommentReference() { Id = id }), cmtEnd);
        }
    }
}

More Detail..

<w:r><w:t>This </w:t></w:r>

<w:r><w:t>is </w:t></w:r>

<w:r><w:t>a first paragraph</w:t></w:r>

So how could I add a comment in to text "is a first para" in that case.

Or in some cases openxml document contains run element as below.

<w:r><w:t>This is a first paragraph</w:t></w:r>

So both of these cases how to add a comment in to my specific selection of words. I have added a screenshot here which exactly what i want.

screenshot here..

1

1 Answers

0
votes

If the style doesn't differ, and if you are allowed to manipulate the doc, you could easily merge all runs in a paragraph, and then isolate the text run.