Watermark opacity (#76)

This commit is contained in:
Anthony Stirling 2023-03-24 13:30:26 +00:00 committed by GitHub
parent 472082cb03
commit f866c8a61f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 58 additions and 3 deletions

View file

@ -5,7 +5,7 @@ plugins {
} }
group = 'stirling.software' group = 'stirling.software'
version = '0.4.1' version = '0.4.2'
sourceCompatibility = '17' sourceCompatibility = '17'
repositories { repositories {

View file

@ -26,6 +26,7 @@ import org.springframework.web.multipart.MultipartFile;
import stirling.software.SPDF.utils.PdfUtils; import stirling.software.SPDF.utils.PdfUtils;
import stirling.software.SPDF.utils.WatermarkRemover; import stirling.software.SPDF.utils.WatermarkRemover;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
@Controller @Controller
public class WatermarkController { public class WatermarkController {
@ -45,7 +46,8 @@ public class WatermarkController {
@PostMapping("/add-watermark") @PostMapping("/add-watermark")
public ResponseEntity<byte[]> addWatermark(@RequestParam("fileInput") MultipartFile pdfFile, @RequestParam("watermarkText") String watermarkText, public ResponseEntity<byte[]> 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 = "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 { throws IOException {
// Load the input PDF // Load the input PDF
@ -53,9 +55,18 @@ public class WatermarkController {
// Create a page in the document // Create a page in the document
for (PDPage page : document.getPages()) { for (PDPage page : document.getPages()) {
// Get the page's content stream // Get the page's content stream
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true); 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 // Set font of watermark
PDFont font = PDType1Font.HELVETICA_BOLD; PDFont font = PDType1Font.HELVETICA_BOLD;
contentStream.beginText(); contentStream.beginText();

View file

@ -225,6 +225,7 @@ watermark.selectText.3=حجم الخط:
watermark.selectText.4=دوران (0-360): watermark.selectText.4=دوران (0-360):
watermark.selectText.5=widthSpacer (مسافة بين كل علامة مائية أفقيًا): watermark.selectText.5=widthSpacer (مسافة بين كل علامة مائية أفقيًا):
watermark.selectText.6=heightSpacer (مسافة بين كل علامة مائية عموديًا): watermark.selectText.6=heightSpacer (مسافة بين كل علامة مائية عموديًا):
watermark.selectText.7=\u0627\u0644\u062A\u0639\u062A\u064A\u0645 (0\u066A - 100\u066A):
watermark.submit=إضافة علامة مائية watermark.submit=إضافة علامة مائية
#remove-watermark #remove-watermark

View file

@ -224,6 +224,7 @@ watermark.selectText.3=Schriftgröße:
watermark.selectText.4=Drehung (0-360): watermark.selectText.4=Drehung (0-360):
watermark.selectText.5=breiteSpacer (horizontaler Abstand zwischen den einzelnen Wasserzeichen): watermark.selectText.5=breiteSpacer (horizontaler Abstand zwischen den einzelnen Wasserzeichen):
watermark.selectText.6=höheSpacer (vertikaler 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 watermark.submit=Wasserzeichen hinzufügen
#remove-watermark #remove-watermark

View file

@ -222,6 +222,7 @@ watermark.selectText.3=Font Size:
watermark.selectText.4=Rotation (0-360): watermark.selectText.4=Rotation (0-360):
watermark.selectText.5=widthSpacer (Space between each watermark horizontally): watermark.selectText.5=widthSpacer (Space between each watermark horizontally):
watermark.selectText.6=heightSpacer (Space between each watermark vertically): watermark.selectText.6=heightSpacer (Space between each watermark vertically):
watermark.selectText.7=Opacity (0% - 100%):
watermark.submit=Add Watermark watermark.submit=Add Watermark
#remove-watermark #remove-watermark

View file

@ -222,6 +222,7 @@ watermark.selectText.3=Font Size:
watermark.selectText.4=Rotation (0-360): watermark.selectText.4=Rotation (0-360):
watermark.selectText.5=widthSpacer (Space between each watermark horizontally): watermark.selectText.5=widthSpacer (Space between each watermark horizontally):
watermark.selectText.6=heightSpacer (Space between each watermark vertically): watermark.selectText.6=heightSpacer (Space between each watermark vertically):
watermark.selectText.7=Opacity (0% - 100%):
watermark.submit=Add Watermark watermark.submit=Add Watermark
#remove-watermark #remove-watermark

View file

@ -230,6 +230,7 @@ watermark.selectText.3=Taille de la police
watermark.selectText.4=Rotation (0-360)Â: watermark.selectText.4=Rotation (0-360)Â:
watermark.selectText.5=widthSpacer (Espace entre chaque filigrane horizontalement)Â: watermark.selectText.5=widthSpacer (Espace entre chaque filigrane horizontalement)Â:
watermark.selectText.6=heightSpacer (Espace entre chaque filigrane verticalement)Â: watermark.selectText.6=heightSpacer (Espace entre chaque filigrane verticalement)Â:
watermark.selectText.7=Opacité (0 % - 100 %):
watermark.submit=Ajouter un filigrane watermark.submit=Ajouter un filigrane
#remove-watermark #remove-watermark

View file

@ -33,7 +33,7 @@
<input type="checkbox" name="jbig2Lossy" id="jbig2Lossy"> <input type="checkbox" name="jbig2Lossy" id="jbig2Lossy">
<label for="jbig2Lossy" th:text="#{compress.selectText.7}"></label> <label for="jbig2Lossy" th:text="#{compress.selectText.7}"></label>
</div> </div>
<button type="submit" th:text="#{compress.submit}"></button> <button type="submit" class="btn btn-primary" th:text="#{compress.submit}"></button>
</form> </form>
<p class="mt-3" th:text="#{compress.credit}"></p> <p class="mt-3" th:text="#{compress.credit}"></p>

View file

@ -26,6 +26,45 @@
<label for="fontSize" th:text="#{watermark.selectText.3}"></label> <label for="fontSize" th:text="#{watermark.selectText.3}"></label>
<input type="text" id="fontSize" name="fontSize" class="form-control" value="30" /> <input type="text" id="fontSize" name="fontSize" class="form-control" value="30" />
</div> </div>
<div class="form-group">
<label for="opacity" th:text="#{watermark.selectText.7}"></label>
<input type="text" id="opacity" name="opacityText" class="form-control" value="50" onblur="updateopacityValue()" />
<input type="hidden" id="opacityReal" name="opacity" value="0.5">
</div>
<script>
const opacityInput = document.getElementById('opacity');
const opacityRealInput = document.getElementById('opacityReal');
const updateopacityValue = () => {
let percentageValue = parseFloat(opacityInput.value.replace('%', ''));
if (isNaN(percentageValue)) {
percentageValue = 0;
}
percentageValue = Math.min(Math.max(percentageValue, 0), 100);
opacityInput.value = `${percentageValue}`;
opacityRealInput.value = (percentageValue / 100).toFixed(2);
};
const appendPercentageSymbol = () => {
if (!opacityInput.value.endsWith('%')) {
opacityInput.value += '%';
}
};
opacityInput.addEventListener('focus', () => {
opacityInput.value = opacityInput.value.replace('%', '');
});
opacityInput.addEventListener('blur', () => {
updateopacityValue();
appendPercentageSymbol();
});
// Set initial values
updateopacityValue();
appendPercentageSymbol();
</script>
<div class="form-group"> <div class="form-group">
<label for="rotation" th:text="#{watermark.selectText.4}"></label> <label for="rotation" th:text="#{watermark.selectText.4}"></label>
<input type="text" id="rotation" name="rotation" class="form-control" value="45" /> <input type="text" id="rotation" name="rotation" class="form-control" value="45" />