From 167c792bf0f2a3b703764d541e6a37dc3b8ba309 Mon Sep 17 00:00:00 2001 From: Omar Ahmed Hassan Date: Thu, 19 Dec 2024 13:34:13 +0200 Subject: [PATCH] Draw image with the original PDF page dimensions --- src/main/java/stirling/software/SPDF/utils/PdfUtils.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index fb2ffd59..55a83cf2 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -352,12 +352,17 @@ public class PdfUtils { pdfRenderer.setSubsamplingAllowed(true); for (int page = 0; page < document.getNumberOfPages(); ++page) { BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB); - PDPage newPage = new PDPage(new PDRectangle(bim.getWidth(), bim.getHeight())); + PDPage originalPage = document.getPage(page); + + float width = originalPage.getMediaBox().getWidth(); + float height = originalPage.getMediaBox().getHeight(); + + PDPage newPage = new PDPage(new PDRectangle(width, height)); imageDocument.addPage(newPage); PDImageXObject pdImage = LosslessFactory.createFromImage(imageDocument, bim); PDPageContentStream contentStream = new PDPageContentStream(imageDocument, newPage, AppendMode.APPEND, true, true); - contentStream.drawImage(pdImage, 0, 0); + contentStream.drawImage(pdImage, 0, 0, width, height); contentStream.close(); } return imageDocument;