Skip to content

Commit

Permalink
Draw image with the original PDF page dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-ahmed42 committed Dec 19, 2024
1 parent 95a9e10 commit 167c792
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/main/java/stirling/software/SPDF/utils/PdfUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,17 @@ public static PDDocument convertPdfToPdfImage(PDDocument document) throws IOExce
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;
Expand Down

0 comments on commit 167c792

Please sign in to comment.