PDF to Jpeg Conversion not working


#1

when trying to convert a pdf to jpeg using the following code
(i have attached the pdf if you need to test)

public void ConvertPDFToJPEG(byte[] PDFBlob , int resolution , string dataDir)
{
// Open document
using (MemoryStream InputStream = new MemoryStream(PDFBlob))
{
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(InputStream);

            for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
            {

                using (FileStream imageStream = new FileStream(dataDir + "image" + pageCount + "_out" + ".jpg", FileMode.Create))
                {
                    // Create JPEG device with specified attributes
                    // Width, Height, Resolution, Quality
                    // Quality [0-100], 100 is Maximum
                    // Create Resolution object

                    Aspose.Pdf.Devices.Resolution res = new Aspose.Pdf.Devices.Resolution(resolution);
                   
                    int height = 0, width = 0;
                    
                    Aspose.Pdf.Facades.PdfFileInfo info = new Aspose.Pdf.Facades.PdfFileInfo(pdfDocument);
                    width = Convert.ToInt32(ConvertUtil.PointToPixel(info.GetPageWidth(pdfDocument.Pages[pageCount].Number), resolution));
                    height = Convert.ToInt32(ConvertUtil.PointToPixel(info.GetPageHeight(pdfDocument.Pages[pageCount].Number), resolution));
                    
                    Aspose.Pdf.Devices.JpegDevice jpegDevice;
                    jpegDevice =
                        new Aspose.Pdf.Devices.JpegDevice(width, height, res, 100);
                    jpegDevice.Process(pdfDocument.Pages[pageCount], imageStream);
                    // Close stream
                    imageStream.Close();
                }
            }
        }
    }

an exception is thrown when the jpefdevice.Process function is called. Comes back with an exception
System.ArgumentException: ‘Parameter is not valid.’

here is the stack trace

at System.Drawing.Bitmap…ctor(Int32 width, Int32 height, PixelFormat format)
at #=zXJxbTQBu_62mzPmusqpnu6WXfc3i.#=zEBI$5Zc=(Int32 #=z8RTnEV4=, Int32 #=z_ddWRJs=, PixelFormat #=zn5srW8g=)
at #=zgNo5e0c9gHO8iAzPeGDDCG48ORmmIOo5uP2LGVs=…ctor(#=zF5qJTWkY_5iJfiydt_J2ayi2QRd$m1CH0$YO8AA= #=zw8I5NEo=, #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BJh6eo=(Page #=zP0xB2cg=)
at Aspose.Pdf.Devices.JpegDevice.Process(Page page, Stream output)
at Conversion.Converters.AsposeConverter.ConvertPDFToJPEG(Byte[] PDFBlob, Int32 resolution, String dataDir) in C:\Civica\WorkflowEngine\ib0300 workflow 2 core\Conversion\Converters\AsposeConverter.cs:line 348
at Conversion.ConversionService.ConvertPDFToJPEG(Byte[] PDFBlob, Int32 resolution, String dataDir) in C:\Civica\WorkflowEngine\ib0300 workflow 2 core\Conversion\ConversionService.cs:line 107

the pdf can be viewed fine , but i think there maybe an issue with it as it cannot be converted to a jpeg.
Ive also opened it in google chrome and right clicked -> save as a jpeg , but then windows cannot open the jpeg as it states it not the correct format.

Any Ideas?
Z03100-DAV-NA-PH2-DR-L-0132_Existing Vegetation as Current_Planning_P 2.pdf (1.1 MB)


#2

@DazzaBee123

We have tested the scenario in our environment using following simple code snippet with Aspose.PDF for .NET 19.10 and were unable to notice the exception you stated.

using (Document doc = new Document(dataDir + "Z03100.pdf"))
{
 foreach(Page page in doc.Pages)
 {
  Rectangle rect = page.GetPageRect(true);
  JpegDevice jpegDevice = new JpegDevice((int)rect.Width, (int)rect.Height, new Resolution(300), 100);
  jpegDevice.Process(page, dataDir + page.Number + ".jpg");
 }
}

For your kind reference, an output image is also attached.

1.jpg (1.7 MB)

Would you kindly try with latest version of the API and in case issue still persists, please feel free to let us know.