We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Impossible to remove all images from page

Hello,

I need to remove images from .pdf by condition. When I remove only 1 image everything is fine but even with two images it fails.
Demo which shows that it’s not possible to remove all (two) images from the second page of a test document:
https://drive.google.com/file/d/0B-CEaN01bFt3VWRpRWtwVklCaDA/view?usp=sharing

Aspose version: 17.9.0.0

Code which is used to remove images:

Public Shared Sub RemoveImages(ByVal pathToDocument As String, ByVal pathToOutputDocument As String)
Dim ImagesToRemove As New List(Of Tuple(Of Integer, Integer)) 'Item1=PageNumber, Item2=ImageIndex

    Dim AsposeLicense As New Aspose.Pdf.License()
    AsposeLicense.SetLicense("Aspose.Total.lic")

    Using PdfDocument As New Aspose.Pdf.Document(pathToDocument)
        For i As Integer = 1 To PdfDocument.Pages.Count
            Using Page As Aspose.Pdf.Page = PdfDocument.Pages(i)
                Dim ImagePlacementAbsorber As New Aspose.Pdf.ImagePlacementAbsorber
                Page.Accept(ImagePlacementAbsorber)
                If ImagePlacementAbsorber IsNot Nothing AndAlso ImagePlacementAbsorber.ImagePlacements IsNot Nothing AndAlso ImagePlacementAbsorber.ImagePlacements.Count > 0 Then
                    For j As Integer = 1 To ImagePlacementAbsorber.ImagePlacements.Count
                        Dim ImagePlacement As Aspose.Pdf.ImagePlacement = ImagePlacementAbsorber.ImagePlacements(j)
                        If i = 2 Then 'i is number of Page
                            ImagesToRemove.Add(New Tuple(Of Integer, Integer)(i, j))
                        End If
                    Next
                End If
            End Using
        Next
    End Using

    RemoveImagesFromPdf(pathToDocument, ImagesToRemove, pathToOutputDocument)
End Sub

Private Shared Sub RemoveImagesFromPdf(ByVal pathToDocument As String, _
                                       ByVal imagesToRemove As List(Of Tuple(Of Integer, Integer)), _
                                       ByVal pathToOutputDocument As String)
    If imagesToRemove IsNot Nothing AndAlso imagesToRemove.Count > 0 Then
        Using PdfDocument As New Aspose.Pdf.Document(pathToDocument)
            For i As Integer = 0 To imagesToRemove.Count - 1
                PdfDocument.Pages(imagesToRemove(i).Item1).Resources.Images.Delete(imagesToRemove(i).Item2)
            Next

            PdfDocument.Save(pathToOutputDocument)
        End Using
    End If
End Sub

@tak,

Please modify the code of method RemoveImagesFromPdf as follows:

[VB.NET]

Private Sub RemoveImagesFromPdf(ByVal pathToDocument As String,
                                   ByVal imagesToRemove As List(Of Tuple(Of Integer, Integer)),
                                   ByVal pathToOutputDocument As String)
    If imagesToRemove IsNot Nothing AndAlso imagesToRemove.Count > 0 Then
        Using PdfDocument As New Aspose.Pdf.Document(pathToDocument)
            Dim count As Integer = 0
            For i As Integer = 0 To imagesToRemove.Count - 1                    
 PdfDocument.Pages(imagesToRemove(i).Item1).Resources.Images.Delete(imagesToRemove(i).Item2 - count)
        count = count + 1
    Next

    PdfDocument.Save(pathToOutputDocument)
End Using
        End If
    End Sub

This is the output PDF with version 17.10: Processed.pdf (346.9 KB)

Thank you, modified version is able to remove all images from the second page.
But after attempt to remove images from both first and second page I have another problem: updated .pdf is saved but Adobe shows error after opening it and scrolling to the second page:
“An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF to correct the problem.”

Demo project which shows new problem:
https://drive.google.com/file/d/0B-CEaN01bFt3Zi1JbmQ4WVo4OTg/view?usp=sharing

@tak,

We managed to replicate the said error in our environment. It has been logged under the ticket ID PDFNET-43588 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.