From f866c8a61f3716da371e332fe3a526d4431679a7 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Fri, 24 Mar 2023 13:30:26 +0000 Subject: [PATCH] Watermark opacity (#76) --- build.gradle | 2 +- .../security/WatermarkController.java | 13 ++++++- src/main/resources/messages_ar_AR.properties | 1 + src/main/resources/messages_de_DE.properties | 1 + src/main/resources/messages_en_GB.properties | 1 + src/main/resources/messages_en_US.properties | 1 + src/main/resources/messages_fr_FR.properties | 1 + .../resources/templates/compress-pdf.html | 2 +- .../templates/security/add-watermark.html | 39 +++++++++++++++++++ 9 files changed, 58 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 3fea6e96..507a5a44 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'stirling.software' -version = '0.4.1' +version = '0.4.2' sourceCompatibility = '17' repositories { diff --git a/src/main/java/stirling/software/SPDF/controller/security/WatermarkController.java b/src/main/java/stirling/software/SPDF/controller/security/WatermarkController.java index ec37ebf1..ea518c30 100644 --- a/src/main/java/stirling/software/SPDF/controller/security/WatermarkController.java +++ b/src/main/java/stirling/software/SPDF/controller/security/WatermarkController.java @@ -26,6 +26,7 @@ import org.springframework.web.multipart.MultipartFile; import stirling.software.SPDF.utils.PdfUtils; import stirling.software.SPDF.utils.WatermarkRemover; +import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState; @Controller public class WatermarkController { @@ -45,7 +46,8 @@ public class WatermarkController { @PostMapping("/add-watermark") public ResponseEntity addWatermark(@RequestParam("fileInput") MultipartFile pdfFile, @RequestParam("watermarkText") String watermarkText, @RequestParam(defaultValue = "30", name = "fontSize") float fontSize, @RequestParam(defaultValue = "0", name = "rotation") float rotation, - @RequestParam(defaultValue = "50", name = "widthSpacer") int widthSpacer, @RequestParam(defaultValue = "50", name = "heightSpacer") int heightSpacer) + @RequestParam(defaultValue = "0.5", name = "opacity") float opacity, + @RequestParam(defaultValue = "50", name = "widthSpacer") int widthSpacer, @RequestParam(defaultValue = "50", name = "heightSpacer") int heightSpacer) throws IOException { // Load the input PDF @@ -53,9 +55,18 @@ public class WatermarkController { // Create a page in the document for (PDPage page : document.getPages()) { + + + + // Get the page's content stream PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true); + // Set transparency + PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); + graphicsState.setNonStrokingAlphaConstant(opacity); + contentStream.setGraphicsStateParameters(graphicsState); + // Set font of watermark PDFont font = PDType1Font.HELVETICA_BOLD; contentStream.beginText(); diff --git a/src/main/resources/messages_ar_AR.properties b/src/main/resources/messages_ar_AR.properties index 9e66580c..4a163e44 100644 --- a/src/main/resources/messages_ar_AR.properties +++ b/src/main/resources/messages_ar_AR.properties @@ -225,6 +225,7 @@ watermark.selectText.3=حجم الخط: watermark.selectText.4=دوران (0-360): watermark.selectText.5=widthSpacer (مسافة بين كل علامة مائية أفقيًا): watermark.selectText.6=heightSpacer (مسافة بين كل علامة مائية عموديًا): +watermark.selectText.7=\u0627\u0644\u062A\u0639\u062A\u064A\u0645 (0\u066A - 100\u066A): watermark.submit=إضافة علامة مائية #remove-watermark diff --git a/src/main/resources/messages_de_DE.properties b/src/main/resources/messages_de_DE.properties index 28daca8c..abf5f3f1 100644 --- a/src/main/resources/messages_de_DE.properties +++ b/src/main/resources/messages_de_DE.properties @@ -224,6 +224,7 @@ watermark.selectText.3=Schriftgröße: watermark.selectText.4=Drehung (0-360): watermark.selectText.5=breiteSpacer (horizontaler Abstand zwischen den einzelnen Wasserzeichen): watermark.selectText.6=höheSpacer (vertikaler Abstand zwischen den einzelnen Wasserzeichen): +watermark.selectText.7=Deckkraft (0% - 100 %): watermark.submit=Wasserzeichen hinzufügen #remove-watermark diff --git a/src/main/resources/messages_en_GB.properties b/src/main/resources/messages_en_GB.properties index 1214ab33..3fd3477d 100644 --- a/src/main/resources/messages_en_GB.properties +++ b/src/main/resources/messages_en_GB.properties @@ -222,6 +222,7 @@ watermark.selectText.3=Font Size: watermark.selectText.4=Rotation (0-360): watermark.selectText.5=widthSpacer (Space between each watermark horizontally): watermark.selectText.6=heightSpacer (Space between each watermark vertically): +watermark.selectText.7=Opacity (0% - 100%): watermark.submit=Add Watermark #remove-watermark diff --git a/src/main/resources/messages_en_US.properties b/src/main/resources/messages_en_US.properties index 77a76514..740b83b7 100644 --- a/src/main/resources/messages_en_US.properties +++ b/src/main/resources/messages_en_US.properties @@ -222,6 +222,7 @@ watermark.selectText.3=Font Size: watermark.selectText.4=Rotation (0-360): watermark.selectText.5=widthSpacer (Space between each watermark horizontally): watermark.selectText.6=heightSpacer (Space between each watermark vertically): +watermark.selectText.7=Opacity (0% - 100%): watermark.submit=Add Watermark #remove-watermark diff --git a/src/main/resources/messages_fr_FR.properties b/src/main/resources/messages_fr_FR.properties index b26d7229..6068bf10 100644 --- a/src/main/resources/messages_fr_FR.properties +++ b/src/main/resources/messages_fr_FR.properties @@ -230,6 +230,7 @@ watermark.selectText.3=Taille de la police watermark.selectText.4=Rotation (0-360): watermark.selectText.5=widthSpacer (Espace entre chaque filigrane horizontalement): watermark.selectText.6=heightSpacer (Espace entre chaque filigrane verticalement): +watermark.selectText.7=Opacit (0 % - 100 %): watermark.submit=Ajouter un filigrane #remove-watermark diff --git a/src/main/resources/templates/compress-pdf.html b/src/main/resources/templates/compress-pdf.html index 855bbeae..ad1b4eb9 100644 --- a/src/main/resources/templates/compress-pdf.html +++ b/src/main/resources/templates/compress-pdf.html @@ -33,7 +33,7 @@ - +

diff --git a/src/main/resources/templates/security/add-watermark.html b/src/main/resources/templates/security/add-watermark.html index cb9b5fed..2a418082 100644 --- a/src/main/resources/templates/security/add-watermark.html +++ b/src/main/resources/templates/security/add-watermark.html @@ -26,6 +26,45 @@ +
+ + + +
+ + +