为 .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 instanceDocument doc = new Document();//Load a sample Word documentdoc.LoadFromFile(@"D:Filesinput.docx");//Loop through all paragraphs in the documentforeach (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 paragraphif (String.IsNullOrEmpty((section.Body.ChildObjects[i] as Paragraph).Text.Trim())){//Remove blank paragraphssection.Body.ChildObjects.Remove(section.Body.ChildObjects[i]);i--;}}}}//Save the documentdoc.SaveToFile("RemoveEmptyLines.docx", FileFormat.Docx2013);}}}
【VB.NET】
Imports Spire.DocImports Spire.Doc.DocumentsNamespace RemoveEmptyLinesClass ProgramPrivate Shared Sub Main(ByVal args As String())'Create a Document instanceDim doc As Document = New Document()'Load a sample Word documentdoc.LoadFromFile("D:Filesinput.docx")'Loop through all paragraphs in the documentFor Each section As Section In doc.SectionsFor i As Integer = 0 To section.Body.ChildObjects.Count - 1'Determine if the paragraph is a blank paragraphIf section.Body.ChildObjects(i).DocumentObjectType = DocumentObjectType.Paragraph Then'Remove blank paragraphsIf String.IsNullOrEmpty((TryCast(section.Body.ChildObjects(i), Paragraph)).Text.Trim()) Thensection.Body.ChildObjects.Remove(section.Body.ChildObjects(i))i -= 1End IfEnd IfNextNext'Save the documentdoc.SaveToFile("RemoveEmptyLines.docx", FileFormat.Docx2013)End SubEnd ClassEnd Namespace

欢迎下载|体验更多E-iceblue产品
如需获取更多产品相关信息请咨询在线客服
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!