diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 22e09d6a..000a8294 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -205,11 +205,6 @@ public class PdfUtils { int pageCount = document.getNumberOfPages(); List images = new ArrayList<>(); - // Create images of all pages - for (int i = 0; i < pageCount; i++) { - images.add(pdfRenderer.renderImageWithDPI(i, DPI, colorType)); - } - // Create a ByteArrayOutputStream to save the image(s) to ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -226,8 +221,8 @@ public class PdfUtils { writer.setOutput(ios); writer.prepareWriteSequence(null); - for (int i = 0; i < images.size(); ++i) { - BufferedImage image = images.get(i); + for (int i = 0; i < pageCount; ++i) { + BufferedImage image = pdfRenderer.renderImageWithDPI(i, DPI, colorType); writer.writeToSequence(new IIOImage(image, null, null), param); } @@ -240,8 +235,9 @@ public class PdfUtils { BufferedImage combined = new BufferedImage(images.get(0).getWidth(), images.get(0).getHeight() * pageCount, BufferedImage.TYPE_INT_RGB); Graphics g = combined.getGraphics(); - for (int i = 0; i < images.size(); i++) { - g.drawImage(images.get(i), 0, i * images.get(0).getHeight(), null); + for (int i = 0; i < pageCount; ++i) { + BufferedImage image = pdfRenderer.renderImageWithDPI(i, DPI, colorType); + g.drawImage(image, 0, i * image.getHeight(), null); } // Write the image to the output stream @@ -253,8 +249,8 @@ public class PdfUtils { } else { // Zip the images and return as byte array try (ZipOutputStream zos = new ZipOutputStream(baos)) { - for (int i = 0; i < images.size(); i++) { - BufferedImage image = images.get(i); + for (int i = 0; i < pageCount; ++i) { + BufferedImage image = pdfRenderer.renderImageWithDPI(i, DPI, colorType); try (ByteArrayOutputStream baosImage = new ByteArrayOutputStream()) { ImageIO.write(image, imageType, baosImage);