前提:需要pdf模板:并且模板内容以pdf 文本框的形式填写
package com.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
public class PdfTest {
@Test
public void testPdf() {
String inputFileName = "certificateModel.pdf";
String outputFileName = "833257_out.pdf";
OutputStream os = null;
PdfStamper ps = null;
PdfReader reader = null;
try {
os = new FileOutputStream(new File(outputFileName));
// 2 读入pdf表单
reader = new PdfReader(inputFileName);
// 3 根据表单生成一个新的pdf
ps = new PdfStamper(reader, os);
// 4 获取pdf表单
AcroFields form = ps.getAcroFields();
// 5给表单添加中文字体 这里采用系统字体。不设置的话,中文可能无法显示
// BaseFont bf = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",
// BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// 方法二:使用iTextAsian.jar中的字体
// BaseFont baseFont =
// BaseFont.createFont("STSong-Light",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
BaseFont bf = BaseFont.createFont("d:/user/80004194/desktop/test/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
form.addSubstitutionFont(bf);
// 6查询数据================================================
Map<String, Object> data = new HashMap<String, Object>();
data.put("empName", "金星");
data.put("empNumber", "束带结发看电视");
data.put("empCompany", "顺丰科技");
data.put("empDepartment", "飞行部747");
data.put("empTrainingCenter", "顺丰安全中心");
data.put("empDateOfIssue", "2019-12-3");
data.put("empCertificateNo", "20190100833257");
data.put("empApprovalNo_2019", "ZN-TP-CSS-20180002");
data.put("empTrainingDate_2019", "2019-11-3");
data.put("empCategory_2019", "初训");
data.put("empResult_2019", "合格");
data.put("empSignature_2019", "露西");
//data.put("answer", "张三");
// 7遍历data 给pdf表单表格赋值
for (String key : data.keySet()) {
form.setField(key, data.get(key).toString());
}
ps.setFormFlattening(true);
System.out.println("===============PDF导出成功=============");
} catch (Exception e) {
System.out.println("===============PDF导出失败=============");
e.printStackTrace();
} finally {
try {
ps.close();
reader.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Test
public void readPdf(){
String outputFileName = "2019_833257_out.pdf";
OutputStream os = null;
PdfStamper ps = null;
PdfReader reader = null;
PdfReader modelReader = null;
String inputFileName = "833257.pdf";
String modelUrl="certificateModel.pdf";
String pageContent="";
try {
os = new FileOutputStream(new File(outputFileName));
// 2 读入pdf表单
reader = new PdfReader(inputFileName);
modelReader = new PdfReader(modelUrl);
// 3 根据表单生成一个新的pdf
ps = new PdfStamper(modelReader, os);
// 4 获取pdf表单
AcroFields form = ps.getAcroFields();
BaseFont bf = BaseFont.createFont("d:/user/80004194/desktop/test/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
form.addSubstitutionFont(bf);
//读取pdf数据
int pageNum = reader.getNumberOfPages();
for(int i=1;i<=pageNum;i++){
pageContent = PdfTextExtractor.getTextFromPage(reader, i);
}
Map<String, Object> data = new HashMap<String, Object>();
String[] content=pageContent.split("
");
//获取所有的培训记录页:
List<String> record=new ArrayList<String>();
String empName="";
String empNumber="";
String empCompany="";
String empDepartment="";
String empTrainingCenter="";
String empDateOfIssue ="";
String empCertificateNo ="";
for(int i =0; i<content.length;i++){
if(content[i].contains("工号")){
String[] empNameAndNumber =content[i].split(" ");
empName = empNameAndNumber[1];
empNumber = empNameAndNumber[3];
}else if(content[i].contains("工作单位")){
String[] empCompanys =content[i].split(" ");
empCompany =empCompanys[1];
}else if(content[i].contains("工作部门")){
String[] empDepartments =content[i].split(" ");
empDepartment = empDepartments[1];
}else if(content[i].contains("培训机构")){
String[] empTrainingCenters =content[i].split(" ");
empTrainingCenter = empTrainingCenters[1];
}else if(content[i].contains("发证日期")){
String[] empDateOfIssues =content[i].split(" ");
empDateOfIssue = empDateOfIssues[1];
}else if(content[i].contains("证书编号")){
String[] empCertificateNos =content[i].split(" ");
empCertificateNo = empCertificateNos[1];
}else if((content[i].contains("初训") || content[i].contains("复训")) && !content[i].contains("有效期")){
record.add(content[i]);
}
}
data.put("empName", empName);
data.put("empNumber", empNumber);
data.put("empCompany", empCompany);
data.put("empDepartment", empDepartment);
data.put("empTrainingCenter", empTrainingCenter);
data.put("empDateOfIssue", empDateOfIssue);
data.put("empCertificateNo", empCertificateNo);
//写入历史详情数据
if(record.size()>0){
for (int i = 0; i < record.size(); i++) {
String[] records=record.get(i).split(" ");
data.put("empApprovalNo_"+(2019+2*i),records[0]);
data.put("empTrainingDate_"+(2019+2*i), records[1]);
data.put("empCategory_"+(2019+2*i),records[2]);
data.put("empResult_"+(2019+2*i), records[3]);
data.put("empSignature_"+(2019+2*i), records[4]);
}
}
//写入新的数据
data.put("empApprovalNo_"+2021, "ZN-GD-TP-0073");
data.put("empTrainingDate_"+2021, "2019-1-21");
data.put("empCategory_"+2021, "复训");
data.put("empResult_"+2021, "优秀");
data.put("empSignature_"+2021, "Jack");
//替换数据
for (String key : data.keySet()) {
form.setField(key, data.get(key).toString());
}
ps.setFormFlattening(true);
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
ps.close();
reader.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}