第一步 准备好pdf模板
要先下载 安装Adobe Acrobat DC 分享一下地址 https://pan.baidu.com/s/15GoD_z9LdLcHWIoVIzdH0A 密码 tyq3
然后将你要赋值的参数名写上
第二步 接下来写的代码
<!-- itext pdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>${itextpdf.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>${itext-asian.version}</version>
</dependency>
@RequestMapping(value = "download")
public void downLoadAccessApply(Long applyId, HttpServletResponse response, HttpServletRequest request)
throws IOException, DocumentException {
String filename = "物联网API接入申请单.pdf";
if (HttpRequest.isMSBrowser(request)) {
// IE浏览器的乱码问题解决
filename = URLEncoder.encode(filename, "UTF-8");
} else {
// 万能乱码问题解决
filename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
}
// 设置请求返回类型
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
ResponseVO queryAccessApplyDetail = accessApplyService.queryAccessApplyDetail(applyId,CommonFunction.getUser(request).getUserId());
AccessApplyDetailVO aadv = (AccessApplyDetailVO) queryAccessApplyDetail.getResult().get(0);
PdfReader reader;
ByteArrayOutputStream bos;
PdfStamper stamper;
OutputStream out1 = response.getOutputStream();
// 读取pdf模板
reader = new PdfReader(AccessApplyConstant.PDF);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
// 封装参数
encapsulationField(form, aadv);
// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.setFormFlattening(true);
stamper.close();
// 1,创建文档对象
Document document = new Document();
PdfCopy copy = new PdfCopy(document, out1);
// 2,监听输出流
PdfWriter instance = PdfWriter.getInstance(document, out1);
// 3,打开文档
document.open();
// 4,添加文本
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
try {
// 5,关闭文档
document.close();
instance.flush();
instance.close();
out1.flush();
out1.close();
} catch (Exception e) {
log.info("The document has no pages");
}
}
public void encapsulationField(AcroFields form, AccessApplyDetailVO aadv) throws DocumentException, IOException {
// 使用中文字体
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
form.setField("serialNo", aadv.getSerialNo());
form.setFieldProperty("customerName", "textfont", bf, null); // 设置中文格式
form.setField("customerName", aadv.getCustomerName());
form.setFieldProperty("contactName", "textfont", bf, null); // 设置中文格式
form.setField("contactName", aadv.getContactName());
form.setFieldProperty("contactPhone", "textfont", bf, null); // 设置中文格式
form.setField("contactPhone", aadv.getContactPhone());
form.setFieldProperty("address", "textfont", bf, null); // 设置中文格式
form.setField("address", aadv.getAddress());
form.setField("ip", aadv.getIp());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
form.setField("applyTime", sdf.format(aadv.getApplyTime()));
String userTime = "";
if (aadv.getUseTime().equals(AccessApplyEnum.THREE_MONTH.getCode())) {
userTime = AccessApplyEnum.THREE_MONTH.getMsg();
} else if (aadv.getUseTime().equals(AccessApplyEnum.SIX_MONTH.getCode())) {
userTime = AccessApplyEnum.SIX_MONTH.getMsg();
} else if (aadv.getUseTime().equals(AccessApplyEnum.ONE_YEAR.getCode())) {
userTime = AccessApplyEnum.ONE_YEAR.getMsg();
} else if (aadv.getUseTime().equals(AccessApplyEnum.TWO_YEAR.getCode())) {
userTime = AccessApplyEnum.TWO_YEAR.getMsg();
} else if (aadv.getUseTime().equals(AccessApplyEnum.THREE_YEAR.getCode())) {
userTime = AccessApplyEnum.THREE_YEAR.getMsg();
} else {
userTime = "未知";
}
form.setField("useTime", userTime);
Set<ApiInfo> apiInfoSet = aadv.getApiInfoSet();
StringBuilder apiInfos = new StringBuilder();
for (ApiInfo api : apiInfoSet) {
apiInfos.append(" " + api.getApiNo() + " " + api.getApiName() + "\n");
}
form.setFieldProperty("apiInfoSet", "textfont", bf, null); // 设置中文格式
form.setField("apiInfoSet", apiInfos.toString());
StringBuilder type = new StringBuilder();
if (aadv.getType().equals(AccessApplyEnum.TEST.getCode())) {
type.append(AccessApplyEnum.TEST.getMsg());
} else if (aadv.getType().equals(AccessApplyEnum.COMMERCIAL.getCode())) {
type.append(AccessApplyEnum.COMMERCIAL.getMsg());
} else {
type.append("未知");
}
form.setFieldProperty("type", "textfont", bf, null); // 设置中文格式
form.setField("type", type.toString());
form.setFieldProperty("specificContent", "textfont", bf, null); // 设置中文格式
form.setField("specificContent", aadv.getSpecificContent());
}
搞定,记录下 。
有问题,欢迎留言。