Dear support team,
Kindly provide VB.Net script for removing all images/pictures from Word doc and saving changes to same file.
Thank you!
Private Sub button15_Click(sender As Object, e As EventArgs)
Dim document As New Document("C:\Users\Administrator\Desktop\sample.docx")
For Each sec As Section In document.Sections
'1. remove the images in the header
For Each hObject As DocumentObject In sec.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)
End If
Next
'2. remove the images in the footer
For Each fObject As DocumentObject In sec.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)
End If
Next
'3. remove the pictures in boby
For Each bObject As DocumentObject In sec.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
document.SaveToFile("..\..\result.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("..\..\result.docx")
Next
End Sub
Private Shared Sub removeImagesinTablePar(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
'traverse paragraph in the cell
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
For k As Integer = 0 To tableParaPictures.Count - 1
par.ChildObjects.Remove(tableParaPictures(k))
Next
Next
Next
Next
End Sub
Private Shared Sub RemoveImageinPara(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
For i As Integer = 0 To headerParaPictures.Count - 1
para.ChildObjects.Remove(headerParaPictures(i))
Next
End Sub
Private Sub button15_Click(sender As Object, e As EventArgs)
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 document As New Document(path__1)
For Each sec As Section In document.Sections
'1. remove the images in the header
For Each hObject As DocumentObject In sec.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)
End If
Next
'2. remove the images in the footer
For Each fObject As DocumentObject In sec.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)
End If
Next
'3. remove the pictures in boby
For Each bObject As DocumentObject In sec.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
document.SaveToFile("result" + fileExts, FileFormat.Auto)
System.Diagnostics.Process.Start("result" + fileExts)
Next
End Sub
Private Shared Sub removeImagesinTablePar(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
'traverse paragraph in the cell
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
For k As Integer = 0 To tableParaPictures.Count - 1
par.ChildObjects.Remove(tableParaPictures(k))
Next
Next
Next
Next
End Sub
Private Shared Sub RemoveImageinPara(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
For i As Integer = 0 To headerParaPictures.Count - 1
para.ChildObjects.Remove(headerParaPictures(i))
Next
End Sub