Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Sat Apr 11, 2020 12:06 pm

Hi,
I'm using the sample vb.net demo to extract text and images from pdf files.
However some of my pdf's do not contain images so the program throws an exception at the next statement

How do I check for the condition and prevent the exception

For Each image As Image In page.ExtractImages
images.Add(image)
Next

regards

lfcnutter
 
Posts: 7
Joined: Tue Apr 07, 2020 9:48 am

Mon Apr 13, 2020 1:43 am

Hello,

Thanks for your inquiry and sorry for the late reply as weekend.
Please refer to the below code to extract images from PDF documents. If there is any question, please provide your input file for further investigation.
Code: Select all
        Dim doc As PdfDocument = New PdfDocument
        'Load a PDF file
        doc.LoadFromFile("test.pdf")
        Dim ListImage As List(Of Image) = New List(Of Image)
        Dim i As Integer = 0
        Do While (i < doc.Pages.Count)
            ' Get an object of Spire.Pdf.PdfPageBase
            Dim page As PdfPageBase = doc.Pages(i)
            ' Extract images from Spire.Pdf.PdfPageBase
            Dim images() As Image = page.ExtractImages
            If ((Not (images) Is Nothing) AndAlso (images.Length > 0)) Then
                ListImage.AddRange(images)
            End If
            i = (i + 1)
        Loop

        If (ListImage.Count > 0) Then
            Dim j As Integer = 0
            Do While (j < ListImage.Count)
                Dim image As Image = ListImage(j)
                image.Save(String.Format("image-{0}.png", (j + 1)), ImageFormat.Png)
                j = (j + 1)
            Loop

        End If


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Apr 13, 2020 1:26 pm

Hi Rachel
that did the trick
thanks for your help

lfcnutter
 
Posts: 7
Joined: Tue Apr 07, 2020 9:48 am

Tue Apr 14, 2020 1:04 am

Hello,

Thanks for your feedback.
If you encounter any question related to our product in the future, just feel free to contact us.
Have a nice day!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF