pdf添加图片及而文字水印

package com.pdf;


import java.awt.Color;
import java.io.FileOutputStream;


import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;


public class Test2 {
/** 
   * 给pdf文件添加水印 
   * @param InPdfFile 要加水印的原pdf文件路径 
   * @param outPdfFile 加了水印后要输出的路径 
   * @param markImagePath 水印图片路径 
   * @param pageSize 原pdf文件的总页数(该方法是我当初将数据导入excel中然后再转换成pdf所以我这里的值是用excel的行数计算出来的,如果不是我这种可以 直接用reader.getNumberOfPages()获取pdf的总页数) 
   * @throws Exception 
   */  
  public static void addPdfMark(String InPdfFile, String outPdfFile, String markImagePath) throws Exception {  
     
   PdfReader reader = new PdfReader(InPdfFile, "PDF".getBytes());  
      
   PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outPdfFile));  
    
   Image img = Image.getInstance(markImagePath);// 插入水印     
  
   img.setAbsolutePosition(0, 0);  
        BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.EMBEDDED);
   int pageSize = reader.getNumberOfPages();
   for(int i = 1; i <= pageSize; i++) {  
      
    PdfContentByte under = stamp.getUnderContent(i);  
    under.beginText();  
    under.setColorFill(Color.LIGHT_GRAY);  
    under.setFontAndSize(base, 50);  
    under.setTextMatrix(70, 200);  
    under.showTextAligned(Element.ALIGN_CENTER, "www.google.com!", 300,450, 55); 
    under.addImage(img);  
        
   }  
     
   stamp.close();// 关闭   
     
  /* File tempfile = new File(InPdfFile);  
     
   if(tempfile.exists()) {  
      
    tempfile.delete();  
   }  */
     
  }  
  public static void main(String[] args) {
  try {
addPdfMark("d:\\pdf\\111.pdf","d:\\pdf\\222.pdf","d:\\pdf\\logo1.jpg");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值