【java】 iText使用PDF模板生成输出PDF

转载自:http://blog.csdn.net/leeyefang/article/details/46805617

本文所要用到的工具或jar主要有:

Adobe Acrobat 这个主要用来制作PDF模板、eclipse、 itext.jar、 解决中文的输出问题,需要多下载一个名为iTextAsian.jar的JAR包。这个包里面定义了与中文输出相关的一些文件。 

pdf模板效果如下:


JAVA代码:

[java]  view plain  copy
  1. package com.yfli.iText;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.OutputStream;  
  7. import java.util.ArrayList;  
  8. import java.util.HashMap;  
  9. import java.util.Map;  
  10.   
  11. import com.lowagie.text.DocumentException;  
  12. import com.lowagie.text.pdf.AcroFields;  
  13. import com.lowagie.text.pdf.BaseFont;  
  14. import com.lowagie.text.pdf.PdfContentByte;  
  15. import com.lowagie.text.pdf.PdfReader;  
  16. import com.lowagie.text.pdf.PdfStamper;  
  17.   
  18. public class Test {  
  19.     public static void main(String[] args) throws Exception {  
  20.         test();  
  21.         System.out.println("success");  
  22.     }  
  23.       
  24.     public static void test() throws IOException, DocumentException {  
  25.         String fileName = "F:/zxing/zs/zsTemp.pdf"// pdf模板  
  26.         PdfReader reader = new PdfReader(fileName);  
  27.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  28.         /* 将要生成的目标PDF文件名称 */   
  29.         PdfStamper ps = new PdfStamper(reader, bos);  
  30.         PdfContentByte under = ps.getUnderContent(1);     
  31.           
  32.           /* 使用中文字体 */    
  33.         BaseFont bf = BaseFont.createFont("STSong-Light""UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);  
  34.         ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();  
  35.         fontList.add(bf);  
  36.           
  37.         /* 取出报表模板中的所有字段 */    
  38.         AcroFields fields = ps.getAcroFields();  
  39.         fields.setSubstitutionFonts(fontList);  
  40.         fillData(fields, data());  
  41.           
  42.         /* 必须要调用这个,否则文档不会生成的 */    
  43.         ps.setFormFlattening(true);  
  44.         ps.close();  
  45.           
  46.           
  47.         OutputStream fos = new FileOutputStream("F:/zxing/zs/zsResult.pdf");  
  48.         fos.write(bos.toByteArray());  
  49.         fos.flush();  
  50.         fos.close();  
  51.         bos.close();  
  52.     }  
  53.   
  54.     public static void fillData(AcroFields fields, Map<String, String> data)  
  55.             throws IOException, DocumentException {  
  56.         for (String key : data.keySet()) {  
  57.             String value = data.get(key);  
  58.             fields.setField(key, value); // 为字段赋值,注意字段名称是区分大小写的  
  59.         }  
  60.     }  
  61.   
  62.     public static Map<String, String> data() {  
  63.         Map<String, String> data = new HashMap<String, String>();  
  64.         data.put("name""test:");  
  65.         data.put("bianhao""xx第10000001号");  
  66.         data.put("amount""1000");  
  67.         data.put("date","2015年7月7日");  
  68.         return data;  
  69.     }  
  70. }  

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值