用iText给pdf添加水印图片

  1.  /** 
  2.     * 给pdf文件添加水印 
  3.     * @param InPdfFile 要加水印的原pdf文件路径 
  4.     * @param outPdfFile 加了水印后要输出的路径 
  5.     * @param markImagePath 水印图片路径 
  6.     * @param pageSize 原pdf文件的总页数(该方法是我当初将数据导入excel中然后再转换成pdf所以我这里的值是用excel的行数计算出来的,如果不是我这种可以 直接用reader.getNumberOfPages()获取pdf的总页数) 
  7.     * @throws Exception 
  8.     */  
  9.    public static void addPdfMark(String InPdfFile, String outPdfFile, String markImagePath, int pageSize) throws Exception {  
  10.       
  11.     PdfReader reader = new PdfReader(InPdfFile, "PDF".getBytes());  
  12.        
  13.     PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outPdfFile));  
  14.      
  15.     Image img = Image.getInstance(markImagePath);// 插入水印     
  16.    
  17.     img.setAbsolutePosition(150100);  
  18.       
  19.     for(int i = 1; i <= pageSize; i++) {  
  20.        
  21.      PdfContentByte under = stamp.getUnderContent(i);  
  22.       
  23.      under.addImage(img);  
  24.          
  25.     }  
  26.       
  27.     stamp.close();// 关闭   
  28.       
  29.     File tempfile = new File(InPdfFile);  
  30.       
  31.     if(tempfile.exists()) {  
  32.        
  33.      tempfile.delete();  
  34.     }  
  35.       
  36.    }  

所需jar包iText-2.0.8.jar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值