需求 :财务将费用写入pdf模版交给客户确认。
使用的PdfStamper,即扣模板。模板上通过工具设置上name属性名
template.png
我使用的Wondershare PDFelement(Mac版)编辑的表单;
这种扣模版的实现方式灵活性比较低,不利于后期维护
使用jar包 :
jar.png
下面是代码部分
//模板路径
String filePath = request.getSession().getServletContext().getRealPath("/Files/Temp/Export/AFSStatement.pdf");
// 定义输出流数组,每页作为一个单独的流
ByteArrayOutputStream bos[] = null;
if (financialStatisticsQueryList != null && financialStatisticsQueryList.size() > 0) {
double usd = 0, unUsd = 0, sumUsd = 0, sumUnWriteOffUsd = 0;
//每页放25条数据
int pageNum = (int) Math.ceil(Double.valueOf(financialStatisticsQueryList.size()) / Double.valueOf(25));
bos = new ByteArrayOutputStream[pageNum];
int row = 1;
for (int k = 0; k < pageNum; k++) {
bos[k] = new ByteArrayOutputStream();
PdfReader reader = new PdfReader(filePath);// 读取pdf模板
PdfStamper stamper = new PdfStamper(reader, bos[k]); //生成输出流
AcroFields af = stamper.getAcroFields(); //获取文本域
//使用微软雅黑字体显示中文
BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
ArrayList<BaseFont> fontArrayList = new ArrayList<BaseFont>();
fontArrayList.add(baseFont);
af.setSubstitutionFonts(fontArrayList);
af.setField("page", (k+1) + "-" + pageNum);
af.setField("clearingUnitName", financialStatisticsQueryList.get(0).getClearingUnitName());//结算单位名称
af.setField("address", financialStatisticsQueryList.get(0).getAddress());//结算单位地址
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
af.setField("date", sdf.format(new Date()));//导出时间
String dueDate = "";
int size = (financialStatisticsQueryList.size() - 25 * k) < 26 ? financialStatisticsQueryList.size() - 25 * k : 25;
for (int i = 25*k,j = 0; j < size; i++,j++) {
usd = financialStatisticsQueryList.get(i).getTotalAmountUSD();
unUsd = financialStatisticsQueryList.get(i).getAmount();
//获取Due Date
if (financialStatisticsQueryList.get(i).getEtd() != null) {
dueDate = getLastDayOfMonth(financialStatisticsQueryList.get(i).getEtd());
}
af.setField("invNo" + row, financialStatisticsQueryList.get(i).getBusinessNum() != null ? financialStatisticsQueryList.get(i).getBusinessNum() : "");//业务编号
af.setField("invDate" + row, financialStatisticsQueryList.get(i).getEtd() != null ? sdf.format(financialStatisticsQueryList.get(i).getEtd()) : "");//离港日期
af.setField("MblNo" + row, financialStatisticsQueryList.get(i).getLadingBillNum() != null ? financialStatisticsQueryList.get(i).getLadingBillNum() : "");//提单号
af.setField("totalAmt" + row, String.valueOf(usd));//Total Amt
af.setField("unpaidAmt" + row, String.valueOf(unUsd));//Unpaid Amt
af.setField("dueDate" + row, dueDate);////Due Date
af.setField("refNo" + row, financialStatisticsQueryList.get(i).getProxyBillNum() != null ? financialStatisticsQueryList.get(i).getProxyBillNum() : "");//代理账单号
sumUsd = add(sumUsd, usd);
sumUnWriteOffUsd = add(sumUnWriteOffUsd, unUsd);
row++;
}
//下一页
row = 1;
if ((k+1) == pageNum) {
af.setField("total", "**Total**");//Total
af.setField("totalAmt", String.valueOf(sumUsd));//Total usd
af.setField("totalUnpaidAmt", String.valueOf(sumUnWriteOffUsd));//Total unpaid usd
}
stamper.setFormFlattening(true);
stamper.close();
reader.close();
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();// 输出流
Document doc = new Document(); //新建一个文档
PdfCopy copy = new PdfCopy(doc, out); //用于保存原页面内容,然后输出
if (bos != null) {
doc.open();
PdfImportedPage page;
//复制页
for (int i = 0; i < bos.length; i++) {
//页数为1,一页一页复制
page = copy.getImportedPage(new PdfReader(bos[i].toByteArray()), 1);
copy.addPage(page);
}
//关闭流
doc.close();
out.close();
} else {
//导出为空的case
ByteArrayOutputStream outEmpty = new ByteArrayOutputStream();// 输出流
PdfReader reader = new PdfReader(filePath);
PdfStamper stamper = new PdfStamper(reader, outEmpty);
AcroFields af = stamper.getAcroFields(); //获取文本域
//使用微软雅黑字体显示中文
BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
ArrayList<BaseFont> fontArrayList = new ArrayList<BaseFont>();
fontArrayList.add(baseFont);
af.setSubstitutionFonts(fontArrayList);
af.setField("page", 1 + "-" + 1);
af.setField("clearingUnitName", "");//结算单位名称
af.setField("address", "");//结算单位地址
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
af.setField("date", sdf.format(new Date()));//导出时间
stamper.setFormFlattening(true);
stamper.close();
reader.close();
return outEmpty.toByteArray();
}
return out.toByteArray();
vans.png











网友评论