读取行在Word中进行跟踪修订

时间:2017-01-11 04:35:46

标签: c# .net openxml openxml-sdk

有什么方法可以使用Open XML SDK找到Line There Track Changes [Inserted or Deleted]。我尝试使用下面的代码,我能够检测文档正文是否有跟踪更改和它正常工作现在我想要的是找到正文的哪个文本行包含跟踪更改

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

    }

1 个答案:

答案 0 :(得分:0)

你究竟是什么意思&#34;身体的文字线&#34;?文本行将显示在布局文档(这不容易)或更改的Open XML元素上?

如果这是关于布局文档中的文本行(由Microsoft Word生成),这很难,因为您需要使用布局算法来了解将跟踪这些跟踪更改的行的位置。

如果这是关于OpenXmlElements,例如Text或Paragraph,那么您已经拥有了部分解决方案,因为这是关于查询XML标记。