//依赖jar包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<!--输出中文,还要引入下面itext-asian.jar包-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
//如何构建pdf模板呢?
首先去下载个Adobe Acrobat DC用来设置模板参数
将你的pdf文档(如果原来是doc那么用wps转一下)打开
// 模板路径
String path = "/***/doc/contract.pdf";
PdfReader reader;
ByteArrayOutputStream bos;//用来进行输出到浏览器的流
PdfStamper stamper;
try {
//获取数据
UserHr userHr = userHrDao.findByTokenAndUsable(token, Constant.USABLE);
Optional<OrderContract> optional = orderContractDao.findByIdAndUsable(id, Constant.USABLE);
if (!optional.isPresent()) {
return;
}
OrderContract orderContract = optional.get();
//pdf操作
reader = new PdfReader(path);// 读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
String downloadFilename = UUID.randomUUID()+".pdf";//文件的名称
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//宋体字体
//设置使用文字格式
form.setFieldProperty("company", "textfont", bf, null);
form.setFieldProperty("startDate","textfont",bf,null);
form.setFieldProperty("endDate","textfont",bf,null);
form.setFieldProperty("money","textfont",bf,null);
form.setFieldProperty("upperMoney","textfont",bf,null);
form.setFieldProperty("email","textfont",bf,null);
form.setFieldProperty("taxPayerId","textfont",bf,null);
//设置对应的值
form.setField("company", userHr.getCompany());
Date today = new Date();
form.setField("startDate", DateUtil.dataToString(today, "yyyy年MM月dd日"));
form.setField("endDate", DateUtil.dataToString(DateUtil.getOneYearLater(today, 1), "yyyy年MM月dd日"));
double money = orderContract.getTotalMoney();
form.setField("money", String.valueOf(money) );
form.setField("upperMoney", ConvertNumberToUpper.digitUppercase(money));
form.setField("email", userHr.getEmail());
form.setField("taxPayerId", userHr.getTaxpayerId());
//多选设置
Map<String,String> map=new HashMap<>();
map.put("1","A");
map.put("2","B");
map.put("3","C");
map.put("4","D");
map.put("5","E");
map.put("6","F");
String[] goodIds=orderContract.getGoodsId().split(";");
Iterator<String> iter=map.keySet().iterator();
while (iter.hasNext()){//直接map去remove元素是没有,必须使用迭代器才行
for(int j=0;j<goodIds.length;j++) {
if (iter.next().equals(goodIds[j])) {
iter.remove();
}
}
}
for(String i:map.keySet()){
form.setField(map.get(i),"on");
}
stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();//必须关闭否则无法向浏览器输出数据
/*如果你要下载到本地或者用来测试也可以
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage=null;
for (int j=1;j<=5;j++){//具体看你自己pdf的页数
importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), j);
copy.addPage(importPage);
}
doc.close();
*/
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/pdf;charset=utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + downloadFilename);
OutputStream out=response.getOutputStream();
out.write(bos.toByteArray());//写入respone
out.close();
bos.close();
reader.close();
} catch (IOException e) {
System.out.println(1);
} catch (DocumentException e) {
System.out.println(2);
}