• Word控件Spire.Doc 【段落处理】教程(十四):如C#/VB.NET:删除 Word 中的空行


    将网上的内容复制到Word文档中时,你可能会发现段落之间有很多空行,这样不仅会使文档显得冗长,而且影响可读性。在本文中,您将学习如何使用Spire.Doc for .NET以编程方式删除现有 Word 文档中的空行/空白段落。

    Spire.Doc for.NET 最新下载icon-default.png?t=M7J4https://www.evget.com/product/3368/download

    为 .NET 安装 Spire.Doc

    首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。

    PM> Install-Package Spire.Doc

    删除现有 Word 文档中的空行

    详细步骤如下:

    • 创建一个文档实例。
    • 使用Document.LoadFromFile()方法加载示例 Word 文档。
    • 循环遍历文档中的所有段落并确定该段落是否为空白段落。
    • 使用DocumentObjectCollection.Remove()方法从文档中删除空白段落。
    • 使用Document.SaveToFile()方法将文档保存到另一个文件。

    【C#】

    using Spire.Doc;
    using Spire.Doc.Documents;
    using System;
    
    namespace RemoveEmptyLines
    {
    class Program
    {
    
    static void Main(string[] args)
    {
    
    //Create a Document instance
    Document doc = new Document();
    
    //Load a sample Word document
    doc.LoadFromFile(@"D:\Files\input.docx");
    
    //Loop through all paragraphs in the document
    foreach (Section section in doc.Sections)
    {
    for (int i = 0; i < section.Body.ChildObjects.Count; i++)
    {
    if (section.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
    {
    //Determine if the paragraph is a blank paragraph
    if (String.IsNullOrEmpty((section.Body.ChildObjects[i] as Paragraph).Text.Trim()))
    {
    //Remove blank paragraphs
    section.Body.ChildObjects.Remove(section.Body.ChildObjects[i]);
    i--;
    }
    }
    
    }
    }
    
    //Save the document
    doc.SaveToFile("RemoveEmptyLines.docx", FileFormat.Docx2013);
    
    }
    
    }
    }
    

    【VB.NET】

    Imports Spire.Doc
    Imports Spire.Doc.Documents
    
    Namespace RemoveEmptyLines
    Class Program
    Private Shared Sub Main(ByVal args As String())
    
    'Create a Document instance
    Dim doc As Document = New Document()
    
    'Load a sample Word document
    doc.LoadFromFile("D:\Files\input.docx")
    
    'Loop through all paragraphs in the document
    For Each section As Section In doc.Sections
    
    For i As Integer = 0 To section.Body.ChildObjects.Count - 1
    
    'Determine if the paragraph is a blank paragraph
    If section.Body.ChildObjects(i).DocumentObjectType = DocumentObjectType.Paragraph Then
    
    'Remove blank paragraphs
    If String.IsNullOrEmpty((TryCast(section.Body.ChildObjects(i), Paragraph)).Text.Trim()) Then
    section.Body.ChildObjects.Remove(section.Body.ChildObjects(i))
    i -= 1
    End If
    End If
    Next
    Next
    
    'Save the document
    doc.SaveToFile("RemoveEmptyLines.docx", FileFormat.Docx2013)
    End Sub
    End Class
    End Namespace

  • 相关阅读:
    Http协议网络原理概述
    win11更新后任务栏空白怎么办? win11更新后任务栏空白卡死的解决方法
    挂载VMware esxi服务器文件夹到本地ubuntu
    go垃圾回收
    美团二面:SpringBoot读取配置优先级顺序是什么?
    【PostgreSQL支持中文的全文检索插件(zhparser)】
    VUE之正则表达式全集整理
    SAP UI5 指定 / 变更版本
    GO语言-栈的应用-表达式求值
    交叉编译详解
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/126718620