2016-04-22 17 views
0

OpenXmlを使用して返信としてコメントを挿入しようとしています。可能でない場合は、選択したコメントの直後にコメントを挿入したいと思います。これまでのところ、私が好きなところにコメントを挿入することはできましたが、ドキュメントを開いたときにコメントを表示することはできません。OpenXML 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でデバッガを使用して文書に入れているが、私は、ドキュメントを開いたときにそれが表示されていないことがわかります。

saveコマンドの実行後、コメントがドキュメントに追加されますが、表示されません。

ここでの目標は、ドキュメントに含まれるコメントのリストに特定のコメントの後ろにコメントを挿入することです。誰かがこれに対する解決策を見つけるのを助けることができますか?

+1

単純なドキュメントを1つのコメントで作成して保存することをお勧めします。今度は別のコメント(または返信)を追加し、これを別の名前で保存してください。 Open XML Productivity Toolで最初のものを開き、 "比較"機能を使用して2番目のドキュメントと比較します。このツールは、1)基礎となるXMLの違いと、2)最初の文書を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); 
    }