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

项目需求涉及到操作pdf模板,根据生成好的模板向里面填充数据  

用到的jar包是iText-5.0.6.jar 和iTextAsian.jar

pdf模板效果如下:

  1. import java.io.ByteArrayOutputStream;  

  2. import java.io.FileOutputStream;  

  3. import java.io.IOException;  

  4. import java.io.OutputStream;  

  5. import java.util.ArrayList;  

  6. import java.util.HashMap;  

  7. import java.util.Map;  

  8.   

  9. import com.lowagie.text.DocumentException;  

  10. import com.lowagie.text.pdf.AcroFields;  

  11. import com.lowagie.text.pdf.BaseFont;  

  12. import com.lowagie.text.pdf.PdfContentByte;  

  13. import com.lowagie.text.pdf.PdfReader;  

  14. import com.lowagie.text.pdf.PdfStamper;  

  15.   

  16. public class Test {  

  17.     public static void main(String[] args) throws Exception {  

  18.         test();  

  19.         System.out.println("success");  

  20.     }  

  21.       

  22.     public static void test() throws IOException, DocumentException {  

  23.         String fileName = "F:/zxing/zs/zsTemp.pdf"// pdf模板  

  24.         PdfReader reader = new PdfReader(fileName);  

  25.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  

  26.         /* 将要生成的目标PDF文件名称 */   

  27.         PdfStamper ps = new PdfStamper(reader, bos);  

  28.         PdfContentByte under = ps.getUnderContent(1);     

  29.           

  30.           /* 使用中文字体 */    

  31.         BaseFont bf = BaseFont.createFont("STSong-Light""UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);  

  32.         ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();  

  33.         fontList.add(bf);  

  34.           

  35.         /* 取出报表模板中的所有字段 */    

  36.         AcroFields fields = ps.getAcroFields();  

  37.         fields.setSubstitutionFonts(fontList);  

  38.         fillData(fields, data());  

  39.           

  40.         /* 必须要调用这个,否则文档不会生成的 */    

  41.         ps.setFormFlattening(true);  

  42.         ps.close();  

  43.           

  44.           

  45.         OutputStream fos = new FileOutputStream("F:/zxing/zs/zsResult.pdf");  

  46.         fos.write(bos.toByteArray());  

  47.         fos.flush();  

  48.         fos.close();  

  49.         bos.close();  

  50.     }  

  51.   

  52.     public static void fillData(AcroFields fields, Map<String, String> data)  

  53.             throws IOException, DocumentException {  

  54.         for (String key : data.keySet()) {  

  55.             String value = data.get(key);  

  56.             fields.setField(key, value); // 为字段赋值,注意字段名称是区分大小写的  

  57.         }  

  58.     }  

  59.   

  60.     public static Map<String, String> data() {  

  61.         Map<String, String> data = new HashMap<String, String>();  

  62.         data.put("name""test:");  

  63.         data.put("bianhao""xx第10000001号");  

  64.         data.put("amount""1000");  

  65.         data.put("date","2015年7月7日");  

  66.         return data;  

  67.     }  

  68. }  



java 操作pdf模板(向指定域添加文本内容和图片)

  1.  public static void main(String[] args)  

  2. {  

  3. try  

  4. {  

  5. String TemplatePDF = "e:/F-3.pdf"//魔板路径  

  6. String outFile = "e:/test.pdf"//生成新的pdf的路径  

  7. PdfReader reader = new PdfReader(TemplatePDF);  

  8. PdfStamper ps = new PdfStamper(reader, new FileOutputStream(outFile)); // 生成的输出流  

  9.   

  10. AcroFields s = ps.getAcroFields();  

  11. // 插入文字  

  12. insertText(ps, s);  

  13. // 插入图片  

  14. insertImage(ps, s);  

  15. ps.close();  

  16. reader.close();  

  17. }  

  18. catch (Exception e)  

  19. {  

  20. e.printStackTrace();  

  21. }   

  22. }  

  23. /** 

  24. * 插入图片 

  25.  

  26. * @param ps 

  27. * @param s 

  28. * @author WangMeng 

  29. * @date 2016年6月16日 

  30. */  

  31. public static void insertImage(PdfStamper ps, AcroFields s)  

  32. {  

  33.   

  34.   

  35. try  

  36. {  

  37. List<AcroFields.FieldPosition> list = s.getFieldPositions("QR_CODE");  

  38. Rectangle signRect = list.get(0).position;  

  39.   

  40.   

  41. Image p_w_picpath = Image.getInstance("e:/pdf.jpg");  

  42. PdfContentByte under = ps.getOverContent(2);  

  43. float x = signRect.getLeft();  

  44. float y = signRect.getBottom();  

  45. System.out.println(x);  

  46. System.out.println(y);  

  47. p_w_picpath.setAbsolutePosition(x, y);  

  48. p_w_picpath.scaleToFit(signRect.getWidth(), signRect.getHeight());  

  49.   

  50.   

  51. under.addImage(p_w_picpath);  

  52. }  

  53. catch (Exception e)  

  54. {  

  55. // TODO Auto-generated catch block  

  56. e.printStackTrace();  

  57. }  

  58.   

  59.   

  60. }  

  61. /** 

  62. * 创建Chunk 

  63.  

  64. * @return 

  65. * @author WangMeng 

  66. * @date 2016年6月16日 

  67. */  

  68. public static Chunk CreateChunk()  

  69. {  

  70. BaseFont bfChinese;  

  71. Chunk chunk = null;  

  72. try  

  73. {  

  74. bfChinese = BaseFont.createFont("STSong-Light""UniGB-UCS2-H", BaseFont.EMBEDDED);  

  75. Font fontChinese = new Font(bfChinese, 10 * 4 / 3);  

  76. chunk = new Chunk("张三", fontChinese);  

  77. }  

  78. catch (Exception e)  

  79. {  

  80. e.printStackTrace();  

  81. }  

  82.   

  83.   

  84. return chunk;  

  85. }  

  86. /** 

  87. * 插入文本 

  88.  

  89. * @return 

  90. * @author WangMeng 

  91. * @date 2016年6月16日 

  92. */  

  93. public static void insertText(PdfStamper ps, AcroFields s)  

  94. {  

  95. List<AcroFields.FieldPosition> list = s.getFieldPositions("CONNECT_NAME");  

  96. Rectangle rect = list.get(0).position;  

  97.   

  98.   

  99. PdfContentByte cb = ps.getOverContent(1);  

  100. PdfPTable table = new PdfPTable(1);  

  101. float tatalWidth = rect.getRight() - rect.getLeft() - 1;  

  102. table.setTotalWidth(tatalWidth);  

  103.   

  104.   

  105. PdfPCell cell = new PdfPCell(new Phrase(CreateChunk()));  

  106. cell.setFixedHeight(rect.getTop() - rect.getBottom() - 1);  

  107. cell.setBorderWidth(0);  

  108. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);  

  109. cell.setHorizontalAlignment(Element.ALIGN_CENTER);  

  110. cell.setLeading(0, (float1.1);  

  111.   

  112.   

  113. table.addCell(cell);  

  114. table.writeSelectedRows(0, -1, rect.getLeft(), rect.getTop(), cb);  

  115. }  

save_snippets.png




jar包下载地址:http://down.51cto.com/data/2300129