使用pdf模板生成pdf

1,工具 Adobe Acrobat X Pro

2,pom文件配置

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>

3,如果是比较复杂的pdf比如需要合并多个pdf文件可以参考如下代码

public class PdfViewUtil {

/**
* 生成合同pdf文件
*
* @param model 表单字段对应需要填充的数据
* @param list 出借人出借详情列表
* @param reType 还款方式
* @param producid 产品类型
* @return
* @throws IOException
* @throws DocumentException
*/
public static File getPdfFile(Map<String, String> model,List<ViewInvestPdf> list,String reType,Integer producid) throws IOException, DocumentException{
PdfReader reader = null;
if(producid == 5 || producid == 6 || producid == 7){
reader = new PdfReader(PdfViewUtil.class.getResourceAsStream("/template2.pdf"));
}else {
reader = new PdfReader(PdfViewUtil.class.getResourceAsStream("/template1.pdf"));
}
String tempDir = SystemUtil.getTempDir();
String prefix = UUID.randomUUID().toString().replaceAll("-", "");
String tempFileName = tempDir +File.separator+ prefix + ".pdf";// 模板生成的临时文件
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(tempFileName));
AcroFields fields = stamper.getAcroFields();
fillData(fields, model);
stamper.setFreeTextFlattening(true);
stamper.close();
String fileName = createTable(list,reType);//生成附件
Document doc = new Document();
prefix = UUID.randomUUID().toString().replaceAll("-", "");
String finalFileName = tempDir + File.separator + prefix + ".pdf";//最终生成的pdf临时文件
PdfCopy copy = new PdfCopy(doc, new FileOutputStream(finalFileName));
doc.open();
PdfReader r1 = new PdfReader(tempFileName);
int n = r1.getNumberOfPages();
for(int i = 1;i<=n;i++){
doc.newPage();
PdfImportedPage page = copy.getImportedPage(r1, i);
copy.addPage(page);
if(i == n){

PdfReader r2 = new PdfReader(fileName);
int m = r2.getNumberOfPages();
for(int j = 1;j<=m;j++){

doc.newPage();
PdfImportedPage page1 = copy.getImportedPage(r2, j);
copy.addPage(page1);
}
}
}
doc.close();
File file = new File(finalFileName);
return file;
}

/**
* 填充模板表单数据
*
* @param fields pdf表单字段
* @param data 表单字段填充的数据
* @throws IOException
* @throws DocumentException
*/
private static void fillData(AcroFields fields,Map<String, String> data) throws IOException, DocumentException{
for(String key :data.keySet()){
String value = data.get(key);
fields.setField(key, value);
}
}

/**
* 生成附件pdf模板(出借人列表
*
* @param list 出借人出借详情列表
* @param reType 还款方式
* @return
* @throws DocumentException
* @throws IOException
*/
private static String createTable(List<ViewInvestPdf> list,String reType) throws DocumentException, IOException{
Document document = new Document();
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // 中文处理
Font FontChinese = new Font(bfChinese, 10, Font.NORMAL); // 其他所有文字字体
Font BoldChinese = new Font(bfChinese, 14, Font.BOLD); // 粗体
String tempDir = SystemUtil.getTempDir();
String prefix = UUID.randomUUID().toString().replaceAll("-", "");
String fileName = tempDir +File.separator+ prefix + ".pdf";
PdfWriter pdfwWriter = PdfWriter.getInstance(document, new FileOutputStream(fileName));
document.open();
// 写数据
document.add(new Paragraph("附件一",FontChinese));
document.add(Chunk.NEWLINE);
PdfPTable table = new PdfPTable(7);
table.addCell(new Phrase("网站id",FontChinese));
table.addCell(new Phrase("出借金额",FontChinese));
table.addCell(new Phrase("借款期限",FontChinese));
table.addCell(new Phrase("年化利率",FontChinese));
table.addCell(new Phrase("还款方式",FontChinese));
table.addCell(new Phrase("应还款日",FontChinese));
table.addCell(new Phrase("到期应收本息",FontChinese));
if(!list.isEmpty()){
ViewInvestPdf viewPdf = list.get(0);
String period = viewPdf.getPeriod() + "个月";
String apr = viewPdf.getApr() + "%";
NumberFormat nf = new DecimalFormat("###,##0.00");
for(ViewInvestPdf pdf : list){
String userName = pdf.getUserName();
userName = userName.substring(0, 1) + "**" + userName.substring(userName.length() - 1);
table.addCell(new Phrase(userName,FontChinese));
table.addCell(new Phrase("¥"+nf.format(pdf.getAmount()),FontChinese));
table.addCell(new Phrase(period,FontChinese));
table.addCell(new Phrase(apr,FontChinese));
table.addCell(new Phrase(reType,FontChinese));
table.addCell(new Phrase(new JDateTime(pdf.getReceiveTime()).toString("YYYY-MM-DD"),FontChinese));
table.addCell(new Phrase("¥"+nf.format(pdf.getWaitAmount()),FontChinese));
}
}
document.add(table);
document.close();
return fileName;
}
}

最新文章

  1. 圆角button
  2. IMAQ Flatten Image to String VI的参数设置对比
  3. 如何从零开始学习DIV+CSS
  4. [妙味JS基础]第七课:运算符、流程控制
  5. POJ1837--二维背包
  6. [转]Android sharedpreferences使用
  7. [codility] Lession1 - Iterations - BinaryGap
  8. .net core自定义高性能的Web API服务网关
  9. Ubuntu19.04配置SS+pac非全局代理
  10. IDEA与eclipse:vm参数调优笔记
  11. 基于.net core 2.0+mysql+AceAdmin搭建一套快速开发框架
  12. network / switchboard / jiaohuanji
  13. day03 变量 运算符 基本数据类型 输出功能 格式化输出
  14. Codeforces 1076F Summer Practice Report dp
  15. DOMContentLoaded方法
  16. ORA错误总结
  17. docker swarm join如何获取token
  18. APP获取证书签名指纹
  19. 使用yum安装php72
  20. bzoj5016 一个简单的询问

热门文章

  1. POJ3983 快算24
  2. http://www.codeproject.com/KB/validation/MultipleDataAnnotations.aspx
  3. Android开发跳坑记录
  4. jquery.transform
  5. echarts中关于merge的代码
  6. 稀疏编码(Sparse Coding)的前世今生(一) 转自http://blog.csdn.net/marvin521/article/details/8980853
  7. Codeforces 766E Mahmoud and a xor trip(树形DP)
  8. php 经验之谈
  9. Linux运维:CentOS6和7的区别
  10. php cli模式下调试