使用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. 【HugeChm】HugeChm制作chm帮助文档

    1.下载软件:HugeChm.exe 2.开始打包:  3.选择开始打包即可

  2. python 复习-2

    把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件件内容,然后反序,再追加到文件的下一行中 """把一个数字的list从小到大排序,然后写入文件,然 ...

  3. Day 21 Object_oriented_programming_2

    继承实现原理 python中的类可以同时继承多个父类,继承的顺序有两种:深度优先和广度优先. 一般来讲,经典类在多继承的情况下会按照深度优先的方式查找,新式类会按照广度优先的方式查找 示例解析: 没有 ...

  4. springBoot 读取配置文件yml中的信息

    yml中自定义一些变量 var: analyze_url: test ocr_url: test microsoft_key: test 映射到类变量中 @Getter @Component publ ...

  5. TreeMap和TreeSet的区别与联系

    TreeMap 和 TreeSet 是 Java Collection Framework 的两个重要成员,其中 TreeMap 是 Map 接口的常用实现类,而 TreeSet 是 Set 接口的常 ...

  6. 伪全栈工程师做的有点简陋的ui设计

    站酷:http://www.zcool.com.cn/work/ZMjEwMDIxMDA=.html 这个app 叫自我时间管理  是一个 工具  管理自己开会 购物 健身 记账等 的提醒与管理,还可 ...

  7. luogu P3376 【模板】网络最大流(no)ek

    题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...

  8. Linux下Shell脚本字符串单引号、双引号、反引号、反斜杠的作用和区别

    一.单引号 str='this is a string' 单引号字符串的限制: 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的: 单引号字串中不能出现单引号(对单引号使用转义符后也不行) ...

  9. xamarin.from ToolbarItem 字体大小和颜色更改

    在xamarin.from 上我们经常会使用到页面跳转方式, new NavigationPage(newp page()){ BarBackgroundColor=Color.FromHex(&qu ...

  10. source(.)/export/shell

    用户登录到Linux系统后,系统将启动一个用户shell.在这个shell中,可以使用shell命令或声明变量,也可以创建并运行shell脚本程序.运行shell脚本程序时,系统将创建一个子shell ...