pdf通过itext包增加水印

public static void main(String[] args) throws Exception { 
        PdfReader reader = new PdfReader(new FileInputStream(new File("E:/e1c84094314443dbba6c3f77ea907030.pdf")));//存在的文件
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File("E:/pdfWithWaterMark.pdf")));//输出文件
        int total = reader.getNumberOfPages() + 1;//pdf总页数
        PdfContentByte content;
        BaseFont base = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        PdfGState gs = new PdfGState();
     int a=0,b=0,c=35; //a距左,b距下,c倾斜度
for (int i = 1; i < total; i++) { content = stamper.getOverContent(i);// 在内容上方加水印 // content = stamper.getUnderContent(i);//在内容下方加水印 gs.setFillOpacity(0.4f); content.setGState(gs);// 透明度 content.beginText(); content.setColorFill(Color.LIGHT_GRAY); content.setFontAndSize(base, 30); content.setTextMatrix(70, 200); content.showTextAligned(Element.ALIGN_CENTER, "测试水印", a, b, c); content.setColorFill(Color.BLACK); content.setFontAndSize(base, 8); content.endText(); } stamper.close(); }

不过这个方法一页只能放一个水印  不过可以通过循环

content.showTextAligned(Element.ALIGN_CENTER, "测试水印", a, b, c);

该行一页添加多个水印     还在寻找其他更好的方法

itext包:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.4.5</version>
</dependency>

 

转载于:https://www.cnblogs.com/zcy1996/p/8487782.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值