使用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;
}
}

java操作pdf的更多相关文章

  1. 转 Java操作PDF之iText详细入门

    转 Java操作PDF之iText详细入门 2016年08月08日 11:06:00 阅读数:19490 iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成 ...

  2. Java 操作pdf与excel

    java 操作pdf组件  itextpdf <dependency> <groupId>com.itextpdf</groupId> <artifactId ...

  3. java操作pdf添加页眉条码添加水印图片

    添加条码页眉以及图片水印 1. 引入jar包     1. itext-4.2.1.jar     2. itext-asian-5.2.0.jar     3. jbarcode-0.2.8.jar ...

  4. Java操作PDF之itext入门

    转载:http://lichunhui.iteye.com/blog/1550584 iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档 ...

  5. Java操作PDF之iText超入门

    iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转化为PDF文件. http://itextpdf.c ...

  6. Java操作pdf: JarsperReport的简单使用

    在企业级应用开发中,报表生成.报表打印下载是其重要的一个环节.除了 Excel 报表之外,PDF 报表也有广泛的应用场景. 目前世面上比较流行的制作 PDF 报表的工具如下: iText PDF :i ...

  7. Java 借助poi操作PDF工具类

    ​ 一直以来说写一个关于Java操作PDF的工具类,也没有时间去写,今天抽空写一个简单的工具类,拥有PDF中 换行,字体大小,字体设置,字体颜色,首行缩进,居中,居左,居右,增加新一页等功能,如果需要 ...

  8. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  9. java操作Excel、PDF文件

    java操作Excel.PDF文件 分享者:Vashon 分享来源:CSDN博客 下面这些是在开发中用到的一些东西,有的代码贴的不是完整的,只是贴出了关于操作EXCEL的代码: jxl是一个*国人写的 ...

随机推荐

  1. poj 1795 DNA Laboratory

    DNA Laboratory Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 2892   Accepted: 516 Des ...

  2. python面试模拟真题讲解

    一.选择题(32分) 1.python不支持的数据类型有:(A) A.char B.int C.float D.list 2.x = “foo” y = 2 print(x+y)           ...

  3. TCP/IP协议详解笔记——ARP协议和RARP协议

    ARP:地址解析协议 对于以太网,数据链路层上是根据48bit的以太网地址来确定目的接口,设备驱动程序从不检查IP数据报中的目的IP地址.ARP协议为IP地址到对应的硬件地址之间提供动态映射. 工作过 ...

  4. 【XPButton类】美化MFC button (转)

    从网上找到别人写的一个XPButton类,利用XPButton类实现XP风格的按钮.百度一下即可找到这个类,接下来具体的步骤如下: 1.创建基于对话框的MFC工程假设命名为:XPButtonTest, ...

  5. android基本控件学习-----Button

    Button讲解: 一.在我们实际的使用button的时候经常会对button不同状态会有不同的显示,在讲解Button前,首先对drawable下面的statelistdrawable的相关知识讲一 ...

  6. 《手把手教你学C语言》学习笔记(9)--- 程序的选择控制

    C语言是面向过程编程语言的主要代表,其特征就是严格控制程序的执行语句顺序,因此,C程序的主要结构控制就是顺序控制,以main函数为入口函数,根据控制,一条一条地执行语句.由于实际需求是很复杂的,只用顺 ...

  7. OSX 系统无法直接用 Chrome 双击点击打开本地 html 文件

    在 Mac OS X 下,文件经常会被附加上 OS X 特有的扩展属性 ( extend attributes ),具体表现是用 ls -l 查看时会有 @ 的标记,譬如: $ ls -l index ...

  8. Cryptography I 学习笔记 --- 基于陷门置换的公钥加密

    RSA算法的工作流程 1. 生成公钥私钥 生成两个素数p和q,计算n=p*q,计算φ(n)=n-p-q+1,然后生成e与d,使 e * d = 1 mod φ(n). 然后以(n, e)作为公钥,(n ...

  9. 记一个react拖动排序中的坑:key

    在做一个基于react的应用的时候遇到了对列表拖动排序的需求.当使用sortable对列表添加排序支持后发现一个问题:数据正确排序了,但是dom的顺序却乱了,找了一会儿原因后发现是因为在渲染数据的时候 ...

  10. Jsp2.0自定义标签(第三天)——EL表达式的使用

    1.提出问题: 我们经常会看到这样的jsp页面代码: 浏览器显示: 为什么会在页面输出:Hello World  ,${per}究竟是如何找到“Hello World”的呢? 2.分析问题: 要想解决 ...