POM依赖
<!-- PDF生成jar包iText -->
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
生成PDF实现代码
@Test
public void generatePDF() {
//写出一个pdf的文档文件
try {
//1.创建一个文档
Document document = new Document();
//2.指定文档的输出的位置
PdfWriter.getInstance(document, new FileOutputStream("c:/tmp/report.pdf"));
//3.打开文档
document.open();
//4.准备开始写入内容
// 设置文档字体(宋体)(使其支持中文)
BaseFont baseFontChinese = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont, AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED);
//====文档标题
//设置文档标题字体
Font headerFont=new Font(baseFontChinese, 12, Font.NORMAL, BaseColor.DARK_GRAY);
//构建一个段落
Paragraph headerParagraph=new Paragraph("中国工商银行账户管家子账户明细清单", headerFont);
headerParagraph.setAlignment(Paragraph.ALIGN_CENTER);
document.add(headerParagraph);
//====文档正文
Font font = new Font(baseFontChinese, 6, Font.NORMAL);
PdfPTable title2 = new PdfPTable(5);
title2.getDefaultCell().setBorder(0);
title2.setWidthPercentage(100);
title2.addCell(new Paragraph("地区号:3602", font));
title2.addCell(new Paragraph("网点号:0010", font));
title2.addCell(new Paragraph("币种:人民币(本位币) 单位:元", font));
title2.addCell(new Paragraph("", font));
PdfPCell cell = new PdfPCell(new Paragraph("2021年 页号:1", font));
cell.setHorizontalAlignment(PdfContentByte.ALIGN_RIGHT);
cell.setBorder(0);
title2.addCell(cell);
//段落添加到文档
document.add(title2);
//构建一个表格(表格的初始化)
//创建一个有4列的表格
PdfPTable title3 = new PdfPTable(4);
title3.getDefaultCell().setBorder(0);
title3.setWidthPercentage(100);
title3.addCell(new Paragraph("主账号:8888888888888888", font));
title3.addCell(new Paragraph("主账户名称:cappuccino", font));
title3.addCell(new Paragraph("", font));
title3.addCell(new Paragraph("", font));
//段落添加到文档
document.add(title3);
//构建一个表格(表格的初始化)
//创建一个有4列的表格
PdfPTable title4 = new PdfPTable(4);
title4.getDefaultCell().setBorder(0);
title4.setWidthPercentage(100);
title4.addCell(new Paragraph("分账簿序号:", font));
title4.addCell(new Paragraph("分账簿别名:", font));
title4.addCell(new Paragraph("", font));
title4.addCell(new Paragraph("", font));
//段落添加到文档
document.add(title4);
//构建一个表格(表格的初始化)
//创建一个有4列的表格
PdfPTable table1 = new PdfPTable(4);
font = new Font(baseFontChinese, 6);
table1.setWidthPercentage(100);
PdfPCell cell1 = new PdfPCell(new Paragraph("子账户序号:000000001", font));
cell1.setBorderWidthRight(0);
table1.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Paragraph("子账户别名:无", font));
cell2.setBorderWidthLeft(0);
cell2.setBorderWidthRight(0);
table1.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Paragraph("", font));
cell3.setBorderWidthLeft(0);
cell3.setBorderWidthRight(0);
table1.addCell(cell3);
PdfPCell cell4 = new PdfPCell(new Paragraph("", font));
cell4.setBorderWidthLeft(0);
table1.addCell(cell4);
//段落添加到文档
document.add(table1);
PdfPTable table2 = new PdfPTable(11);
table2.setWidthPercentage(100);
table2.addCell(new Paragraph("日期", font));
table2.addCell(new Paragraph("业务种类", font));
table2.addCell(new Paragraph("凭证种类/证实书号", font));
table2.addCell(new Paragraph("凭证号码/存期", font));
table2.addCell(new Paragraph("对方账号", font));
table2.addCell(new Paragraph("对方户名", font));
table2.addCell(new Paragraph("摘要", font));
table2.addCell(new Paragraph("借方发生额", font));
table2.addCell(new Paragraph("贷方发生额", font));
table2.addCell(new Paragraph("余额", font));
table2.addCell(new Paragraph("备注", font));
//段落添加到文档
document.add(table2);
document.add(new Paragraph("初期金额:38,849,872,83 截止 2021年01月31日, 账户余额(额度):10,218,335,37 ,保留余额:0.00, 冻结金额:0.00 ,透支余额:0.00, 可用余额:10,218,335,37", font));
document.add(new Paragraph("打印次数:1", font));
document.add(new Paragraph("系统时间:2021-02-01-01.48.40.154462", font));
document.add(new Paragraph("交易日期:2021-01-31", font));
document.add(new Paragraph("备注:凭证中类(活期展现凭证种类、定期、通知、协议存款展现证实书号,理财子账户展现为0),凭证号码(活期展现凭证号码,定期、通知、协议存款展现存期,理财子账户展现为0)业务验证码验证时,辅助检索要素请输入交易日期", font));
document.add(new Paragraph("打印日期:2021/1/31", font));
//5.释放资源
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("---------------ok.............");
}
效果图:
评论区