diff --git a/src/main/java/stirling/software/SPDF/controller/api/SplitPDFController.java b/src/main/java/stirling/software/SPDF/controller/api/SplitPDFController.java index 44c87464..a69dea34 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/SplitPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/SplitPDFController.java @@ -50,7 +50,7 @@ public class SplitPDFController { PDDocument document = Loader.loadPDF(file.getBytes()); - List pageNumbers = request.getPageNumbersList(document); + List pageNumbers = request.getPageNumbersList(document, true); if (!pageNumbers.contains(document.getNumberOfPages() - 1)) pageNumbers.add(document.getNumberOfPages() - 1); logger.info( 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 9e53292e..e11ffbaf 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 @@ -49,13 +49,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(); @@ -88,7 +88,9 @@ public class StampController { // Load the input PDF PDDocument document = Loader.loadPDF(pdfFile.getBytes()); - List pageNumbers = request.getPageNumbersList(); + + + List pageNumbers = request.getPageNumbersList(document, false); for (int pageIndex : pageNumbers) { int zeroBasedIndex = pageIndex - 1; @@ -105,10 +107,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, @@ -119,10 +121,10 @@ public class StampController { overrideY, margin, customColor); - } else if ("image".equalsIgnoreCase(watermarkType)) { + } else if ("image".equalsIgnoreCase(stampType)) { addImageStamp( contentStream, - watermarkImage, + stampImage, document, page, rotation, @@ -140,12 +142,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, @@ -214,9 +216,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); @@ -224,13 +224,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, @@ -241,8 +241,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(); @@ -319,4 +319,4 @@ public class StampController { private float calculateTextCapHeight(PDFont font, float fontSize) { return font.getFontDescriptor().getCapHeight() / 1000 * fontSize; } -} +} \ No newline at end of file diff --git a/src/main/java/stirling/software/SPDF/model/api/PDFWithPageNums.java b/src/main/java/stirling/software/SPDF/model/api/PDFWithPageNums.java index 5465988c..850eb927 100644 --- a/src/main/java/stirling/software/SPDF/model/api/PDFWithPageNums.java +++ b/src/main/java/stirling/software/SPDF/model/api/PDFWithPageNums.java @@ -24,8 +24,9 @@ public class PDFWithPageNums extends PDFFile { "The pages to select, Supports ranges (e.g., '1,3,5-9'), or 'all' or functions in the format 'an+b' where 'a' is the multiplier of the page number 'n', and 'b' is a constant (e.g., '2n+1', '3n', '6n-5')\"") private String pageNumbers; + @Hidden - public List getPageNumbersList() { + public List getPageNumbersList(boolean zeroCount) { int pageCount = 0; try { pageCount = Loader.loadPDF(getFileInput().getBytes()).getNumberOfPages(); @@ -33,13 +34,13 @@ public class PDFWithPageNums extends PDFFile { // TODO Auto-generated catch block e.printStackTrace(); } - return GeneralUtils.parsePageString(pageNumbers, pageCount); + return GeneralUtils.parsePageString(pageNumbers, pageCount, zeroCount); } - + @Hidden - public List getPageNumbersList(PDDocument doc) { + public List getPageNumbersList(PDDocument doc,boolean zeroCount ) { int pageCount = 0; pageCount = doc.getNumberOfPages(); - return GeneralUtils.parsePageString(pageNumbers, pageCount); + return GeneralUtils.parsePageString(pageNumbers, pageCount, zeroCount); } } diff --git a/src/main/java/stirling/software/SPDF/utils/GeneralUtils.java b/src/main/java/stirling/software/SPDF/utils/GeneralUtils.java index 4ef5964c..674de8b0 100644 --- a/src/main/java/stirling/software/SPDF/utils/GeneralUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/GeneralUtils.java @@ -116,6 +116,9 @@ public class GeneralUtils { } public static List parsePageString(String pageOrder, int totalPages) { + return parsePageString(pageOrder, totalPages , false ); + } + public static List parsePageString(String pageOrder, int totalPages, boolean isOneBased) { if (pageOrder == null || pageOrder.isEmpty()) { return Collections.singletonList(1); } @@ -123,17 +126,23 @@ public class GeneralUtils { // Convert the single number string to an integer and return it in a list return Collections.singletonList(Integer.parseInt(pageOrder)); } - return parsePageList(pageOrder.split(","), totalPages); + return parsePageList(pageOrder.split(","), totalPages, isOneBased); } - public static List parsePageList(String[] pageOrderArr, int totalPages) { + return parsePageList(pageOrderArr, totalPages, false); + } + + + public static List parsePageList(String[] pageOrderArr, int totalPages, boolean isOneBased) { List newPageOrder = new ArrayList<>(); + int adjustmentFactor = isOneBased ? 1 : 0; + // loop through the page order array for (String element : pageOrderArr) { if ("all".equalsIgnoreCase(element)) { for (int i = 0; i < totalPages; i++) { - newPageOrder.add(i); + newPageOrder.add(i+ adjustmentFactor); } // As all pages are already added, no need to check further break; @@ -164,7 +173,7 @@ public class GeneralUtils { pageNum += constantExists ? constant : 0; if (pageNum <= totalPages && pageNum > 0) { - newPageOrder.add(pageNum - 1); + newPageOrder.add(pageNum - adjustmentFactor); } } } else if (element.contains("-")) { @@ -179,11 +188,11 @@ public class GeneralUtils { // loop through the range of pages for (int j = start; j <= end; j++) { // print the current index - newPageOrder.add(j - 1); + newPageOrder.add(j - adjustmentFactor); } } else { // if the element is a single page - newPageOrder.add(Integer.parseInt(element) - 1); + newPageOrder.add(Integer.parseInt(element) - adjustmentFactor); } } diff --git a/src/main/resources/messages_en_GB.properties b/src/main/resources/messages_en_GB.properties index aeda5c47..92ca7da4 100644 --- a/src/main/resources/messages_en_GB.properties +++ b/src/main/resources/messages_en_GB.properties @@ -11,6 +11,7 @@ imgPrompt=Select Image(s) genericSubmit=Submit processTimeWarning=Warning: This process can take up to a minute depending on file-size pageOrderPrompt=Custom Page Order (Enter a comma-separated list of page numbers or Functions like 2n+1) : +pageSelectionPrompt=Custom Page Selection (Enter a comma-separated list of page numbers 1,5,6 or Functions like 2n+1) : goToPage=Go true=True false=False diff --git a/src/main/resources/templates/misc/stamp.html b/src/main/resources/templates/misc/stamp.html index a05e9717..126c8f7e 100644 --- a/src/main/resources/templates/misc/stamp.html +++ b/src/main/resources/templates/misc/stamp.html @@ -22,6 +22,12 @@

+ +
+ + +
+