Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Nov 29, 2017 5:25 am

Dear support team,
Kindly advise on VB.Net code for deleting all empty lines / line break in Word doc, for example: after removing tables, images etc.

Thank you!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Wed Nov 29, 2017 6:23 am

Hello,

Thanks for your inquiry.
Please kindly note that line break is different from empty lines, or paragraph mark. Could you please first specify your requirement and share a sample document together with the expected result?
Then we will provide the code accordingly.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Wed Nov 29, 2017 10:22 am

Hi, I'm basically looking for a VB.Net code to remove the empty lines left when removing some elements such as tables or images from the document. The code should "look" into the entire Word doc and do this whenever such as tables or images are removed.

Thanks!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Thu Nov 30, 2017 1:49 am

Hello,

According to your description, you are more likely to remove the empty paragraphs. Please refer to the below code.
Code: Select all
Private Sub button15_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim path = ""
    Dim fileExts As String = ""
    Dim openFileDialog As OpenFileDialog = New OpenFileDialog()
    If openFileDialog.ShowDialog() = DialogResult.OK Then
        path = openFileDialog.FileName
        fileExts = Path.GetExtension(path)
    End If

    Dim doc As Document = New Document(path)
    For Each sec As Section In doc.Sections
        Dim emptyHeaderParas As List(Of Paragraph) = New List(Of Paragraph)()
        For Each hObj As DocumentObject In sec.HeadersFooters.Header.ChildObjects
            If TypeOf hObj Is Paragraph Then
                Dim para As Paragraph = TryCast(hObj, Paragraph)
                If String.IsNullOrWhiteSpace(para.Text) Then
                    emptyHeaderParas.Add(para)
                End If
            End If

            If TypeOf hObj Is Table Then
                Dim table As Table = TryCast(hObj, Table)
                RemoveEmptyParasinTb(table)
            End If
        Next

        For i As Integer = 0 To emptyHeaderParas.Count - 1
            sec.HeadersFooters.Header.ChildObjects.Remove(emptyHeaderParas(i))
        Next

        Dim emptyBodyParas As List(Of Paragraph) = New List(Of Paragraph)()
        For Each bObj As DocumentObject In sec.Body.ChildObjects
            If TypeOf bObj Is Paragraph Then
                Dim para As Paragraph = TryCast(bObj, Paragraph)
                If String.IsNullOrWhiteSpace(para.Text) Then
                    emptyBodyParas.Add(para)
                End If
            End If

            If TypeOf bObj Is Table Then
                Dim table As Table = TryCast(bObj, Table)
                RemoveEmptyParasinTb(table)
            End If
        Next

        For i As Integer = 0 To emptyBodyParas.Count - 1
            sec.Body.ChildObjects.Remove(emptyBodyParas(i))
        Next

        Dim emptyFooterParas As List(Of Paragraph) = New List(Of Paragraph)()
        For Each fObj As DocumentObject In sec.HeadersFooters.Footer.ChildObjects
            If TypeOf fObj Is Paragraph Then
                Dim para As Paragraph = TryCast(fObj, Paragraph)
                If String.IsNullOrWhiteSpace(para.Text) Then
                    emptyFooterParas.Add(para)
                End If
            End If

            If TypeOf fObj Is Table Then
                Dim table As Table = TryCast(fObj, Table)
                RemoveEmptyParasinTb(table)
            End If
        Next

        For i As Integer = 0 To emptyFooterParas.Count - 1
            sec.HeadersFooters.Footer.ChildObjects.Remove(emptyFooterParas(i))
        Next
    Next

    doc.SaveToFile("result" & fileExts, FileFormat.Auto)
    System.Diagnostics.Process.Start("result" & fileExts)
End Sub

Private Shared Sub RemoveEmptyParasinTb(ByVal table As Table)
    For i As Integer = 0 To table.Rows.Count - 1
        For j As Integer = 0 To table.Rows(i).Cells.Count - 1
            Dim tableEmptyParas As List(Of Paragraph) = New List(Of Paragraph)()
            For Each par As Paragraph In table.Rows(i).Cells(j).Paragraphs
                If String.IsNullOrWhiteSpace(par.Text) Then
                    tableEmptyParas.Add(par)
                End If
            Next

            For k As Integer = 0 To tableEmptyParas.Count - 1
                table.Rows(i).Cells(j).ChildObjects.Remove(tableEmptyParas(k))
            Next
        Next
    Next
