OpenXML将评论回复插入word文档C#

时间:2016-04-22 17:09:25

标签: c# ms-word openxml openxml-sdk

我正在尝试使用OpenXml插入评论作为回复。如果不可能,我想在选定的评论后立即插入评论。到目前为止,我能够在我想要的地方插入注释,但是当我打开文档时,我无法显示注释。

以下是插入评论的代码。

 using (WordprocessingDocument document = WordprocessingDocument.Open(path + fileName + ".docx", true)){

            // Locate the first paragraph in the document.
            //XMLParagraphAlias firstParagraph = document.MainDocumentPart.Document.Descendants<XMLParagraphAlias>().First();

            XMLCommentsAlias comments = document.MainDocumentPart.WordprocessingCommentsPart.Comments;

            string id = comments.Descendants<DocumentFormat.OpenXml.Wordprocessing.Comment>()
                 .Where(i => i.Id.Value == reply.CurrentCommentID.ToString())
                 .Select(e => e.Id.Value)
                 .First();


             // Compose a new Comment and add it to the Comments part.
             XMLParagraphAlias p = new XMLParagraphAlias(new Run(new Text(reply.ReplyText)));

             string newCommentID = comments.Descendants<DocumentFormat.OpenXml.Wordprocessing.Comment>().Select(e => e.Id.Value).Max();

             XMLCommentAlias cmt = new XMLCommentAlias()
                                  {
                                    Id = newCommentID,
                                    Author = reply.CurrentUserName,
                                    Date = DateTime.Now.Date
                                  };

             XMLCommentAlias comment = comments.Elements<XMLCommentAlias>()
                                       .Where(n => n.Id.Value == reply.CurrentCommentID.ToString())
                                       .First();

             XMLParagraphAlias test2 = comment.Descendants<XMLParagraphAlias>().First();

            cmt.AppendChild(p);        
            comments.AppendChild(cmt);
            comments.Save();

            // Specify the text range for the Comment. 
            // Insert the new CommentRangeStart before the first run of paragraph.
            test2.InsertBefore(new CommentRangeStart() { Id = reply.CurrentCommentID.ToString() }, test2.GetFirstChild<Run>());

            // Insert the new CommentRangeEnd after last run of paragraph.
            var cmtEnd = test2.InsertAfter(new CommentRangeEnd() { Id = reply.CurrentCommentID.ToString() }, test2.Elements<Run>().Last());

            // Compose a run with CommentReference and insert it.
            test2.InsertAfter(new Run(new CommentReference() { Id = reply.CurrentCommentID.ToString() }), cmtEnd);
        }

我可以看到使用VS中的调试器将注释放入文档中,但是当我打开文档时它没有显示。

执行保存命令后,评论将添加到文档中,但不会显示。

此处的总体目标是在文档中包含的注释列表中的特定注释之后插入注释。有人可以帮我找到解决方案吗?

2 个答案:

答案 0 :(得分:2)

我发现需要以下内容来创建回复评论

  • CommentEx零件已添加到文档中
  • 回复评论段链接到其父段
  • CommentRange和CommentReference以正确的顺序添加
  • 回复评论仍必须添加到评论部分

下面的示例代码将为文档中的现有单词添加注释

 foreach (var paragraph in document.MainDocumentPart.Document.Descendants<Paragraph>())
 {
      foreach (var run in paragraph.Elements<Run>())
      {
         var item = run.Elements<Text>().FirstOrDefault(b => b.Text.Contains("DTT"));
         if (item != null)
         {

           if (document.MainDocumentPart.GetPartsCountOfType<WordprocessingCommentsPart>() > 0)
           {
              comments = document.MainDocumentPart.WordprocessingCommentsPart.Comments;
              commentsEx = document.MainDocumentPart.WordprocessingCommentsExPart.CommentsEx;
              if (comments.HasChildren)
              {
                   // Obtain an unused ID.
                   id = comments.Descendants<Comment>().Select(e => e.Id.Value).Max();
              }
         }
         else
         {
             // No WordprocessingCommentsPart part exists, so add one to the package.
             WordprocessingCommentsPart commentPart = document.MainDocumentPart.AddNewPart<WordprocessingCommentsPart>();
             commentPart.Comments = new Comments();
             comments = commentPart.Comments;

             WordprocessingCommentsExPart commentsExPart = document.MainDocumentPart.AddNewPart<WordprocessingCommentsExPart>();
             commentsExPart.CommentsEx = new CommentsEx();
             commentsEx = commentsExPart.CommentsEx;
        }

        Comment comment1 = new Comment() { Initials = "GS", Author = "Tony, Stark", Date = System.Xml.XmlConvert.ToDateTime("2018-11-19T14:54:00Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind), Id = "1" };
        Paragraph paragraph1 = new Paragraph() {  ParagraphId = "68DAFED3", TextId = "77777777" };               
       paragraph1.Append(new Run(new Text("fsdfas")));
       comment1.Append(paragraph1);

       Comment comment2 = new Comment() { Initials = "GS", Author = "Tony, Stark", Date = System.Xml.XmlConvert.ToDateTime("2018-11-19T14:54:00Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind), Id = "2" };
       Paragraph paragraph2 = new Paragraph() { ParagraphId = "346EE35B", TextId = "77777777" };                          
       paragraph2.Append(new Run(new Text("sadfsad")));
       comment2.Append(paragraph2);

       comments.Append(comment1);
       comments.Append(comment2);
       comments.Save();

       CommentRangeStart commentRangeStart1 = new CommentRangeStart() { Id = "1" };
       CommentRangeStart commentRangeStart2 = new CommentRangeStart() { Id = "2" };
       CommentRangeEnd commentRangeEnd1 = new CommentRangeEnd() { Id = "1" };
       CommentReference commentReference1 = new CommentReference() { Id = "1" };
       CommentRangeEnd commentRangeEnd2 = new CommentRangeEnd() { Id = "2" };
       CommentReference commentReference2 = new CommentReference() { Id = "2" };

       run.InsertBefore(commentRangeStart1, item);
       run.InsertBefore(commentRangeStart2, item);
       var cmtEnd = run.InsertAfter(commentRangeEnd1, item);
       var cmtEnd2 = run.InsertAfter(commentRangeEnd2, cmtEnd);
       run.InsertAfter(new Run(commentReference1), cmtEnd);
       run.InsertAfter(new Run(commentReference2), cmtEnd2);

       CommentEx commentEx1 = new CommentEx() { ParaId = "68DAFED3", Done = false };
       CommentEx commentEx2 = new CommentEx() { ParaId = "346EE35B", ParaIdParent = "68DAFED3", Done = false };
       commentsEx.Append(commentEx1);
       commentsEx.Append(commentEx2);
       commentsEx.Save();

       }
    }
}

答案 1 :(得分:0)

在遵循梅斯特的建议后,我能够解决我的问题。标记为回复的注释位于commentsExtended.xml文件中。要创建与注释的关系,您需要创建一个CommentEX对象,并将您要插入的注释回复链接到您要回复的注释。实施评论回复的代码位于下方。 ParaIdParent属性是您要回复的注释的paraId。

        private void BuildCommentExtendXML(WordprocessingDocument document, string randomHexBinaryValue, HexBinaryValue commentsParagraphDescendantId)
    {
        var commentsEx = document.MainDocumentPart.WordprocessingCommentsExPart.CommentsEx;

        CommentEx commentEx = new CommentEx()
        {
            ParaId = randomHexBinaryValue,
            ParaIdParent = commentsParagraphDescendantId,
            Done = new OnOffValue(false)
        };
        commentsEx.Append(commentEx);
    }