翻譯|使用教程|編輯:胡濤|2022-05-25 10:55:01.987|閱讀 290 次
概述:在本文中,您將學(xué)習(xí)如何使用Spire.Doc for .NET以編程方式刪除現(xiàn)有 Word 文檔中的空行/空白段落。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
將網(wǎng)上的內(nèi)容復(fù)制到Word文檔中時,你可能會發(fā)現(xiàn)段落之間有很多空行,這樣不僅會使文檔顯得冗長,而且影響可讀性。在本文中,您將學(xué)習(xí)如何使用Spire.Doc for .NET以編程方式刪除現(xiàn)有 Word 文檔中的空行/空白段落。
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作為 .NET 項(xiàng)目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.Doc
詳細(xì)步驟如下:
【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
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn