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 9f779d881b0..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,24 +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 effective line length according to the angle used - float effectiveHeight = watermarkHeight / (float) Math.cos(Math.toRadians(rotation)); - int watermarkRowsRotation = (int) (pageHeight / effectiveHeight + 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 - /* In the 'for' loop, is do (watermarkRowsRotation - watermarkRows) so that the watermark starts at the bottom of the page. - This compensate for the rotation by adjusting the starting point for inserting the watermark. */ - for (int i = (watermarkRowsRotation - watermarkRows); 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]);