import com.hw.common.exception.BaseException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
/**
* @Description TODO
* @Author liqinglong
* @DateTime 2022-12-06 09:58
* @Version 1.0
*/
public class PdfUtils {
/**
* 根据模板创建生成pdf
* @param map 模板中的表单数据 key-表单属性值;value-值
* @param templatePath 模板路径
* @return 返回生成的pdf文件路径
*/
public static String createPdfByTemplate(Map<String,Object> map, String templatePath,String newPdfPath) {
PdfReader reader;
ByteArrayOutputStream bos;
PdfStamper stamper;
//生成的pdf文件存放地址 要确保文件夹的存在
newPdfPath = newPdfPath + (System.currentTimeMillis()) +".pdf";
System.out.println(newPdfPath);
try {
//设置字体是必须要的,不然没法向模板pdf里写值
//用以支持中文
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 读取pdf模板
reader = new PdfReader(templatePath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
//拿到pdf模板中的表单属性
AcroFields form = stamper.getAcroFields();
//设置字体
form.addSubstitutionFont(bfChinese);
java.util.Iterator<String> it = form.getFields().keySet().iterator();
//遍历表单属性,对每个属性赋值
while (it.hasNext()) {
String name = it.next().toString();
Object o = map.get(name);
String value = o == null ? null : o.toString();
if(value == null){
continue;
}
form.setField(name,value);
}
// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.setFormFlattening(true);
bos.close();
stamper.close();
Document doc = new Document();
File file = new File(newPdfPath);
PdfCopy copy = new PdfCopy(doc, new FileOutputStream(file));
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
} catch (IOException e) {
e.printStackTrace();
throw new BaseException("生成pdf失败-IOException,请联系管理员");
} catch (DocumentException e) {
throw new BaseException("生成pdf失败-DocumentException,请联系管理员");
}
return newPdfPath;
}
public static void main(String[] args) {
FilingPropertiesVo filingVo = new FilingPropertiesVo();
filingVo.setSaleRegistNo("123456789");
filingVo.setCustomerType(1);
filingVo.setRefereeName("张三");
filingVo.setRefereePhone("13809449028");
filingVo.setRefereeDept("酒业公司");
filingVo.setRefereePost("销售经理");
filingVo.setPersonContactName("李四");
filingVo.setPersonContactPhone("13809449026");
filingVo.setPersonContactNumber("522228198610071613");
filingVo.setMatName("茅台纪念酒1951|酱香型|53°|500ML|2017");
filingVo.setAmount(10);
filingVo.setMatCode("20221026");
filingVo.setMatName1("飞天茅台|53°|500ML|2015|酱香型");
filingVo.setAmount1(15);
filingVo.setMatCode1("12345678901001");
filingVo.setMatName2("白酒全家福|酱香型|53°|500ML|2022");
filingVo.setAmount2(20);
filingVo.setMatCode2("20221027");
filingVo.setDeliveryAddress("贵州省贵阳市南明区朝阳洞路50号");
Map<String,Object> dataMap = JSON.parseObject(JSON.toJSONString(filingVo));
String path = "E:/project/airport/airport_service/airport_admin/src/main/resources/file/template/pdf/";
String filePath = path + "registration_form.pdf";
PdfUtils.createPdfByTemplate(dataMap,filePath,path);
}
}
java根据pdf模板生成pdf
最新推荐文章于 2024-07-26 15:16:07 发布