Convert PDF to JPG using Aspose.PDF for .NET - string “image” to type ‘Double’ is not valid

hi,
when i try to convert PDF to Jpeg i get the next Error
ERROR:Conversion from string “image” to type ‘Double’ is not valid.
igave you sample of the code and the file

           Dim pdfDocument As Aspose.pdf.Document = New Aspose.pdf.Document(fileFolder + fileName)
      Try
            '   ' -----===== remove hyperLinks  
            For pageCount As Integer = 1 To pdfDocument.Pages.Count
            
              Using imageStream As FileStream = New FileStream(fileFolder & "image" + pageCount & "_out" & ".jpg", FileMode.Create)
              Dim resolution As Resolution = New Resolution(300)
              Dim jpegDevice As JpegDevice = New JpegDevice(resolution, 100)
              jpegDevice.Process(pdfDocument.Pages(pageCount), imageStream)
              imageStream.Close()
              End Using
            Next

              Catch exp As Exception

    		              '---=== response binary for debuging ===--
                      '================================================================================================================
                     Response.Clear()
                      Response.ClearContent()
                      Response.ClearHeaders()

                      Response.TrySkipIisCustomErrors = true
                      Response.StatusCode = 205
                      Response.ContentType = "text/plain"
                      Response.Write("ERROR:"&exp.Message)
                     Response.end
                      '================================================================================================================
                               
                End Try 
      
        
                Dim docStream As New MemoryStream()  
                pdfDocument.Save(docStream)


        Response.Clear()
        Response.ClearContent()
        Response.ClearHeaders()
         Response.StatusCode = 202
        Response.AddHeader("Content-Description", "File Transfer")
        Response.ContentType = "application/pdf" 
        Response.AddHeader("content-disposition", "attachment; filename=test.pdf")
        Response.AddHeader("Content-Transfer-Encoding", "binary")
         docStream.WriteTo(Response.OutputStream)
       response.Write(pdfDocument)
      response.end

094937Anastasya Mingalyov KOROT HAIM (1) (1).pdf (81.2 KB)

@eranlipi

We used following code snippet to test the scenario with Aspose.PDF for .NET 20.11 and did not notice any issue. Resultant images are also attached for your reference.

var fs = new FileStream(dataDir + "094937Anastasya Mingalyov KOROT HAIM (1) (1).pdf", FileMode.Open);
Document pdfDocument = new Document(fs);
foreach (Page page in pdfDocument.Pages)
{
 using (FileStream imageStream = new FileStream(dataDir + "image" + page.Number + ".jpg", FileMode.Create))
 {
  Resolution resolution = new Resolution(300);
  JpegDevice jpegDeviceLarge = new JpegDevice(resolution, 100);
  jpegDeviceLarge.RenderingOptions.InterpolationHighQuality = true;
  jpegDeviceLarge.RenderingOptions.UseNewImagingEngine = true;
  jpegDeviceLarge.Process(page, imageStream);
 }
}

image2.jpg (481.2 KB)
image1.jpg (853.8 KB)

Would you kindly share a sample console application which is able to reproduce the issue so that we can test the scenario in our environment and address it accordingly.

at the end i want to create binary file response.
how can i do it?

@eranlipi

You can try adding following code snippet after conversion:

/////////
jpegDeviceLarge.Process(page, imageStream);
System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream);
using (MemoryStream ms = new MemoryStream())
{
 image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
 ms.WriteTo(Response.OutputStream);
}

@asad.ali

i get this error " Compiler Error Message: BC30002: Type ‘Aspose.Pdf.Drawing.Image’ is not defined." i used the last aspose.pdf version

Dim fs = New FileStream(fileFolder + fileName, FileMode.Open)
Dim pdfDocument As  Aspose.Pdf.Document = New  Aspose.Pdf.Document(fs)

For Each page As Page In pdfDocument.Pages

    Using imageStream As FileStream = New FileStream(fileFolder & "image" + page.Number & ".jpg", FileMode.Create)
        Dim resolution As Resolution = New Resolution(300)
        Dim jpegDeviceLarge As JpegDevice = New JpegDevice(resolution, 100)
        jpegDeviceLarge.RenderingOptions.InterpolationHighQuality = True
        jpegDeviceLarge.RenderingOptions.UseNewImagingEngine = True
        jpegDeviceLarge.Process(page, imageStream)
        Dim image As Aspose.Pdf.Drawing.Image = Aspose.Pdf.Drawing.Image.FromStream(imageStream)

Using ms As MemoryStream = New MemoryStream()
    image.Save(ms, Drawing.Imaging.ImageFormat.Png)
    ms.WriteTo(Response.OutputStream)
End Using
    End Using
Next

@eranlipi

Would you kindly share your sample application with us in .zip format. We will test the scenario in our environment and address it accordingly. You can create a sample application as well which only includes the functionality being discussed here and share with us.

@asad.ali

thank you :slight_smile:
pdf2image.aspx.zip (2.0 KB)

@eranlipi

The culprit is above line of code. Please note that you need to use System.Drawing.Image instead of Aspose.Pdf.Drawing.Image like following;

Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(imageStream)

@asad.ali

hi.
is work. but i dont want to save the file inside folder. i want to save at MemoryStream().
how can i do it?

at docx is work but i fail at pdf.

thank you :slight_smile:

@eranlipi

You can use a memory stream in below line of code instead of FileStream:

' Second argument can be a MemoryStream
jpegDeviceLarge.Process(page, imageStream)