2012-08-02 121 views
2

我想使用C#和Open XML SDK以编程方式将特定文本分成两部分。 我为第一部分所做的工作是删除所有段落,直到包含所需文本的段落。这工作得很好。 然后在原始文档的副本上,我只做了相同的操作,只是从包含所需文本的文档中删除所有段落。 由于某种原因,第二部分原来是无效的文档,不能用word打开。 使用“Open XML SDK 2.0生产力工具”打开损坏的文档并进行验证,不会检测到任何文档问题。如何使用C#和Open XML SDK通过特定文本分割Word文档?

这是所希望的文本之前移除所述部分的代码(正常工作):

public static void DeleteFirstPart(string docName) 
    { 
     using (WordprocessingDocument document = WordprocessingDocument.Open(docName, true)) 
     { 
      DocumentFormat.OpenXml.Wordprocessing.Document doc = document.MainDocumentPart.Document; 

      List<Text> textparts = document.MainDocumentPart.Document.Body.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().ToList(); 
      foreach (Text textfield in textparts) 
      { 
       if (!textfield.Text.Contains("split here")) 
       { 
        RemoveItem1(textfield); 
       } 
       else 
       { 
        break; 
       } 
      } 
     } 
    } 

我尝试了两种不同的删除项目的方法,均与相同的结果:

private static void RemoveItem1(Text item) 
    { 
     // Need to go up at least two levels to get to the run. 
     if ((item.Parent != null) && 
      (item.Parent.Parent != null) && 
      (item.Parent.Parent.Parent != null)) 
     { 
      var topNode = item.Parent.Parent; 
      var topParentNode = item.Parent.Parent.Parent; 
      if (topParentNode != null) 
      { 
       topNode.Remove(); 
       // No more children? Remove the parent node, as well. 
       if (!topParentNode.HasChildren) 
       { 
        topParentNode.Remove(); 
       } 
      } 
     } 
    } 


private static void RemoveItem2(Text textfield) 
    { 
     if (textfield.Parent != null) 
     { 
      if (textfield.Parent.Parent != null) 
      { 
       if (textfield.Parent.Parent.Parent != null) 
       { 
        textfield.Parent.Parent.Remove(); 
       } 
       else 
       { 
        textfield.Parent.Remove(); 
       } 
      } 
      else 
      { 
       textfield.Remove(); 
      } 
     } 
    } 

这是代码删除从所需文本开始的部分(损坏文档):

public static void DeleteSecondPart(string docName) 
    { 
     using (WordprocessingDocument document = WordprocessingDocument.Open(docName, true)) 
     { 
      DocumentFormat.OpenXml.Wordprocessing.Document doc = document.MainDocumentPart.Document; 

      List<Text> textparts = document.MainDocumentPart.Document.Body.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().ToList(); 
      bool remove = false; 
      foreach (Text textfield in textparts) 
      { 
       if (textfield.Text.Contains("split here")) 
       { 
        remove = true; 
       } 

       if(remove) 
       { 
        RemoveItem1(textfield); 
        //Using this commented code line, instead of the one above, removes only the text field itself, it works fine, the document is valid, but it leaves empty paragraphs that could be pages long. 
        //textfield.Remove(); 

       } 
      } 
     } 
    } 
+0

好了,它似乎适用于某些文件,并且未能别人,所以我想一个特定的文档部分造成这种失败的(或许表...) 。需要进一步调查......但我会为任何线索感到高兴。 – RonyK 2012-08-02 10:19:12

回答

2

A rew该RemoveItem方法的仪式的伎俩:

private static void RemoveItem3(Text textfield) 
    { 
     OpenXmlElement element = textfield; 
     while (!(element.Parent is DocumentFormat.OpenXml.Wordprocessing.Body) && element.Parent != null) 
     { 
      element = element.Parent; 
     } 

     if (element.Parent != null) 
     { 
      element.Remove(); 
     } 
    } 
相关问题