使用itext通过pdf模板打印pdf步骤如下:
一、制作模板(通过Adobe Acrobat DC或其它方式)
二、实现抽象类AbstractPdfStamperView
package com.gymboree.cos.fin.contract.info.view;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.document.AbstractPdfStamperView;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfStamper;
public class PdfStamperView extends AbstractPdfStamperView {
public static final String DATA = "data";
public static final String FILENAME = "mergePdfFileName";
@Override
protected void mergePdfDocument(Map model,
PdfStamper stamper, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 设置文件已附件形式下载
// response.setHeader("Content-Disposition", "attachment;filename=" + new String(model.get(FILENAME).toString().replaceAll(" ", "").getBytes("UTF-8"), "ISO8859-1"));
// 获取pdf模板上设置的变量
AcroFields fields = stamper.getAcroFields();
// 使用本机自带字体
// BaseFont bf = BaseFont.createFont("/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
//BaseFont bf = BaseFont.createFont("/usr/share/fonts/TTF/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
// 设置模板上字体的格式
fields.addSubstitutionFont(bf);
// 填充模板上数据
fillData(fields, (Map) model.get(DATA));
// System.out.println(((Map) model.get(DATA)).get("Signature"));
// 设置文本域是否可以编辑 true:不可以编辑
stamper.setFormFlattening(true);
}
private void fillData(AcroFields fields, Map data)
throws IOException, DocumentException {
for (String key : data.keySet()) {
String value = data.get(key);
fields.setField(key, value);
}
}
}
三、在SpringMVC的配置文件中配置视图
四、controller中的业务逻辑处理
@RequestMapping(value="/exportPDF/{contractId}")
public String contractPrintPreview(Model model,@PathVariable("contractId")String contractId,HttpServletResponse response,HttpServletRequest request){
// System.out.println("contractId: " + contractId);
try {
ContractAndRelatedInfoDto contractAndRelatedInfoDto = contractManagementService.queryContractAndRelatedInfoByContractId(contractId);
Map data = new HashMap();
// 开始时间
data.put("EFFECTIVE_TIME", contractAndRelatedInfoDto.getContractInfoDto().getEffectiveDate());
// 截止时间
data.put("END_TIME", contractAndRelatedInfoDto.getContractInfoDto().getEndDate());
// 有效期
String periodTime = contractAndRelatedInfoDto.getContractInfoDto().getPeriodOfValidity();
String[] periodArr = periodTime.split("\u002B");
if(periodArr.length > 1){
int total = 0;
for(int i = 0; i < periodArr.length; i++){
String str = periodArr[i];
str = str.substring(0, str.length()-2);
total += Integer.valueOf(str);
}
periodTime = String.valueOf(total);
}else{
periodTime = periodTime.substring(0, periodTime.length()-2);
}
data.put("PERIOD_OF_VALIDITY", periodTime);
// 总课时数
data.put("TOTAL_COURSE_NUM", contractAndRelatedInfoDto.getContractInfoDto().getCourseNum());
// 课程金额
data.put("TOTAL_COURSE_PRICE", contractAndRelatedInfoDto.getContractInfoDto().getCourseAmount());
// 所属中心名
data.put("C_ID", contractAndRelatedInfoDto.getContractInfoDto().getCenterName());
data.put("Signature", contractAndRelatedInfoDto.getContractInfoDto().getContractId());
// System.out.println("Signature: " + contractAndRelatedInfoDto.getContractInfoDto().getContractId());
// 数据
model.addAttribute(PdfStamperView.DATA, data);
// 文件名称
model.addAttribute(PdfStamperView.FILENAME, "学员培训协议_" + contractAndRelatedInfoDto.getContractInfoDto().getContractId() + ".pdf");
//同时生成一条打印记录
savePrintRecord(contractAndRelatedInfoDto.getContractInfoDto().getContractCode());
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return "contract";
}



