简体   繁体   English

使用OpenXML c#在Word文档中的选定文本中添加注释

[英]Add Comment in to selected Text in Word Document Using OpenXML c#

I need to use OpenXML to add comments in to a word document. 我需要使用OpenXML在Word文档中添加注释。 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. 通常,在word文档openxml中,将这些文本作为运行元素返回。 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. 这意味着我无法添加特定的CommentRangeStartCommentRangeEnd对象。

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. 因此,在这种情况下,如何在文本“ is first first”中添加注释。

Or in some cases openxml document contains run element as below. 或者在某些情况下,openxml文档包含以下run元素。

<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. 我在这里添加了一个截图,正​​是我想要的。

屏幕截图在这里。

如果样式没有变化,并且允许您操作文档,则可以轻松地将所有运行合并到一个段落中,然后隔离文本运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM