简体   繁体   English

openxml:跟踪在word文档中找不到的更改

[英]openxml : track changes not found in word document

I parse MS Word documents with OpenXML to find track changes with a Author value in order to blank them. 我使用OpenXML解析MS Word文档,以查找具有Author值的曲目更改,以便将其空白。 It works except for one particular Word document and I don't know why. 它只适用于一个特定的Word文档,但我不知道为什么。 Only 6 track changes out of 9 are found. 在9个曲目中仅找到6个。

This is how I look for track changes : 这就是我寻找曲目更改的方式:

 var file = Path.GetFileName(filePath);

  using (WordprocessingDocument document = WordprocessingDocument.Open(filePath, true))
  {
    var types = typeof(OpenXmlCompositeElement).Assembly.GetTypes()
                .Where(p => !p.IsInterface && p.GetProperty("Author") != null && p.GetProperty("Author").PropertyType.Equals(typeof(StringValue)))
                .ToList();

       var body = document.MainDocumentPart.Document.Body;
       var changes = body.Descendants().Where(x =>types.Contains(x.GetType()));
  }

Is there a better way to achieve this ? 有没有更好的方法来实现这一目标?

I have also tried by passing the list of types found on this page but the result was the same. 我也尝试通过传递在此页面上找到类型列表,但是结果是相同的。 Still these 3 ignored track changes. 仍然这3个忽略了音轨变化。

EDIT 编辑

Using Open XML SDK Productivity tool, here's one of the node that should be found as a track change but is missing by the code above : 使用Open XML SDK生产力工具,以下是应作为跟踪更改而发现的节点之一,但上面的代码缺少该节点:

  <w:ins w:id="283" w:author="Mr Smith" w:date="2019-09-11T12:34:00Z">
   <w:r w:rsidR="008458AA">
    <w:rPr>
     <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
    </w:rPr>
    <w:t>
     2
    </w:t>
   </w:r>
  </w:ins>

I found the problem. 我发现了问题。

The line var body = document.MainDocumentPart.Document.Body; var body = document.MainDocumentPart.Document.Body; does not include footer and header ! 不包括页脚和页眉!

So I also have to search for track changes within footer and header. 因此,我还必须在页脚和页眉中搜索曲目更改。

eg for footer : 例如页脚:

 var footer = document.MainDocumentPart.FooterParts.ToList();
 var footerChanges = new List<OpenXmlElement>();
                footer.ForEach(f => 
 footerChanges.AddRange(f.Footer.Descendants().Where(x => 
 types.Contains(x.GetType()))));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM