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!
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
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