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

Free Support Forum - aspose.com

Convert PDF to Images in Java using Aspose.PDF - API is too slow to generate pictures

Pdf is too slow to generate pictures, is there any way to speed it up or is there any problem with my code?

public static void main(String[] args) {
        String inputFileName = Paths.get("aaa.pdf").toString();
        Document pdfDocument = new Document(inputFileName);

        try {
            for (int pageCount = 1; pageCount <= pdfDocument.getPages().size(); pageCount++) {
                // Create stream object to save the output image
                String outputFilePath = "aaa.jpg";
                java.io.OutputStream imageStream = new java.io.FileOutputStream(outputFilePath);
                Resolution resolution = new Resolution(300);
                JpegDevice JpegDevice = new JpegDevice(resolution);
                JpegDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream);
                imageStream.close();
            }
        } catch (FileNotFoundException e) {

        } catch (IOException e) {

        } finally {
            if (pdfDocument != null) {
                pdfDocument.dispose();
            }
        }

    }

<a class="attachment" href="/uploads/discourse_instance3/52674">image.png</a> (735.4 KB)

@zyx

The code seems fine. Could you please share which JDK version are your using? Also, please share your sample PDF document with us so that we can test the scenario in our environment and address it accordingly.

jdk1.8 aspose 21.6

@zyx

Please note that the API performance depends upon the size of PDF document as well as its complexity and structure. We used the below code snippet at our end noticed that the conversion time was different in case of each page of the PDF:

try {
            Document pdfDocument = new Document(dataDir + "2021毕业生_王宁_平面设计师能力需求数据可视化.pdf");
            for (Page page : pdfDocument.getPages()) {
                long totalStart = System.currentTimeMillis();
                java.io.OutputStream imageStream = new java.io.FileOutputStream(dataDir + "Converted_Image_" + page.getNumber() + ".jpg");
                com.aspose.pdf.devices.Resolution resolution = new com.aspose.pdf.devices.Resolution(300);
                com.aspose.pdf.devices.JpegDevice jpegDevice = new com.aspose.pdf.devices.JpegDevice(resolution);
                jpegDevice.process(page, imageStream);
                imageStream.close();
                long totalEnd = System.currentTimeMillis();
                System.out.println("Page Number=" + page.getNumber() + " Total time taken was " + (totalEnd - totalStart) / 1000 + " seconds for conversion");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

Output

Page Number=1 Total time taken was 34 seconds for conversion
Page Number=2 Total time taken was 17 seconds for conversion
Page Number=3 Total time taken was 7 seconds for conversion
Page Number=4 Total time taken was 7 seconds for conversion
Page Number=5 Total time taken was 24 seconds for conversion
Page Number=6 Total time taken was 23 seconds for conversion
Page Number=7 Total time taken was 7 seconds for conversion
Page Number=8 Total time taken was 7 seconds for conversion
Page Number=9 Total time taken was 7 seconds for conversion
Page Number=10 Total time taken was 5 seconds for conversion
Page Number=11 Total time taken was 80 seconds for conversion
Page Number=12 Total time taken was 15 seconds for conversion
Page Number=13 Total time taken was 40 seconds for conversion
Page Number=14 Total time taken was 7 seconds for conversion
Page Number=15 Total time taken was 249 seconds for conversion
Page Number=16 Total time taken was 17 seconds for conversion
Page Number=17 Total time taken was 371 seconds for conversion

Can you please use this code snippet at your side and share the console output with us?