From 9da88b7652835cdc17c18fae749d527bf39e1caf Mon Sep 17 00:00:00 2001 From: mannam <101550345+ManoharMannam@users.noreply.github.com> Date: Fri, 9 Feb 2024 19:35:34 +0530 Subject: [PATCH 1/2] fixed wrong filename generation through stamp tool #757 --- .../controller/api/misc/StampController.java | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java index 99a5e2df..bb467331 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java @@ -48,13 +48,13 @@ public class StampController { @Operation( summary = "Add stamp to a PDF file", description = - "This endpoint adds a stamp to a given PDF file. Users can specify the watermark type (text or image), rotation, opacity, width spacer, and height spacer. Input:PDF Output:PDF Type:SISO") + "This endpoint adds a stamp to a given PDF file. Users can specify the stamp type (text or image), rotation, opacity, width spacer, and height spacer. Input:PDF Output:PDF Type:SISO") public ResponseEntity addStamp(@ModelAttribute AddStampRequest request) throws IOException, Exception { MultipartFile pdfFile = request.getFileInput(); - String watermarkType = request.getStampType(); - String watermarkText = request.getStampText(); - MultipartFile watermarkImage = request.getStampImage(); + String stampType = request.getStampType(); + String stampText = request.getStampText(); + MultipartFile stampImage = request.getStampImage(); String alphabet = request.getAlphabet(); float fontSize = request.getFontSize(); float rotation = request.getRotation(); @@ -99,10 +99,10 @@ public class StampController { graphicsState.setNonStrokingAlphaConstant(opacity); contentStream.setGraphicsStateParameters(graphicsState); - if ("text".equalsIgnoreCase(watermarkType)) { + if ("text".equalsIgnoreCase(stampType)) { addTextStamp( contentStream, - watermarkText, + stampText, document, page, rotation, @@ -113,10 +113,10 @@ public class StampController { overrideY, margin, customColor); - } else if ("image".equalsIgnoreCase(watermarkType)) { + } else if ("image".equalsIgnoreCase(stampType)) { addImageStamp( contentStream, - watermarkImage, + stampImage, document, page, rotation, @@ -134,12 +134,12 @@ public class StampController { document, Filenames.toSimpleFileName(pdfFile.getOriginalFilename()) .replaceFirst("[.][^.]+$", "") - + "_watermarked.pdf"); + + "_stamped.pdf"); } private void addTextStamp( PDPageContentStream contentStream, - String watermarkText, + String stampText, PDDocument document, PDPage page, float rotation, @@ -208,9 +208,7 @@ public class StampController { x = overrideX; y = overrideY; } else { - x = - calculatePositionX( - pageSize, position, fontSize, font, fontSize, watermarkText, margin); + x = calculatePositionX(pageSize, position, fontSize, font, fontSize, stampText, margin); y = calculatePositionY( pageSize, position, calculateTextCapHeight(font, fontSize), margin); @@ -218,13 +216,13 @@ public class StampController { contentStream.beginText(); contentStream.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(rotation), x, y)); - contentStream.showText(watermarkText); + contentStream.showText(stampText); contentStream.endText(); } private void addImageStamp( PDPageContentStream contentStream, - MultipartFile watermarkImage, + MultipartFile stampImage, PDDocument document, PDPage page, float rotation, @@ -235,8 +233,8 @@ public class StampController { float margin) throws IOException { - // Load the watermark image - BufferedImage image = ImageIO.read(watermarkImage.getInputStream()); + // Load the stamp image + BufferedImage image = ImageIO.read(stampImage.getInputStream()); // Compute width based on original aspect ratio float aspectRatio = (float) image.getWidth() / (float) image.getHeight(); From a4f923eb3a0c7f26cce156177dba92d57d8f0942 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sat, 10 Feb 2024 11:19:22 +0000 Subject: [PATCH 2/2] Update StampController.java --- .../controller/api/misc/StampController.java | 39 +------------------ 1 file changed, 1 insertion(+), 38 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java index eec48821..68a65d71 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/StampController.java @@ -88,44 +88,7 @@ public class StampController { // Load the input PDF PDDocument document = Loader.loadPDF(pdfFile.getBytes()); - for (PDPage page : document.getPages()) { - PDRectangle pageSize = page.getMediaBox(); - float margin = marginFactor * (pageSize.getWidth() + pageSize.getHeight()) / 2; - - PDPageContentStream contentStream = - new PDPageContentStream( - document, page, PDPageContentStream.AppendMode.APPEND, true, true); - - PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); - graphicsState.setNonStrokingAlphaConstant(opacity); - contentStream.setGraphicsStateParameters(graphicsState); - - if ("text".equalsIgnoreCase(stampType)) { - addTextStamp( - contentStream, - stampText, - document, - page, - rotation, - position, - fontSize, - alphabet, - overrideX, - overrideY, - margin, - customColor); - } else if ("image".equalsIgnoreCase(stampType)) { - addImageStamp( - contentStream, - stampImage, - document, - page, - rotation, - position, - fontSize, - overrideX, - overrideY, - margin); + List pageNumbers = request.getPageNumbersList();