How to Quickly Delete Extra Blank Lines in Your Word Document in C#/VB.NET?

 The document downloaded from the web has too many empty lines (manual line breaks). Manually removing extra blank lines from a Word document is tedious. To save paper and space, we need to remove all empty lines. Is there an easier way to remove all empty lines instead of deleting each one manually? This tutorial will list a method for removing all empty lines in an existing Word document. Read on for more information.

Programming Environment

In this test, Free Spire.Doc for .NET is introduced into the program. The Spire.Doc.dll file can be referenced by:

Method 1: Download Free Spire.Doc for .NET locally, unzip it, and install it. After the installation is complete, find Spire.Doc.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.

Method 2: Install via NuGet. It can be installed by the following 2 methods:

(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.Doc", and click "Install". Wait for the program installation to complete.

(2) Copy the following content to the PM console installation.

Install-Package FreeSpire.Doc -Version 10.7

Specific Steps:

l  Create a Document instance.

l  Load a sample Word document using Document.LoadFromFile() method.

l  Loop through all paragraphs in the document and determine whether the paragraph is a blank paragraph.

l  Remove blank paragraphs from the document using DocumentObjectCollection.Remove() method.

l  Save the document to another file using Document.SaveToFile() method.

Full Code

【C#】

  1. 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("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】

  1. Imports Spire.Doc

    Imports Spire.Doc.Documents

    Namespace RemoveEmptyLines

        Friend 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("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     

            If section.Body.ChildObjects(i).DocumentObjectType Is DocumentObjectType.Paragraph Then

  2.   'Determine if the paragraph is a blank paragraph  

  3.             If String.IsNullOrEmpty(TryCast(section.Body.ChildObjects(i), Paragraph).Text.Trim()) Then      

                    'Remove blank paragraphs                   

    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


    Effective Shot 

    Conclusion:

    In this post, you have learned how to remove watermark from wordin C#/VB.NET. Not only that, we also have other functions, such as, C#/VB.NET:Remove Paragraphs in a Word Document , C#/VB.NET: Align Text in Word and so on. Apart from that,if you'd like to learn more, you can visit the this link to explore more about for Spire.Doc for .NET

Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application

Java: How to encrypt or decrypt PDF documents?