如何使用JAVA生成自定义格式的pdf文件(IText)

更新时间:2023-07-31 06:02:41 阅读: 评论:0

如何使⽤JAVA⽣成⾃定义格式的pdf⽂件(IText)
最近接⼿⼀个⽣成PDF授权书的需求,公司使⽤JDK8,于是还是先搜了搜使⽤JAVA语⾔如何操作授权书,经过筛选最终选择了IText,因为api⽐较通俗,并且社区还算活跃,下⾯则是对⼀些常⽤到的⽅法做了个总结(使⽤的是itext5的api)
孕妇能喝八宝粥吗准备⼯作:加依赖,我使⽤的是Itext5,推荐使⽤最新的itext7,新的功能更加丰富,有时间我会进⾏升级。
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
⾸先,对于授权书的⽣成有三个想法:
1.填充⽂本⽅式
授权书嘛,想想就是pdf中⼤部分数据内容都是相同的,可能就是每个⽤户的授权主体,授权时间,电话什么的不同,所以思路就是预先⽣成好授权书的源⽂件,就是将对于⽤户相同的内容先写好到授权书,然后将对于每个⽤户不⽤的数据预留出空间来进⾏内容的填充。但是⽐较⿇烦的是需要定位的填充内容的位置,通过x,y的坐标定位填写内容的起始位置。
如上图,对于授权⽅相关的信息每个⽤户都是⼀样的,所以可以提前⽣成⼀个内容都写好的pdf⽂件,然后将被授权⽅需要填写的内容空出来,根据每个⽤户填写的不同信息分别填充不同的内容。代码:
// 读取pdf源⽂件
try(InputStream is = new FileInputStream(new File("C:\\Urs\\Administrator\\Desktop\\source_v1.pdf"))){
// 设置字体样式这种是使⽤⾃定义的字体,也可以使⽤默认的字体,但是默认字体格式太少,⼀般都是使⽤⾃定义的吧
BaFont font = ateFont("C:\\Urs\\Administrator\\Desktop\\f",BaFont.IDENTITY_H, BaFont.NOT_EMBEDDED);
// 替换⽂本
PdfReader reader = new PdfReader(is); // input PDF
PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(new File("C:\\Urs\\Administrator\\Desktop\\res.pdf"))); // output PDF            // 获取pdf⽂件第⼀页内容
PdfContentByte pageOne = OverContent(1);
// 被授权⽅
tText(pageOne,410,580,"王⼤套",font,10);
//社会信⽤代码
tText(pageOne,410,559,"130xxxxxxxxxxxxxx0223",font,10);
// 电话
tText(pageOne,410,538,"131xxxx1234",font,10);
// ⽤户ID
tText(pageOne,410,517,"uid123_adskjflEMDK-dagjKJETD",font,10);
// 绘制
pageOne.fill();
// 关闭
stamper.flush();
stamper.clo();
}catch (Exception e){
e.printStackTrace();
}
结果:
基本可以满⾜需求。
2.替换⽂本⽅式
这个也是个思路,但是没有使⽤,就是利⽤itext⽂本替换的功能,和⽅案1类似,将需要进⾏⾃定义填写的内容使⽤占位符,⽐如"被授权⽅:${the_auth_ur}",然后⽤替换⽅式将字符串为"${the_auth_ur}"替换成"王⼤套",也可以达到效果,这个⼤家可以试试,我也试过可以实现,但是没有使⽤。
3.从头⽣成
第三种就是如果每个⽤户的绝⼤部分内容都不相同呢,这样我们⽆法找个⼀个合适的⽂件作为源⽂件,所以⼲脆从头到尾⽣成⼀个pdf⽂件。
如上图,这是⼀般授权书的内容,有图⽚,有表格,有⽂字,⽰例代码:
String path = "C:\\Urs\\Administrator\\Desktop\\";
// 设置字体样式
⿊⾊字体
BaFont bfChine = ateFont(path + "f",BaFont.IDENTITY_H, BaFont.NOT_EMBEDDED);        Font blackFont = new Font(bfChine);
blackFont.tSize(12);
blackFont.tStyle(FontFactory.HELVETICA);
blackFont.tColor(BaColor.BLACK);
红⾊字体
Font redFont = new Font(bfChine);
redFont.tSize(12);
redFont.tStyle(FontFactory.HELVETICA);
redFont.tColor(BaColor.RED);
橙⾊字体
Font orangeFont = new Font(bfChine);
orangeFont.tSize(12);
orangeFont.tStyle(FontFactory.HELVETICA);
orangeFont.tColor(BaColor.ORANGE);
// 设置页⾯信息  A4纸,页边距
Document doc = new Document(PageSize.A4,90.14f,90.14f,72f,72f);
OutputStream out = new FileOutputStream(new File(path + "aaa.pdf"));
doc.open();
// 授权书标题
// 授权书标题
Paragraph title = new Paragraph("授权书(个⼈VIP)", blackFont);
title.tAlignment(Element.ALIGN_CENTER);
doc.add(title);
doc.add(new Paragraph("\n"));
// 设计信息表格
// 设置 3 列表格
PdfPTable table = new PdfPTable(3);
// 设置表格宽度
table.tTotalWidth(new float[]{20,40,40});// 这⾥是百分⽐,每列所占整个宽度的⽐例
table.tTotalWidth(414.992f);// 这是设置表格的定宽
table.tLockedWidth(true);// 这个必须设置为true,意思是按照我们设置的锁定表格的宽度
table.tHorizontalAlignment(Element.ALIGN_CENTER);
// 图⽚
Image cellimg = Instance(path + "thumb.png");
PdfPCell cell1 = new PdfPCell(cellimg, true);
cell1.tBorderColor(BaColor.GRAY);// 单元格边框颜⾊
cell1.tPaddingLeft(10);
cell1.tFixedHeight(100);
cell1.tLeading(20,1);跳棋规则及玩法
cell1.tHorizontalAlignment(Element.ALIGN_CENTER);
cell1.tVerticalAlignment(Element.ALIGN_CENTER);
cell1.tRowspan(4);
cell1.tFixedHeight(50);
cell1.tTop(10);
table.addCell(cell1);
// 授权⽅
PdfPCell cell2 = new PdfPCell(new Paragraph("授权⽅:xxxxx",blackFont));
cell2.tBorderColor(BaColor.GRAY);
cell2.tPaddingLeft(10);
cell2.tHorizontalAlignment(Element.ALIGN_LEFT);
cell2.tVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell2);
// 被授权⽅
PdfPCell cell3 = new PdfPCell();
国产护肤
Paragraph elements = new Paragraph();
elements.add(new Chunk("被授权⽅:王⼤套", blackFont));
cell3.tPhra(elements);
cell3.tBorderColor(BaColor.GRAY);
cell3.tPaddingLeft(10);
cell3.tHorizontalAlignment(Element.ALIGN_LEFT);
cell3.tVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell3);
// 授权⽅⽹站
PdfPCell cell4 = new PdfPCell(new Paragraph("授权⽅⽹站:xxxxx",blackFont));
cell4.tBorderColor(BaColor.GRAY);
cell4.tPaddingLeft(10);
cell4.tHorizontalAlignment(Element.ALIGN_LEFT);
cell4.tVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell4);
// ⾝份证/统⼀社会信⽤代码:
坐月子能洗澡吗
PdfPCell cell5 = new PdfPCell(new Paragraph("⾝份证/统⼀社会信⽤代码:xxxxxx",blackFont));        cell5.tBorderColor(BaColor.GRAY);
一次函数cell5.tPaddingLeft(10);
cell5.tHorizontalAlignment(Element.ALIGN_LEFT);
cell5.tVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell5);
// 地址
PdfPCell cell6 = new PdfPCell(new Paragraph("地址:xxxxxx",blackFont));
cell6.tBorderColor(BaColor.GRAY);
cell6.tPaddingLeft(10);
cell6.tHorizontalAlignment(Element.ALIGN_LEFT);
cell6.tVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell6);
// 地址
/
/ 地址
PdfPCell cell7 = new PdfPCell(new Paragraph("地址:xxxxxxx",blackFont));
cell7.tBorderColor(BaColor.GRAY);
cell7.tPaddingLeft(10);
cell7.tHorizontalAlignment(Element.ALIGN_LEFT);
cell7.tVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell7);
// 设计ID
PdfPCell cell8 = new PdfPCell();
Paragraph elements1 = new Paragraph();
elements1.add(new Chunk("内容id:", redFont));
示字组词
elements1.add(new Chunk("sdfasd-DALKDA-dagfsadf", blackFont));
cell8.addElement(elements1);
cell8.tBorderColor(BaColor.GRAY);
cell8.tPaddingLeft(10);
cell8.tHorizontalAlignment(Element.ALIGN_LEFT);
cell8.tVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell8);
// 联系电话
手串结尾打法图解
谢长留PdfPCell cell9 = new PdfPCell(new Paragraph("联系电话:xxxxxx",blackFont));
cell9.tBorderColor(BaColor.GRAY);
cell9.tPaddingLeft(10);
cell9.tHorizontalAlignment(Element.ALIGN_LEFT);
cell9.tVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell9);
// 空⽩
PdfPCell cell10 = new PdfPCell(new Paragraph(" ",blackFont));
cell10.tBorderColor(BaColor.GRAY);
cell10.tPaddingLeft(10);
cell10.tHorizontalAlignment(Element.ALIGN_LEFT);
cell10.tVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell10);
// 空⽩
PdfPCell cell11 = new PdfPCell(new Paragraph(" ",blackFont));
cell11.tBorderColor(BaColor.GRAY);
cell11.tPaddingLeft(10);
cell11.tHorizontalAlignment(Element.ALIGN_CENTER);
cell11.tVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell11);
// 被授权⽤户ID:
PdfPCell cell12 = new PdfPCell(new Paragraph("被授权⽤户ID:xxxxxx",blackFont));        cell12.tBorderColor(BaColor.GRAY);
cell12.tPaddingLeft(10);
cell12.tHorizontalAlignment(Element.ALIGN_LEFT);
cell12.tVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell12);
doc.add(table);
doc.add(new Paragraph("\n"));
// 授权内容
Paragraph temp = new Paragraph("1.授权内容", blackFont);
temp.tLeading(23);
temp.tAlignment(Element.ALIGN_LEFT);
doc.add(temp);
temp = new Paragraph("xxxxxx", blackFont);
temp.tLeading(23);
temp.tAlignment(Element.ALIGN_LEFT);
doc.add(temp);
doc.add(new Paragraph("\n"));// 换⾏
temp = new Paragraph("2.授权范围", blackFont);
temp.tLeading(23);
temp.tAlignment(Element.ALIGN_LEFT);
doc.add(temp);
temp = new Paragraph("xxxxxx", blackFont);

本文发布于:2023-07-31 06:02:41,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1102768.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:内容   授权书   授权   设置   字体   需要   表格
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图