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.

Sat Nov 25, 2017 3:48 pm

Dear support team,
Kindly provide VB.Net script for removing all images/pictures from Word doc and saving changes to same file.

Thank you!

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

Mon Nov 27, 2017 3:47 am

Hello,

Thanks for your inquiry.
Below is the code for your kind reference.
Code: Select all

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


Sincerely,
Jane
E-iceblue support team
User avatar

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

Mon Nov 27, 2017 6:11 pm

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

Mon Nov 27, 2017 6:56 pm

This code works - Please provide a code that supports both DOCX and DOC.

Thank you!

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

Tue Nov 28, 2017 2:41 am

Hi,

Please refer to the code below.
Code: Select all
    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



Sincerely,
Jane
E-iceblue support team
User avatar

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

Wed Nov 29, 2017 2:50 am

Hello,

Greetings form e-iceblue!
Has your issue been resolved?
Your feedback will be greatly appreciated.

Sincerely,
Jane
E-iceblue support team
User avatar

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

Return to Spire.Doc