mirror of
				https://github.com/Frooodle/Stirling-PDF.git
				synced 2025-11-01 01:21:18 +01:00 
			
		
		
		
	Merge pull request #794 from mannam11/fixed-stamptool-wrong-filename-generaion-#757
fixed wrong filename generation through stamp tool #757
This commit is contained in:
		
						commit
						7a9417a62f
					
				@ -49,13 +49,13 @@ public class StampController {
 | 
			
		||||
    @Operation(
 | 
			
		||||
            summary = "Add stamp to a PDF file",
 | 
			
		||||
            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)
 | 
			
		||||
            throws IOException, Exception {
 | 
			
		||||
        MultipartFile pdfFile = request.getFileInput();
 | 
			
		||||
        String watermarkType = request.getStampType();
 | 
			
		||||
        String watermarkText = request.getStampText();
 | 
			
		||||
        MultipartFile watermarkImage = request.getStampImage();
 | 
			
		||||
        String stampType = request.getStampType();
 | 
			
		||||
        String stampText = request.getStampText();
 | 
			
		||||
        MultipartFile stampImage = request.getStampImage();
 | 
			
		||||
        String alphabet = request.getAlphabet();
 | 
			
		||||
        float fontSize = request.getFontSize();
 | 
			
		||||
        float rotation = request.getRotation();
 | 
			
		||||
@ -88,6 +88,8 @@ public class StampController {
 | 
			
		||||
        // Load the input PDF
 | 
			
		||||
        PDDocument document = Loader.loadPDF(pdfFile.getBytes());
 | 
			
		||||
 | 
			
		||||
      
 | 
			
		||||
 | 
			
		||||
        List<Integer> pageNumbers = request.getPageNumbersList();
 | 
			
		||||
 | 
			
		||||
        for (int pageIndex : pageNumbers) {
 | 
			
		||||
@ -105,10 +107,10 @@ public class StampController {
 | 
			
		||||
                graphicsState.setNonStrokingAlphaConstant(opacity);
 | 
			
		||||
                contentStream.setGraphicsStateParameters(graphicsState);
 | 
			
		||||
 | 
			
		||||
                if ("text".equalsIgnoreCase(watermarkType)) {
 | 
			
		||||
                if ("text".equalsIgnoreCase(stampType)) {
 | 
			
		||||
                    addTextStamp(
 | 
			
		||||
                            contentStream,
 | 
			
		||||
                            watermarkText,
 | 
			
		||||
                            stampText,
 | 
			
		||||
                            document,
 | 
			
		||||
                            page,
 | 
			
		||||
                            rotation,
 | 
			
		||||
@ -119,10 +121,10 @@ public class StampController {
 | 
			
		||||
                            overrideY,
 | 
			
		||||
                            margin,
 | 
			
		||||
                            customColor);
 | 
			
		||||
                } else if ("image".equalsIgnoreCase(watermarkType)) {
 | 
			
		||||
                } else if ("image".equalsIgnoreCase(stampType)) {
 | 
			
		||||
                    addImageStamp(
 | 
			
		||||
                            contentStream,
 | 
			
		||||
                            watermarkImage,
 | 
			
		||||
                            stampImage,
 | 
			
		||||
                            document,
 | 
			
		||||
                            page,
 | 
			
		||||
                            rotation,
 | 
			
		||||
@ -140,12 +142,12 @@ public class StampController {
 | 
			
		||||
                document,
 | 
			
		||||
                Filenames.toSimpleFileName(pdfFile.getOriginalFilename())
 | 
			
		||||
                                .replaceFirst("[.][^.]+$", "")
 | 
			
		||||
                        + "_watermarked.pdf");
 | 
			
		||||
                        + "_stamped.pdf");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void addTextStamp(
 | 
			
		||||
            PDPageContentStream contentStream,
 | 
			
		||||
            String watermarkText,
 | 
			
		||||
            String stampText,
 | 
			
		||||
            PDDocument document,
 | 
			
		||||
            PDPage page,
 | 
			
		||||
            float rotation,
 | 
			
		||||
@ -214,9 +216,7 @@ public class StampController {
 | 
			
		||||
            x = overrideX;
 | 
			
		||||
            y = overrideY;
 | 
			
		||||
        } else {
 | 
			
		||||
            x =
 | 
			
		||||
                    calculatePositionX(
 | 
			
		||||
                            pageSize, position, fontSize, font, fontSize, watermarkText, margin);
 | 
			
		||||
            x = calculatePositionX(pageSize, position, fontSize, font, fontSize, stampText, margin);
 | 
			
		||||
            y =
 | 
			
		||||
                    calculatePositionY(
 | 
			
		||||
                            pageSize, position, calculateTextCapHeight(font, fontSize), margin);
 | 
			
		||||
@ -224,13 +224,13 @@ public class StampController {
 | 
			
		||||
 | 
			
		||||
        contentStream.beginText();
 | 
			
		||||
        contentStream.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(rotation), x, y));
 | 
			
		||||
        contentStream.showText(watermarkText);
 | 
			
		||||
        contentStream.showText(stampText);
 | 
			
		||||
        contentStream.endText();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void addImageStamp(
 | 
			
		||||
            PDPageContentStream contentStream,
 | 
			
		||||
            MultipartFile watermarkImage,
 | 
			
		||||
            MultipartFile stampImage,
 | 
			
		||||
            PDDocument document,
 | 
			
		||||
            PDPage page,
 | 
			
		||||
            float rotation,
 | 
			
		||||
@ -241,8 +241,8 @@ public class StampController {
 | 
			
		||||
            float margin)
 | 
			
		||||
            throws IOException {
 | 
			
		||||
 | 
			
		||||
        // Load the watermark image
 | 
			
		||||
        BufferedImage image = ImageIO.read(watermarkImage.getInputStream());
 | 
			
		||||
        // Load the stamp image
 | 
			
		||||
        BufferedImage image = ImageIO.read(stampImage.getInputStream());
 | 
			
		||||
 | 
			
		||||
        // Compute width based on original aspect ratio
 | 
			
		||||
        float aspectRatio = (float) image.getWidth() / (float) image.getHeight();
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user