java实现pdf加水印

直接上代码: 增加依赖:

 <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>7.0.4</version>
        </dependency>
     
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>layout</artifactId>
            <version>7.0.4</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>forms</artifactId>
            <version>7.0.4</version>
        </dependency>
      
     
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.4.3</version>
        </dependency>
      
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>font-asian</artifactId>
            <version>7.0.3</version>
        </dependency>

实现代码:

public static void downloadPDFFile(HttpServletResponse response, String fileName, String contentType, byte[] content,String text) throws IOException {
        // 清空response
        response.reset();
        // 通知浏览器进行文件下载
        fileName = URLEncoder.encode(fileName, "UTF-8");
//        response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), "ISO8859-1"));
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" +fileName);
        response.setContentType(contentType);
        response.setCharacterEncoding("UTF-8");

        OutputStream outputStream = response.getOutputStream();

        InputStream input = new ByteArrayInputStream(content);
        // FileUtils.downloadFile(response, name, fileModel.getContentType(), fileModel.getContent());
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(input), new PdfWriter(outputStream));


        Document doc = new Document(pdfDoc);
        PdfExtGState gs1 = new PdfExtGState().setFillOpacity(0.5f);
        PdfFont font = PdfFontFactory.createFont("STSong-Light","UniGB-UCS2-H");
        doc.setFont(font);


        Paragraph paragraph = new Paragraph(new Text(text))

                .setOpacity(0.1F) // 字体透明度 0-1 完全透明~不透明
                .setFontSize(20); // 字体大小

        for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) {

            PdfPage pdfPage = pdfDoc.getPage(i);
            // 获取页面大小,考虑页面旋转
            Rectangle pageSize = pdfPage.getPageSizeWithRotation();

            // 当页面有旋转时,内容自动旋转
            pdfPage.setIgnorePageRotationForContent(true);

            // 计算添加的位置坐标
            float x1 = (pageSize.getLeft() + pageSize.getRight()) / 2;
            float y1 = (pageSize.getTop() + pageSize.getBottom()) / 2;
            float y2 = (pageSize.getTop() + y1) / 2;
            float y3 = (y1 + pageSize.getBottom()) / 2;

           /* // 添加图片水印使用
            PdfCanvas over = new PdfCanvas(pdfDoc.getPage(i));
            over.saveState();
            over.setExtGState(gs1);*/
            // 添加文本水印
            // 参数分别为:文本、x 坐标、y 坐标、添加到第几页、文本水平对齐方式、文本垂直对齐方式、旋转弧度
            doc.showTextAligned(paragraph, x1, y1, i, TextAlignment.CENTER, VerticalAlignment.TOP, 6.5f);
            doc.showTextAligned(paragraph, x1, y2, i, TextAlignment.CENTER, VerticalAlignment.TOP, 6.5f);
            doc.showTextAligned(paragraph, x1, y3, i, TextAlignment.CENTER, VerticalAlignment.TOP, 6.5f);

           // over.restoreState();
        }

        doc.close();

        outputStream.write(content);
        safeClose(outputStream, null);
    }

结束, 这里需要注意的坑: 依赖用上面搭配好的; itextpdf 包因为版本不通, 字体依赖路径发生了变化, 如果不匹配会出现找不到字体的错误

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值