Itext PDF生成 加水印图片文字

转载:http://blog.csdn.net/qm4050/article/details/9039069

由于项目中用到,所以网上找了很多,但或多或少有些问题,我整理更新了一下。

Itext架包下载地址:

http://download.csdn.net/detail/qm4050/5531349



PDF生成代码如下:

[java]  view plain  copy
  1. import java.io.*;  
  2. import com.itextpdf.text.*;  
  3. import com.itextpdf.text.pdf.*;  
  4.   
  5. /** 
  6.  * PDF生成 
  7.  */  
  8. public class PDFBuild {  
  9.   
  10.     public static void buidPDF(String pdfFile, String imageFile,  
  11.             String waterMarkName, int permission) {  
  12.         try {  
  13.             File file = File.createTempFile("tempFile"".pdf"); // 创建临时文件  
  14.   
  15.             // 生成PDF  
  16.             if (createPDFFile(file)) {  
  17.                 waterMark(file.getPath(), imageFile, pdfFile, waterMarkName,  
  18.                         permission); // 添加水印  
  19.             }  
  20.         } catch (Exception e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.     }  
  24.   
  25.     /** 
  26.      * 创建PDF文件 
  27.      *  
  28.      * @param file 
  29.      *            临时文件 
  30.      * @return 成功/失败 
  31.      */  
  32.     public static boolean createPDFFile(File file) {  
  33.         Rectangle rect = new Rectangle(PageSize.A4);  
  34.         Document doc = new Document(rect, 50.0F, 50.0F, 50.0F, 50.0F);  
  35.         try {  
  36.             PdfWriter.getInstance(doc, new FileOutputStream(file));  
  37.             doc.open();  
  38.   
  39.             BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",  
  40.                     "Identity-H"true);// 使用系统字体  
  41.   
  42.             Font font = new Font(bf, 14.0F, 0);  
  43.             font.setStyle(37); // 设置样式  
  44.             font.setFamily("宋体"); // 设置字体  
  45.   
  46.             Paragraph p = new Paragraph("付 款 通 知 书\r\n", font);  
  47.             p.setAlignment(1);  
  48.             doc.add(p);  
  49.             doc.close();  
  50.             return true;  
  51.         } catch (Exception e) {  
  52.             e.printStackTrace();  
  53.         }  
  54.         return false;  
  55.     }  
  56.   
  57.     public static void waterMark(String inputFile, String imageFile,  
  58.             String outputFile, String waterMarkName, int permission) {  
  59.         try {  
  60.             PdfReader reader = new PdfReader(inputFile);  
  61.             PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(  
  62.                     outputFile));  
  63.   
  64.             BaseFont base = BaseFont.createFont(  
  65.                     "C:/WINDOWS/Fonts/SIMSUN.TTC,1""Identity-H"true);// 使用系统字体  
  66.   
  67.             int total = reader.getNumberOfPages() + 1;  
  68.             Image image = Image.getInstance(imageFile);  
  69.   
  70.             // 图片位置  
  71.             image.setAbsolutePosition(400480);  
  72.             PdfContentByte under;  
  73.             int j = waterMarkName.length();  
  74.             char c = 0;  
  75.             int rise = 0;  
  76.             for (int i = 1; i < total; i++) {  
  77.                 rise = 400;  
  78.                 under = stamper.getUnderContent(i);  
  79.                 under.beginText();  
  80.                 under.setFontAndSize(base, 30);  
  81.   
  82.                 if (j >= 15) {  
  83.                     under.setTextMatrix(200120);  
  84.                     for (int k = 0; k < j; k++) {  
  85.                         under.setTextRise(rise);  
  86.                         c = waterMarkName.charAt(k);  
  87.                         under.showText(c + "");  
  88.                     }  
  89.                 } else {  
  90.                     under.setTextMatrix(240100);  
  91.                     for (int k = 0; k < j; k++) {  
  92.                         under.setTextRise(rise);  
  93.                         c = waterMarkName.charAt(k);  
  94.                         under.showText(c + "");  
  95.                         rise -= 18;  
  96.   
  97.                     }  
  98.                 }  
  99.   
  100.                 // 添加水印文字  
  101.                 under.endText();  
  102.   
  103.                 // 添加水印图片  
  104.                 under.addImage(image);  
  105.   
  106.                 // 画个圈  
  107.                 under.ellipse(250450350550);  
  108.                 under.setLineWidth(1f);  
  109.                 under.stroke();  
  110.             }  
  111.             stamper.close();  
  112.         } catch (Exception e) {  
  113.             e.printStackTrace();  
  114.         }  
  115.     }  
  116.   
  117.     public static void main(String[] args) {  
  118.         String imageFilePath = "D:/itext2.png"// 水印图片路径  
  119.         String pdfFilePath = "D:/itext.pdf"// 文件生成路径  
  120.         buidPDF(pdfFilePath, imageFilePath, "正版授权"16);  
  121.     }  
  122. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值