标签:ima main tput ash auth free eal ext .text
Itextpdf + Adobe Acrobat DC填充模板生成pdf快速入门
生成pdf有很多种方法,如通过freemarker,或 使用itextpdf。本文将使用itextpdf生成pdf
1.下载Adobe Acrobat DC 并安装
从官网http://get.adobe.com/cn/reader/otherversions/下载
或者下载绿色版本
安装完之后又如下图标:
2.将word文件通过Adobe Acrobat DC生成pdf模板
如需要填充的是姓名和身份信息,如设置属性为realName 和idNo
3.运行结果如下
4.填充代码
1 /** 2 * Project Name:mk-project <br> 3 * Package Name:com.suns.pdf <br> 4 * 5 * @author mk <br> 6 * Date:2018-11-2 14:32 <br> 7 */ 8 9 package com.suns.pdf; 10 11 import com.itextpdf.text.pdf.AcroFields; 12 import com.itextpdf.text.pdf.BaseFont; 13 import com.itextpdf.text.pdf.PdfReader; 14 import com.itextpdf.text.pdf.PdfStamper; 15 import org.apache.commons.io.FileUtils; 16 import org.apache.commons.io.IOUtils; 17 18 import java.io.File; 19 import java.io.FileOutputStream; 20 import java.io.IOException; 21 import java.util.HashMap; 22 import java.util.Map; 23 24 /** 25 * Description: PdfUtils <br> 26 * 依赖的包:itextpdf itext-asian 27 * commons-io,commons-codec 28 * @author mk 29 * @Date 2018-11-2 14:32 <br> 30 * @Param 31 * @return 32 */ 33 public class PdfUtils { 34 35 36 public static void main(String[] args) throws IOException { 37 HashMap map = new HashMap<String, String>(); 38 map.put("realName","对应pdf中的表单名为realName"); 39 map.put("idNo","对应pdf中的表单名为idNo"); 40 String path = PdfUtils.class.getResource("/template").getPath(); 41 System.out.println("path:"+path); 42 String sourceFile = path + File.separator + "test.pdf"; 43 String targetFile = "d:/pdf/test_fill.pdf"; 44 genPdf(map,sourceFile,targetFile); 45 } 46 47 private static void genPdf(HashMap map, String sourceFile, String targetFile) throws IOException { 48 File templateFile = new File(sourceFile); 49 fillParam(map, FileUtils.readFileToByteArray(templateFile), targetFile); 50 } 51 52 /** 53 * Description: 使用map中的参数填充pdf,map中的key和pdf表单中的field对应 <br> 54 * @author mk 55 * @Date 2018-11-2 15:21 <br> 56 * @Param 57 * @return 58 */ 59 public static void fillParam(Map<String, String> fieldValueMap, byte[] file, String contractFileName) { 60 FileOutputStream fos = null; 61 try { 62 fos = new FileOutputStream(contractFileName); 63 PdfReader reader = null; 64 PdfStamper stamper = null; 65 BaseFont base = null; 66 try { 67 reader = new PdfReader(file); 68 stamper = new PdfStamper(reader, fos); 69 stamper.setFormFlattening(true); 70 base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 71 AcroFields acroFields = stamper.getAcroFields(); 72 for (String key : acroFields.getFields().keySet()) { 73 acroFields.setFieldProperty(key, "textfont", base, null); 74 acroFields.setFieldProperty(key, "textsize", new Float(9), null); 75 } 76 if (fieldValueMap != null) { 77 for (String fieldName : fieldValueMap.keySet()) { 78 acroFields.setField(fieldName, fieldValueMap.get(fieldName)); 79 } 80 } 81 } catch (Exception e) { 82 e.printStackTrace(); 83 } finally { 84 if (stamper != null) { 85 try { 86 stamper.close(); 87 } catch (Exception e) { 88 e.printStackTrace(); 89 } 90 } 91 if (reader != null) { 92 reader.close(); 93 } 94 } 95 96 } catch (Exception e) { 97 System.out.println("填充参数异常"); 98 e.printStackTrace(); 99 } finally { 100 IOUtils.closeQuietly(fos); 101 } 102 } 103 }
Itextpdf + Adobe Acrobat DC填充模板生成pdf快速入门
标签:ima main tput ash auth free eal ext .text
原文地址:https://www.cnblogs.com/lookupthesky/p/9897373.html