思路
根據(jù)業(yè)務(wù)需求沧踏,獲取實際業(yè)務(wù)數(shù)據(jù)歌逢,借助itext庫功能,將業(yè)務(wù)數(shù)據(jù)填充入提前制作好的PDF模板中
操作步驟
- 制作PDF模板:提前下載好Adobe Acrobat Pro DC
- 效果展示
準(zhǔn)備PDF,如下圖
編輯PDF
在
準(zhǔn)備表單
設(shè)置字體
設(shè)置多行
模板表單標(biāo)簽
標(biāo)簽綁定具體業(yè)務(wù)數(shù)據(jù)
- pom依賴
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
- 具體實現(xiàn)
private ByteArrayOutputStream getYearData() {
ClassPathResource classPathResource = new ClassPathResource("template/pdf-template.pdf");
PdfReader reader;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
reader = new PdfReader(classPathResource.getStream());
bos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
Map<String, String> map = new HashMap<>();
map.put("changeTag1", ReportConstant.CHOOSE_FLAG);
map.put("changeTag2", ReportConstant.CHOOSE_FLAG);
map.put("passport0", "88484695274732");
map.put("passport1", "88484695274733");
map.put("signDate0", DateUtil.format(new Date(), ReportConstant.DATE_FORMAT_YMS));
map.put("signDate1", DateUtil.format(new Date(), ReportConstant.DATE_FORMAT_YMS));
map.put("validDate0", DateUtil.format(new Date(), ReportConstant.DATE_FORMAT_YM));
map.put("validDate1", DateUtil.format(new Date(), ReportConstant.DATE_FORMAT_YM));
map.put("orgName0", "賢一坊科技有限公司");
map.put("orgName1", "賢一坊科技有限公司");
map.put("remark0", "這是一個備注翘狱,里面的內(nèi)容并不是無限長");
map.put("remark1", "這是一個備注秘案,里面的內(nèi)容并不是無限長");
this.fillPdfCellForm(map, form);
// true代表生成的PDF文件不可編輯
stamper.setFormFlattening(true);
stamper.close();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
return bos;
}
private void fillPdfCellForm(Map<String, String> map, AcroFields form) throws IOException, DocumentException {
for (Map.Entry entry : map.entrySet()) {
String key = (String) entry.getKey();
String value = (String) entry.getValue();
form.setField(key, value);
}
}
效果展示
最后
我這里準(zhǔn)備了一線大廠面試資料和我原創(chuàng)的超硬核PDF技術(shù)文檔,以及我為大家精心準(zhǔn)備的多套大廠面試題(不斷更新中)潦匈,需要的朋友可以關(guān)注我的公眾號:前程有光阱高,獲取茬缩!希望大家都能找到心儀的工作赤惊!