2016-04-22 115 views
0

我试图插入注释作为使用OpenXml的回复。如果不可能,我想在选定的评论之后插入评论。到目前为止,我可以在我想要的地方插入评论,但是我无法在打开文档时显示评论。OpenXML将注释回复插入Word文档C#

下面是插入评论的代码。

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调试器中的文件,但是当我打开该文件没有显示它。

保存命令执行后,注释被添加到文档,但不显示。

此处的总体目标是在文档中包含的评论列表中的特定评论之后插入评论。有人能帮我找到解决办法吗?

+1

我建议你创建一个简单的文档,带一个注释,保存它。现在添加另一个评论(或答复),并以不同的名称保存。在Open XML Productivity Tool中打开其中的第一个,然后使用其“比较”功能将其与第二个文档进行比较。该工具将1)向您显示底层XML中的差异,以及2)将第一个文档更改为第二个文档所需的代码。 IOW它应该告诉你如何添加回复(或其他评论)。 –

回答

0

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

 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); 
    }