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是一个*国人写的 ...
随机推荐
- javascript 实现 接口编程
// Constructor. var Interface = function (name, methods) { if (arguments.length != 2) { throw new Er ...
- Android Win7 上使用cmd生成Jks
Android Win7 上使用cmd生成Jks cmd 命令 C:\Program Files\Java\jre1.8.0_111\bin>keytool -genkeypair -alias ...
- Python学习杂记_8_从程序外部传参的办法sys.argv
Python用 sys.argv[] 实现从程序外部传参 “外部”的含义,其实就是这些参数不是你在程序中定义的,而是在程序之外通过输入操作传递进来的.sys.argv 会返回一个元组,元组的首个元素即 ...
- yii框架美化访问路径,去掉index.php/?r=部分
一.找到配置文件(ps:advance高级模板) 在工程目录-> backend目录 或 frontend目录 -> config目录 -> main.php文件 -> 在 r ...
- 记录: 百度webuploader 分片文件上传java服务器端(spring mvc)示例的优化
最近项目上用到文件分片上传,于是找到了百度的一个开源前端控件webuploader. 于是尝试使用. 下载下来后,它提供的服务器端示例代码是php版的,那么Java版的呢? 其实,上传文件都是按照rf ...
- AC日记——[ZJOI2006]物流运输 bzoj 1003
1003 思路: 最短路+dp: 节点在a-b天里不能使用 那么我们准备每一组a-b求一条最短路,如果没有,则用极大值表示: cost[a,b]记录这个最短路: 然后,开始dp: dp[i]=min( ...
- Block为什么使用Copy?
block:本质就是一个object-c对象block:存储位置,可能分为3个地方:代码去,堆区.栈区(ARC情况下会自动拷贝到堆区,因此ARC下只能有两个地方:代码去.堆区)代码区:不访问栈区的变量 ...
- POJ 2505 A multiplication game [博弈]
题意:两个人做游戏,每个人都可以在自己的回合里将数p乘以2到9之间的一个数,初始时p=1,谁先将p乘到大于等于n就算赢. 思路:一开始我算sg值,结果算来算去都没算明白... 后来看了别人题解,才豁然 ...
- 转:如何将 Java 项目转换成 Maven 项目
如何将 Java 项目转换成 Maven 项目 本文内容 Java 项目 Maven 项目 Java 项目转换成 Maven 项目 本文主要介绍如何将 Java 项目转换成 Maven 项目.首先要明 ...
- ios开发 - 获取从http上下载文件的大小
- (void)connectionNSURLConnection *)connection didReceiveResponseNSURLResponse *)response{ NSHTTPURL ...