PDF文件加水印

public static void main(String[] args) {
	 watermark(new File("D:/测试.pdf"),"测试专用文件");
}
//依赖
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.12</version>
</dependency>

 public static String watermark(File file,String ts) {
 
        //创建新pdf文件
        File tmpPDF = new File("D:/"+file.getName());

        //打开pdf文件
        PDDocument doc = null;
        try {
            doc = PDDocument.load(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        doc.setAllSecurityToBeRemoved(true);

        //遍历pdf所有页
        for (PDPage page : doc.getPages()) {
            PDPageContentStream cs = null;
            try {
                cs = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true);
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            //引入系统字体
            PDFont font = null;
            try {
                font = PDType0Font.load(doc, new FileInputStream("C:/WINDOWS/Fonts/SIMYOU.TTF"), true);
            } catch (IOException e) {
                e.printStackTrace();
            }

            float fontSize = 28;
            PDResources resources = page.getResources();
            PDExtendedGraphicsState r0 = new PDExtendedGraphicsState();

            // 水印透明度
            r0.setNonStrokingAlphaConstant(0.05f);
            r0.setAlphaSourceFlag(true);

            //水印颜色
            try {
                cs.setGraphicsStateParameters(r0);
                cs.setNonStrokingColor(0, 0, 0);
                cs.beginText();
                cs.setFont(font, fontSize);
            } catch (IOException e) {
                e.printStackTrace();
            }


            //根据水印文字大小长度计算横向坐标需要渲染几次水印
            float h = ts.length() * fontSize;
            for (int i = 0; i <= 10; i++) {

                // 获取旋转实例
                try {
                    cs.setTextMatrix(Matrix.getRotateInstance(-150, i * 100, 0));
                    cs.showText(ts);
                } catch (IOException e) {
                    e.printStackTrace();
                }


                for (int j = 0; j < 20; j++) {
                    try {
                        cs.setTextMatrix(Matrix.getRotateInstance(-150, i * 100, j * h));
                        cs.showText(ts);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
            try {
                cs.endText();
                cs.restoreGraphicsState();
                cs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            doc.save(tmpPDF);
            doc.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
      return names;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值