java操作pdf
使用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的更多相关文章
- 转 Java操作PDF之iText详细入门
转 Java操作PDF之iText详细入门 2016年08月08日 11:06:00 阅读数:19490 iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成 ...
- Java 操作pdf与excel
java 操作pdf组件 itextpdf <dependency> <groupId>com.itextpdf</groupId> <artifactId ...
- java操作pdf添加页眉条码添加水印图片
添加条码页眉以及图片水印 1. 引入jar包 1. itext-4.2.1.jar 2. itext-asian-5.2.0.jar 3. jbarcode-0.2.8.jar ...
- Java操作PDF之itext入门
转载:http://lichunhui.iteye.com/blog/1550584 iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档 ...
- Java操作PDF之iText超入门
iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转化为PDF文件. http://itextpdf.c ...
- Java操作pdf: JarsperReport的简单使用
在企业级应用开发中,报表生成.报表打印下载是其重要的一个环节.除了 Excel 报表之外,PDF 报表也有广泛的应用场景. 目前世面上比较流行的制作 PDF 报表的工具如下: iText PDF :i ...
- Java 借助poi操作PDF工具类
一直以来说写一个关于Java操作PDF的工具类,也没有时间去写,今天抽空写一个简单的工具类,拥有PDF中 换行,字体大小,字体设置,字体颜色,首行缩进,居中,居左,居右,增加新一页等功能,如果需要 ...
- java操作office和pdf文件java读取word,excel和pdf文档内容
在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...
- java操作Excel、PDF文件
java操作Excel.PDF文件 分享者:Vashon 分享来源:CSDN博客 下面这些是在开发中用到的一些东西,有的代码贴的不是完整的,只是贴出了关于操作EXCEL的代码: jxl是一个*国人写的 ...
随机推荐
- pat 甲级 Public Bike Management
Public Bike Management (30) 题目描述 There is a public bike service in Hangzhou City which provides grea ...
- HDU 4388 Stone Game II {博弈||找规律}
Stone Game II Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- *LOJ#2085. 「NOI2016」循环之美
$n \leq 1e9,m \leq 1e9,k \leq 2000$,求$k$进制下$\frac{x}{y}$有多少种不同的纯循环数取值,$1 \leq x \leq n,1 \leq y \leq ...
- c#中使用事务
原文发布时间为:2009-04-14 -- 来源于本人的百度文章 [由搬家工具导入] 问:为什么要用事务? 答:事务保证要么一组操作执行成功,要么全不执行。。。。 /// <summary> ...
- 添加一种emit的应用,反射发出,较直接调用稍慢,但好过反射与表达式树。
System.Reflection.MethodInfo mInfo = typeof(TypeParse).GetMethod("Add", System.Reflection. ...
- js判断手机的左右滑动
js代码 $(function() { function judge() { var startx;//让startx在touch事件函数里是全局性变量. var endx; var el = doc ...
- Error-invalid project description(转)
导入android工程时有时会出现下面错误: 按照提示,说是当前的工作空间内已经有同名的工程了,但实际是没有的. 多次碰到这种问题后,无意间找到解决办法.导入工程时,不要选择Android,而是选择G ...
- nslookup命令详解【转】
转自:http://blog.chinaunix.net/uid-28933499-id-3750357.html NSlookup命令的用法 用了域名服务器后,经常要查询域名的解析情况,nslook ...
- C#性能优化篇 - 基于索引器封装EPList
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...
- Linux定时关机
sudo shutdown -h +120 :两小时后关机sudo shutdown -h 23:00 :表示在23点定时关机 一.shutdown命令关机 各参数功能: -c 取消前一个shutdo ...