说明
日常开发中,通常会有动态填充Pdf表单的需求,程序可根据用户的输入或后台数据库的内容,动态、批量向Pdf模板中填充内容。
这里简单介绍一下Pdf模板的制作,以及通过iTextPdf组件给pdf Form表单进行内容填充。
Pdf模板制作
1、word中制作表格
使用Office word 或 WPS word,制作出一个表格,通过表格底色,颜色、字体设置、对齐等方式美化一下表格。
效果图如下:
2、word导出为pdf
可以使用WPS的另存为功能,将word导出为pdf文件
导出完的pdf文件,效果如下:
3、pdf模板制作
使用 “Adobe Acrobat DC” 软件,对已经生成的pdf文件进行表单项默认文本的替换。
操作方法:
A). 使用该软件打开上述pdf模板后,依次点击左上角菜单处“文档”–》右侧工具栏中“编辑PDF”;
B). 新的面板中可以看到左上角已出现“编辑PDF”菜单,在其下拉菜单中,选择最下方的“准备表单”项,这时可以看到所有的pdf表单项均被识别出来了;
C). 但是表单上面默认的文本项名称为类似“fill_1”这种文档,双击该文件,在弹窗中将其修改为业务字段名称,如下:
D). 修改适配完毕后,保存该表单pdf文件,为后续程序读写该文件做准备。
使用iTextPdf对pdf表单进行读写
iText(公司网址:https://itextpdf.com/en)是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。
项目要使用iText,必须引入jar包才能使用,maven依赖如下:
<!-- 核心jar-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<!-- 设置输出中文 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!-- 设置pdf文件密码 -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.54</version>
</dependency>
GenerateResumePDF.java代码清单:
package com.david.pdf;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.*;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
public class GenerateResumePDF {
public static void resumeGenerate(Map<String, String> paramsMap) {
String templatePath = "C:\\Users\\user\\Desktop\\pdf_template\\test-output-2.pdf";
String outputPDFPath = "C:\\Users\\user\\Desktop\\pdf_template\\" + paramsMap.get("name") + "-" + paramsMap.get("phone") + ".pdf";
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
out = new FileOutputStream(outputPDFPath);
reader = new PdfReader(templatePath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields(); //获取pdf上的表单集合,如name/phone
//给表单添加中文字体
BaseFont baseFont = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
form.addSubstitutionFont(baseFont);
//遍历map插入数据
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
form.setField(entry.getKey(), entry.getValue());
System.out.println(String.format("插入PDF数据---> key=%13s, value=%5s", entry.getKey(), entry.getValue()));
}
//如果设置为false,那么生成的文件还能编辑,一般都需要设置为false
stamper.setFormFlattening(true);
stamper.close();
//文档对象,包括header、footer、page信息、和页面元数据信息
Document doc = new Document();
//Make copies of PDF documents. Documents can be edited after reading and before writing them out.
PdfCopy copy = new PdfCopy(doc, out);
//文档打开,等待写入
doc.open();
//待写入的页面
PdfImportedPage importedPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
//添加页面
copy.addPage(importedPage);
doc.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("name", "david");
map.put("age", "30");
map.put("gender", "男");
map.put("phone", "13666666666");
map.put("email", "me@189.com");
map.put("marriage", "未婚");
map.put("jobyear", "2");
map.put("education", "博士");
map.put("location", "墨尔本");
System.out.println("map = " + map);
resumeGenerate(map);
}
}
经过代码填充表单的pdf文件,效果如下图所示:
参考列表,致谢: