2017-01-11 15 views
0

Open XML SDKを使用して行の変更履歴[InsertedまたはDeleted]が見つかることはありますか。私は文書本体は、変更履歴の記録を持つかどうかを検出することができていたコードの下にしようとしているし、それは私が欲しい正しく動作するようになりました体のテキスト行はトラックがWordでトラックのリビジョンを読み取る行を読み取る

public static System.Type[] trackedRevisionsElements = new System.Type[] { 
    typeof(CellDeletion), 
    typeof(CellInsertion), 
    typeof(CellMerge), 
    typeof(CustomXmlDelRangeEnd), 
    typeof(CustomXmlDelRangeStart), 
    typeof(CustomXmlInsRangeEnd), 
    typeof(CustomXmlInsRangeStart), 
    typeof(Deleted), 
    typeof(DeletedFieldCode), 
    typeof(DeletedMathControl), 
    typeof(DeletedRun), 
    typeof(DeletedText), 
    typeof(Inserted), 
    typeof(InsertedMathControl), 
    typeof(InsertedMathControl), 
    typeof(InsertedRun), 
    typeof(MoveFrom), 
    typeof(MoveFromRangeEnd), 
    typeof(MoveFromRangeStart), 
    typeof(MoveTo), 
    typeof(MoveToRangeEnd), 
    typeof(MoveToRangeStart), 
    typeof(MoveToRun), 
    typeof(NumberingChange), 
    typeof(ParagraphMarkRunPropertiesChange), 
    typeof(ParagraphPropertiesChange), 
    typeof(RunPropertiesChange), 
    typeof(SectionPropertiesChange), 
    typeof(TableCellPropertiesChange), 
    typeof(TableGridChange), 
    typeof(TablePropertiesChange), 
    typeof(TablePropertyExceptionsChange), 
    typeof(TableRowPropertiesChange), 
}; 

    public static bool PartHasTrackedRevisions(OpenXmlPart part) 
    { 
     List<OpenXmlElement> insertions = 
     part.RootElement.Descendants<Inserted>() 
     .Cast<OpenXmlElement>().ToList(); 
     //Body bdy = wordDoc.MainDocumentPart.Document.Body; 
     if (part.RootElement.Descendants() 
      .Any(e => trackedRevisionsElements.Contains(e.GetType()))) 
     { 
      var initialTextDescendants = part.RootElement.Descendants<Text>(); 
      string dummy = string.Empty; 
      foreach (Text t in initialTextDescendants) 
      { 
       MessageBox.Show(t.Text); 
      } 
     } 
     return part.RootElement.Descendants() 
      .Any(e => trackedRevisionsElements.Contains(e.GetType())); 
    } 

    public static bool HasTrackedRevisions(WordprocessingDocument doc) 
    { 
     if (PartHasTrackedRevisions(doc.MainDocumentPart)) 
      return true; 
     foreach (var part in doc.MainDocumentPart.HeaderParts) 
      if (PartHasTrackedRevisions(part)) 
       return true; 
     foreach (var part in doc.MainDocumentPart.FooterParts) 
      if (PartHasTrackedRevisions(part)) 
       return true; 
     if (doc.MainDocumentPart.EndnotesPart != null) 
      if (PartHasTrackedRevisions(doc.MainDocumentPart.EndnotesPart)) 
       return true; 
     if (doc.MainDocumentPart.FootnotesPart != null) 
      if (PartHasTrackedRevisions(doc.MainDocumentPart.FootnotesPart)) 
       return true; 
     return false; 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     foreach (var documentName in Directory.GetFiles(".", "*.docx")) 
     { 
      using (WordprocessingDocument wordDoc = 
       WordprocessingDocument.Open(documentName, false)) 
      { 
       if (HasTrackedRevisions(wordDoc)) { 
        //Body bdy = wordDoc.MainDocumentPart.Document.Body; 
        //var initialTextDescendants = bdy.Descendants<Text>(); 
        //string dummy = string.Empty; 
        //foreach (Text t in initialTextDescendants) 
        //{ 
        // richTextBox1.Text = richTextBox1.Text + t.Text; 
        //} 


        Console.WriteLine("{0} contains tracked revisions", documentName); 
       } 
       else 
        Console.WriteLine("{0} does not contain tracked revisions", documentName); 
      } 
     } 

    } 

答えて

0

あなたが正確に何を意味するのかを変更含まれて見つけることです「本文のテキスト行」を使用していますか?レイアウトされたドキュメント(簡単ではない)または変更されたOpen XML要素に表示されるテキスト行

Microsoft Wordで作成された、レイアウトされたドキュメント上のテキスト行については、レイアウトアルゴリズムを使用して、変更が追跡された行がレンダリングされる場所を理解する必要があります。

これは、OpenXmlElements(TextやParagraphなど)に関するもので、XMLマークアップをクエリするためのソリューションの一部です。

関連する問題