From 5cf53e39d0b3809a453bb7316a8ebc66eae746a0 Mon Sep 17 00:00:00 2001 From: Renan <82916964+thisisrenan@users.noreply.github.com> Date: Wed, 13 Nov 2024 08:12:30 -0300 Subject: [PATCH] Increase watermark coverage to fill page (#2049) (#2220) * Increase watermark coverage to fill page (#2049) * Increase watermark coverage to fill page with the new calculation (#2049) --- .../api/security/WatermarkController.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java b/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java index de7efd713c6..7bf9bbb75a5 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java @@ -187,18 +187,32 @@ private void addTextWatermark( float watermarkHeight = heightSpacer + fontSize * textLines.length; float pageWidth = page.getMediaBox().getWidth(); float pageHeight = page.getMediaBox().getHeight(); - int watermarkRows = (int) (pageHeight / watermarkHeight + 1); - int watermarkCols = (int) (pageWidth / watermarkWidth + 1); + + + //Calculating the new width and height depending on the angle. + float radians = (float) Math.toRadians(rotation); + float newWatermarkWidth = + (float) + (Math.abs(watermarkWidth * Math.cos(radians)) + + Math.abs(watermarkHeight * Math.sin(radians))); + float newWatermarkHeight = + (float) + (Math.abs(watermarkWidth * Math.sin(radians)) + + Math.abs(watermarkHeight * Math.cos(radians))); + + //Calculating the number of rows and columns. + int watermarkRows = (int) (pageHeight / newWatermarkHeight + 1); + int watermarkCols = (int) (pageWidth / newWatermarkWidth + 1); // Add the text watermark - for (int i = 0; i < watermarkRows; i++) { - for (int j = 0; j < watermarkCols; j++) { + for (int i = 0; i <= watermarkRows; i++) { + for (int j = 0; j <= watermarkCols; j++) { contentStream.beginText(); contentStream.setTextMatrix( Matrix.getRotateInstance( (float) Math.toRadians(rotation), - j * watermarkWidth, - i * watermarkHeight)); + j * newWatermarkWidth, + i * newWatermarkHeight)); for (int k = 0; k < textLines.length; ++k) { contentStream.showText(textLines[k]);