mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-01-05 00:06:24 +01:00
fixed wrong filename generation through stamp tool #757
This commit is contained in:
parent
729c8006d2
commit
9da88b7652
@ -48,13 +48,13 @@ public class StampController {
|
|||||||
@Operation(
|
@Operation(
|
||||||
summary = "Add stamp to a PDF file",
|
summary = "Add stamp to a PDF file",
|
||||||
description =
|
description =
|
||||||
"This endpoint adds a stamp to a given PDF file. Users can specify the watermark type (text or image), rotation, opacity, width spacer, and height spacer. Input:PDF Output:PDF Type:SISO")
|
"This endpoint adds a stamp to a given PDF file. Users can specify the stamp type (text or image), rotation, opacity, width spacer, and height spacer. Input:PDF Output:PDF Type:SISO")
|
||||||
public ResponseEntity<byte[]> addStamp(@ModelAttribute AddStampRequest request)
|
public ResponseEntity<byte[]> addStamp(@ModelAttribute AddStampRequest request)
|
||||||
throws IOException, Exception {
|
throws IOException, Exception {
|
||||||
MultipartFile pdfFile = request.getFileInput();
|
MultipartFile pdfFile = request.getFileInput();
|
||||||
String watermarkType = request.getStampType();
|
String stampType = request.getStampType();
|
||||||
String watermarkText = request.getStampText();
|
String stampText = request.getStampText();
|
||||||
MultipartFile watermarkImage = request.getStampImage();
|
MultipartFile stampImage = request.getStampImage();
|
||||||
String alphabet = request.getAlphabet();
|
String alphabet = request.getAlphabet();
|
||||||
float fontSize = request.getFontSize();
|
float fontSize = request.getFontSize();
|
||||||
float rotation = request.getRotation();
|
float rotation = request.getRotation();
|
||||||
@ -99,10 +99,10 @@ public class StampController {
|
|||||||
graphicsState.setNonStrokingAlphaConstant(opacity);
|
graphicsState.setNonStrokingAlphaConstant(opacity);
|
||||||
contentStream.setGraphicsStateParameters(graphicsState);
|
contentStream.setGraphicsStateParameters(graphicsState);
|
||||||
|
|
||||||
if ("text".equalsIgnoreCase(watermarkType)) {
|
if ("text".equalsIgnoreCase(stampType)) {
|
||||||
addTextStamp(
|
addTextStamp(
|
||||||
contentStream,
|
contentStream,
|
||||||
watermarkText,
|
stampText,
|
||||||
document,
|
document,
|
||||||
page,
|
page,
|
||||||
rotation,
|
rotation,
|
||||||
@ -113,10 +113,10 @@ public class StampController {
|
|||||||
overrideY,
|
overrideY,
|
||||||
margin,
|
margin,
|
||||||
customColor);
|
customColor);
|
||||||
} else if ("image".equalsIgnoreCase(watermarkType)) {
|
} else if ("image".equalsIgnoreCase(stampType)) {
|
||||||
addImageStamp(
|
addImageStamp(
|
||||||
contentStream,
|
contentStream,
|
||||||
watermarkImage,
|
stampImage,
|
||||||
document,
|
document,
|
||||||
page,
|
page,
|
||||||
rotation,
|
rotation,
|
||||||
@ -134,12 +134,12 @@ public class StampController {
|
|||||||
document,
|
document,
|
||||||
Filenames.toSimpleFileName(pdfFile.getOriginalFilename())
|
Filenames.toSimpleFileName(pdfFile.getOriginalFilename())
|
||||||
.replaceFirst("[.][^.]+$", "")
|
.replaceFirst("[.][^.]+$", "")
|
||||||
+ "_watermarked.pdf");
|
+ "_stamped.pdf");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addTextStamp(
|
private void addTextStamp(
|
||||||
PDPageContentStream contentStream,
|
PDPageContentStream contentStream,
|
||||||
String watermarkText,
|
String stampText,
|
||||||
PDDocument document,
|
PDDocument document,
|
||||||
PDPage page,
|
PDPage page,
|
||||||
float rotation,
|
float rotation,
|
||||||
@ -208,9 +208,7 @@ public class StampController {
|
|||||||
x = overrideX;
|
x = overrideX;
|
||||||
y = overrideY;
|
y = overrideY;
|
||||||
} else {
|
} else {
|
||||||
x =
|
x = calculatePositionX(pageSize, position, fontSize, font, fontSize, stampText, margin);
|
||||||
calculatePositionX(
|
|
||||||
pageSize, position, fontSize, font, fontSize, watermarkText, margin);
|
|
||||||
y =
|
y =
|
||||||
calculatePositionY(
|
calculatePositionY(
|
||||||
pageSize, position, calculateTextCapHeight(font, fontSize), margin);
|
pageSize, position, calculateTextCapHeight(font, fontSize), margin);
|
||||||
@ -218,13 +216,13 @@ public class StampController {
|
|||||||
|
|
||||||
contentStream.beginText();
|
contentStream.beginText();
|
||||||
contentStream.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(rotation), x, y));
|
contentStream.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(rotation), x, y));
|
||||||
contentStream.showText(watermarkText);
|
contentStream.showText(stampText);
|
||||||
contentStream.endText();
|
contentStream.endText();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addImageStamp(
|
private void addImageStamp(
|
||||||
PDPageContentStream contentStream,
|
PDPageContentStream contentStream,
|
||||||
MultipartFile watermarkImage,
|
MultipartFile stampImage,
|
||||||
PDDocument document,
|
PDDocument document,
|
||||||
PDPage page,
|
PDPage page,
|
||||||
float rotation,
|
float rotation,
|
||||||
@ -235,8 +233,8 @@ public class StampController {
|
|||||||
float margin)
|
float margin)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
// Load the watermark image
|
// Load the stamp image
|
||||||
BufferedImage image = ImageIO.read(watermarkImage.getInputStream());
|
BufferedImage image = ImageIO.read(stampImage.getInputStream());
|
||||||
|
|
||||||
// Compute width based on original aspect ratio
|
// Compute width based on original aspect ratio
|
||||||
float aspectRatio = (float) image.getWidth() / (float) image.getHeight();
|
float aspectRatio = (float) image.getWidth() / (float) image.getHeight();
|
||||||
|
Loading…
Reference in New Issue
Block a user