End Sub

If the code does not work on your side, please share your sample document and the expected result!

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Dec 01, 2017 6:01 am

Thanks a lot - I will check if it works properly and let you know

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Fri Dec 01, 2017 6:21 am

Hello,

Looking forward to your reply.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Dec 01, 2017 8:53 am

Hi, the code does not work properly, for example: when an image is removed from the Word doc, an empty line shows instead.

Kindly fix and advise new code,

Thanks!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Fri Dec 01, 2017 8:59 am

Hello,

In order to help us provide a solution more effeciently, please share your sample document!

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Dec 01, 2017 9:45 am

PFA - Kindly check for image removal + tables removal as well.

Thank you!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Fri Dec 01, 2017 10:18 am

Hello,

Thanks for your response.
Kindly check the result on my side in the attachment. All the empty line has been resolved.
If it is not the result you want, please also send the expected result to us. Then we will provide the solution accordingly.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Dec 01, 2017 1:23 pm

Kindly provide the code for the result shown. Thanks!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Sun Dec 03, 2017 6:33 pm

Reminder - Kindly provide the code for the result shown. Thanks!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Mon Dec 04, 2017 2:31 am

Hello,

Please see the code below.
Code: Select all
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
         Dim path__1 = ""
         Dim fileExts As String = ""
         Dim openFileDialog As New OpenFileDialog()
         If openFileDialog.ShowDialog() = DialogResult.OK Then
            path__1 = openFileDialog.FileName
            fileExts = Path.GetExtension(path__1)
         End If

         Dim doc As New Document(path__1)

         doc= RemoveTablesAndImages(doc)
         doc= RemoveEmptyParagraphs(doc)

         doc.SaveToFile("result" & fileExts, FileFormat.Auto)
         Process.Start("result" & fileExts)
      End Sub

      Private Shared Function RemoveEmptyParagraphs(ByVal doc As Document) As Document
         For Each sec As Section In doc.Sections
            Dim emptyHeaderParas As New List(Of Paragraph)()
            For Each hObj As DocumentObject In sec.HeadersFooters.Header.ChildObjects
               If TypeOf hObj Is Paragraph Then
                  Dim para As Paragraph = TryCast(hObj, Paragraph)
                  If String.IsNullOrWhiteSpace(para.Text) Then
                     emptyHeaderParas.Add(para)
                  End If
               End If

               If TypeOf hObj Is Table Then
                  Dim table As Table = TryCast(hObj, Table)
                  RemoveEmptyParasinTb(table)
               End If
            Next hObj

            For i As Integer = 0 To emptyHeaderParas.Count - 1
               sec.HeadersFooters.Header.ChildObjects.Remove(emptyHeaderParas(i))
            Next i

            Dim emptyBodyParas As New List(Of Paragraph)()
            For Each bObj As DocumentObject In sec.Body.ChildObjects
               If TypeOf bObj Is Paragraph Then
                  Dim para As Paragraph = TryCast(bObj, Paragraph)
                  If String.IsNullOrWhiteSpace(para.Text) Then
                     emptyBodyParas.Add(para)
                  End If
               End If

               If TypeOf bObj Is Table Then
                  Dim table As Table = TryCast(bObj, Table)
                  RemoveEmptyParasinTb(table)
               End If
            Next bObj

            For i As Integer = 0 To emptyBodyParas.Count - 1
               sec.Body.ChildObjects.Remove(emptyBodyParas(i))
            Next i

            Dim emptyFooterParas As New List(Of Paragraph)()
            For Each fObj As DocumentObject In sec.HeadersFooters.Footer.ChildObjects
               If TypeOf fObj Is Paragraph Then
                  Dim para As Paragraph = TryCast(fObj, Paragraph)
                  If String.IsNullOrWhiteSpace(para.Text) Then
                     emptyFooterParas.Add(para)
                  End If
               End If

               If TypeOf fObj Is Table Then
                  Dim table As Table = TryCast(fObj, Table)
                  RemoveEmptyParasinTb(table)
               End If
            Next fObj

            For i As Integer = 0 To emptyFooterParas.Count - 1
               sec.HeadersFooters.Footer.ChildObjects.Remove(emptyFooterParas(i))
            Next i
         Next sec
         Return doc
      End Function

      Private Shared Sub RemoveEmptyParasinTb(ByVal table As Table)
         For i As Integer = 0 To table.Rows.Count - 1
            For j As Integer = 0 To table.Rows(i).Cells.Count - 1
               Dim tableEmptyParas As New List(Of Paragraph)()
               For Each par As Paragraph In table.Rows(i).Cells(j).Paragraphs
                  If String.IsNullOrWhiteSpace(par.Text) Then
                     tableEmptyParas.Add(par)
                  End If
               Next par

               For k As Integer = 0 To tableEmptyParas.Count - 1
                  table.Rows(i).Cells(j).ChildObjects.Remove(tableEmptyParas(k))
               Next k
            Next j
         Next i
      End Sub

      Private Shared Function RemoveTablesAndImages(ByVal doc As Document) As Document
         For Each s As Section In doc.Sections
            Dim hTable As New List(Of Table)()
            For Each hObject As DocumentObject In s.HeadersFooters.Header.ChildObjects
               If TypeOf hObject Is Paragraph Then
                  Dim para As Paragraph = TryCast(hObject, Paragraph)
                  RemoveImageinPara(para)
               End If
               If TypeOf hObject Is Table Then
                  Dim table As Table = TryCast(hObject, Table)
                  removeImagesinTablePar(table)
                  hTable.Add(table)
               End If
            Next hObject

            For i As Integer = 0 To hTable.Count - 1
               s.HeadersFooters.Header.ChildObjects.Remove(hTable(i))
            Next i

            Dim fTable As New List(Of Table)()
            For Each fObject As DocumentObject In s.HeadersFooters.Footer.ChildObjects
               If TypeOf fObject Is Paragraph Then
                  Dim para As Paragraph =TryCast(fObject, Paragraph)
                  RemoveImageinPara(para)
               End If
               If TypeOf fObject Is Table Then
                  Dim table As Table = TryCast(fObject, Table)
                  removeImagesinTablePar(table)
                  fTable.Add(table)
               End If
            Next fObject

            For i As Integer = 0 To fTable.Count - 1
               s.HeadersFooters.Footer.ChildObjects.Remove(fTable(i))
            Next i

            Dim secTables As New List(Of Table)()
            For Each tb As Table In s.Tables
               secTables.Add(tb)
            Next tb

            For i As Integer = 0 To secTables.Count - 1
               s.Tables.Remove(secTables(i))
            Next i
            For Each bObject As DocumentObject In s.Body.ChildObjects
               If TypeOf bObject Is Paragraph Then
                  Dim para As Paragraph = TryCast(bObject, Paragraph)
                  RemoveImageinPara(para)
               End If

               If TypeOf bObject Is Table Then
                  Dim table As Table = TryCast(bObject, Table)
                  removeImagesinTablePar(table)
               End If
            Next bObject

         Next s
         Return doc
      End Function


      Private Shared Sub removeImagesinTablePar(ByVal table As Table)
         For i As Integer = 0 To table.Rows.Count - 1
            For j As Integer = 0 To table.Rows(i).Cells.Count - 1
               For Each par As Paragraph In table.Rows(i).Cells(j).Paragraphs
                  Dim tableParaPictures As New List(Of DocPicture)()
                  For Each docObject As DocumentObject In par.ChildObjects
                     If TypeOf docObject Is DocPicture Then
                        tableParaPictures.Add(TryCast(docObject, DocPicture))
                     End If
                  Next docObject

                  For k As Integer = 0 To tableParaPictures.Count - 1
                     par.ChildObjects.Remove(tableParaPictures(k))
                  Next k
               Next par
            Next j
         Next i
      End Sub

      Private Shared Sub RemoveImageinPara(ByVal para As Paragraph)
         Dim headerParaPictures As New List(Of DocPicture)()
         For Each obj As DocumentObject In para.ChildObjects
            If TypeOf obj Is DocPicture Then
               headerParaPictures.Add(TryCast(obj, DocPicture))
            End If
         Next obj

         For i As Integer = 0 To headerParaPictures.Count - 1
            para.ChildObjects.Remove(headerParaPictures(i))
         Next i
      End Sub


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Mon Dec 04, 2017 4:38 am

Thanks a lot - I will check if it works properly and let you know

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Fri Dec 08, 2017 6:25 am

Hello,

Greetings from e-iceblue!
I wonder if the issue has been resolved.
Thanks in advance for your time and feedback.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc