Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: Support line breaks with stamps #2345

Open
1 task done
DogeAmazed opened this issue Nov 27, 2024 · 1 comment
Open
1 task done

[Feature Request]: Support line breaks with stamps #2345

DogeAmazed opened this issue Nov 27, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@DogeAmazed
Copy link

Feature Description

The stamp feature should support line breaks (e.g. \n) instead of printing them as "\n"

Why is this feature valuable?

With line breaks implemented, overflow of the text can be manually avoided

Suggested Implementation

The behaviour should be the same as watermarks, which already implements line breaks.

Additional Information

String[] textLines = watermarkText.split("\\\\n");
float maxLineWidth = 0;
for (int i = 0; i < textLines.length; ++i) {
maxLineWidth = Math.max(maxLineWidth, font.getStringWidth(textLines[i]));
}
// Set size and location of text watermark
float watermarkWidth = widthSpacer + maxLineWidth * fontSize / 1000;
float watermarkHeight = heightSpacer + fontSize * textLines.length;
float pageWidth = page.getMediaBox().getWidth();
float pageHeight = page.getMediaBox().getHeight();
// 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++) {
contentStream.beginText();
contentStream.setTextMatrix(
Matrix.getRotateInstance(
(float) Math.toRadians(rotation),
j * newWatermarkWidth,
i * newWatermarkHeight));
for (int k = 0; k < textLines.length; ++k) {
contentStream.showText(textLines[k]);
contentStream.newLineAtOffset(0, -fontSize);
}
contentStream.endText();
}
}

No Duplicate of the Feature

  • I have verified that there are no existing features requests similar to my request.
@DogeAmazed DogeAmazed added the enhancement New feature or request label Nov 27, 2024
@saikumarjetti
Copy link
Contributor

Hi @DogeAmazed

This feature has been successfully implemented, and the corresponding PR has been merged.

Feel free to test it out and let us know if you encounter any issues or have further suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants