使用OpenXML SDK 2.0从.docx中删除空段落

时间:2011-11-11 13:29:49

标签: vb.net openxml-sdk

在将内容解析为xml之前,我试图从.docx文件中删除空段落。我怎么做到这一点?

Protected Sub removeEmptyParagraphs(ByRef body As DocumentFormat.OpenXml.Wordprocessing.Body)
    Dim colP As IEnumerable(Of Paragraph) = body.Descendants(Of Paragraph)()

    Dim count As Integer = colP.Count
    For Each p As Paragraph In colP
        If (p.InnerText.Trim() = String.Empty) Then
            body.RemoveChild(Of Paragraph)(p)
        End If
    Next
End Sub

2 个答案:

答案 0 :(得分:1)

您可能遇到的问题是从每个块的列表中删除项目。您可以尝试使用linq和RemoveAll方法:

Protected Sub removeEmptyParagraphs(ByRef body As DocumentFormat.OpenXml.Wordprocessing.Body)
    Dim colP As IEnumerable(Of Paragraph) = body.Descendants(Of Paragraph)()
    colP.RemoveAll(Function(para) para.InnerText.Trim() = String.Empty)
End Sub

答案 1 :(得分:0)

这将有助于删除段落甚至空白页的空白。

IEnumerable<Paragraph> paragraphs =       
                     myDoc.MainDocumentPart.Document.Body.Elements<Paragraph>();

            foreach (Paragraph paragraph in paragraphs)
            {
                if (paragraph != null && string.IsNullOrWhiteSpace(paragraph.InnerText))
                {
                    paragraph.ParagraphProperties = new ParagraphProperties(
                                      new ParagraphStyleId() { Val = "No Spacing" },
                                      new SpacingBetweenLines() { After = "0" }
                                      );
                    paragraph.ParagraphProperties.SpacingBetweenLines.AfterLines = 0;
                    paragraph.ParagraphProperties.SpacingBetweenLines.BeforeLines = 0;
                    paragraph.ParagraphProperties.SpacingBetweenLines.Line = "0";

                }
            }