From fdbc7f46212926fcd3debfc5b9f0b4ad36a81c49 Mon Sep 17 00:00:00 2001 From: libertyunion <123750499+libertyunion@users.noreply.github.com> Date: Tue, 11 Apr 2023 16:22:59 -0400 Subject: [PATCH] Added support for multiple adding an image overlay to multiple pages (#86) --- .../controller/OverlayImageController.java | 4 +- .../software/SPDF/utils/PdfUtils.java | 45 +++++++++++-------- src/main/resources/templates/add-image.html | 3 ++ 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/OverlayImageController.java b/src/main/java/stirling/software/SPDF/controller/OverlayImageController.java index 455915bc..54ccd5a1 100644 --- a/src/main/java/stirling/software/SPDF/controller/OverlayImageController.java +++ b/src/main/java/stirling/software/SPDF/controller/OverlayImageController.java @@ -28,11 +28,11 @@ public class OverlayImageController { @PostMapping("/add-image") public ResponseEntity overlayImage(@RequestParam("fileInput") MultipartFile pdfFile, @RequestParam("fileInput2") MultipartFile imageFile, @RequestParam("x") float x, - @RequestParam("y") float y) { + @RequestParam("y") float y, @RequestParam("everyPage") boolean everyPage) { try { byte[] pdfBytes = pdfFile.getBytes(); byte[] imageBytes = imageFile.getBytes(); - byte[] result = PdfUtils.overlayImage(pdfBytes, imageBytes, x, y); + byte[] result = PdfUtils.overlayImage(pdfBytes, imageBytes, x, y, everyPage); return PdfUtils.bytesToWebResponse(result, pdfFile.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_overlayed.pdf"); } catch (IOException e) { diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 030f9e2b..4ab33e60 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -188,29 +188,36 @@ public class PdfUtils { } } - public static byte[] overlayImage(byte[] pdfBytes, byte[] imageBytes, float x, float y) throws IOException { + public static byte[] overlayImage(byte[] pdfBytes, byte[] imageBytes, float x, float y, boolean everyPage) throws IOException { + + PDDocument document = PDDocument.load(new ByteArrayInputStream(pdfBytes)); - try (PDDocument document = PDDocument.load(new ByteArrayInputStream(pdfBytes))) { // Get the first page of the PDF - PDPage page = document.getPage(0); - try (PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true)) { - // Create an image object from the image bytes - PDImageXObject image = PDImageXObject.createFromByteArray(document, imageBytes, ""); - // Draw the image onto the page at the specified x and y coordinates - contentStream.drawImage(image, x, y); - logger.info("Image successfully overlayed onto PDF"); + int pages = document.getNumberOfPages(); + for (int i = 0; i < pages; i++) { + PDPage page = document.getPage(i); + try (PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true)) { + // Create an image object from the image bytes + PDImageXObject image = PDImageXObject.createFromByteArray(document, imageBytes, ""); + // Draw the image onto the page at the specified x and y coordinates + contentStream.drawImage(image, x, y); + logger.info("Image successfully overlayed onto PDF"); + if (everyPage == false && i == 0) { + break; + } + } catch (IOException e) { + // Log an error message if there is an issue overlaying the image onto the PDF + logger.error("Error overlaying image onto PDF", e); + throw e; + } + } - // Create a ByteArrayOutputStream to save the PDF to - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - document.save(baos); - logger.info("PDF successfully saved to byte array"); - return baos.toByteArray(); - } catch (IOException e) { - // Log an error message if there is an issue overlaying the image onto the PDF - logger.error("Error overlaying image onto PDF", e); - throw e; + // Create a ByteArrayOutputStream to save the PDF to + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + document.save(baos); + logger.info("PDF successfully saved to byte array"); + return baos.toByteArray(); } - } public static ResponseEntity iTextDocToWebResponse(Document document, String docName) throws IOException, DocumentException { // Close the document diff --git a/src/main/resources/templates/add-image.html b/src/main/resources/templates/add-image.html index ee8d1e4e..a74707f8 100644 --- a/src/main/resources/templates/add-image.html +++ b/src/main/resources/templates/add-image.html @@ -26,6 +26,9 @@
+
+ +