SpringBoot集成iTextPDF的实例
更新时间:2024年09月20日 10:43:07 作者:kicinio
SpringBoot集成iTextPDF时,创建PDF文档涉及Document、PdfPTable和PdfPCell对象,设置文档大小和页边距,使用Paragraph设置段落样式,并通过Table和Cell控制表格样式和对齐,还可加入图片美化文档,这些步骤对于生成具有中文内容的PDF文件至关重要
SpringBoot集成iTextPDF
依赖
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>注意:不加下面这个依赖无法设置中文
使用步骤
- 先创建
Document对象,该对象是PDF文档,可以进行一些属性设置 PdfPTable是表格对象,用于表格对象的创建、管理及使用PdfPCell是具体的表格子项了,里面就是我们要操作写入的对象- 将
PdfPCell对象加入到PdfPTable对象中,PdfPTable对象再加入到Document对象中,便完成了文档的创建
基本属性
文档大小:
由Document对象的多个重载构造器决定。
Document(); // 默认页面大小是A4 Document(PageSize.A4); // 指定页面大小为A4 Document(PageSize.A4,50,50,30,20); // 指定页面大小为A4,且自定义页边距(marginLeft、marginRight、marginTop、marginBottom)
段落的设置:
由Paragraph的对象属性决定。
Paragraph paragraph = new Paragraph(name,headfont);//设置字体样式 paragraph.setAlignment(1);//设置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft(12);// 左缩进 paragraph.setIndentationRight(12);// 右缩进 paragraph.setFirstLineIndent(24);// 首行缩进 paragraph.setLeading(20f); //行间距 paragraph.setSpacingBefore(5f); //设置段落上空白 paragraph.setSpacingAfter(10f); //设置段落下空白
表格:
由Table的对象属性决定。
table.setAlignment(Element.ALIGN_CENTER);//居中 table.setAutoFillEmptyCells(true);//自动填满 table.setBorderWidth((float)0.1);//表格边框线条宽度 table.setPadding(1);//边距:单元格的边线与单元格内容的边距 table.setSpacing(0);//间距:单元格与单元格之间的距离
cell:
由Cell的对象属性决定。
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
代码
@RequestMapping(value = "ef/pdf")
public void pdfController() throws IOException, DocumentException {
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
response.setHeader("content-Type", "application/pdf");
// 下载文件的默认名称
response.setHeader("Content-Disposition", "attachment;filename=test.pdf");
Document document = new Document();
try {
PdfWriter.getInstance(document, response.getOutputStream());
} catch (DocumentException e) {
e.printStackTrace();
}
document.open();
document.setPageCount(2);
document.addTitle("Personal Grade Browser");// 标题
document.addAuthor("Jack.Edward");// 作者
document.addSubject("Personal Grade of Jack.Edward");// 主题
document.addKeywords("Simulator");// 关键字
document.addCreator("Kicinio");// 创建者
Image image =Image.getInstance("/xp.png");
List<String> titleList = new ArrayList<>();
titleList.add("Literature");
titleList.add("Math");
titleList.add("English");
for(int i = 0; i < 10; i++){
PdfPTable tableContent = new PdfPTable(titleList.size());
if(i == 0){
PdfPTable tableTitle = new PdfPTable(titleList.size());
PdfPCell cellOne = new PdfPCell();
cellOne.setPhrase(new Paragraph(titleList.get(0)));
cellOne.setBackgroundColor(BaseColor.LIGHT_GRAY);
cellOne.setHorizontalAlignment(Element.ALIGN_CENTER);
tableTitle.addCell(cellOne);
PdfPCell cellTwo = new PdfPCell();
cellTwo.setPhrase(new Paragraph(titleList.get(1)));
cellTwo.setBackgroundColor(BaseColor.LIGHT_GRAY);
cellTwo.setHorizontalAlignment(Element.ALIGN_CENTER);
tableTitle.addCell(cellTwo);
PdfPCell cellThree = new PdfPCell();
cellThree.setPhrase(new Paragraph(titleList.get(2)));
cellThree.setBackgroundColor(BaseColor.LIGHT_GRAY);
cellTwo.setHorizontalAlignment(Element.ALIGN_CENTER);
tableTitle.addCell(cellThree);
document.add(tableTitle);
}
Random randomGrade = new Random();
PdfPCell cell = new PdfPCell();
cell = new PdfPCell();
cell.setPhrase(new Paragraph(String.valueOf(randomGrade.nextInt(100))));
tableContent.addCell(cell);
document.add(tableContent);
cell = new PdfPCell();
cell.setPhrase(new Paragraph(String.valueOf(randomGrade.nextInt(100))));
tableContent.addCell(cell);
cell.setBorderWidth(20);
document.add(tableContent);
cell = new PdfPCell();
cell.setPhrase(new Paragraph(String.valueOf(randomGrade.nextInt(100))));
// cell.setImage(image);
tableContent.addCell(cell);
document.add(tableContent);
}
document.close();
}效果

- 加上图片之后:

有兴趣的读者可自行美化
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot利用validation实现优雅的校验参数
数据的校验是交互式网站一个不可或缺的功能,如果数据库中出现一个非法的邮箱格式,会让运维人员头疼不已。本文将介绍如何利用validation来对数据进行校验,感兴趣的可以跟随小编一起学习一下2022-06-06
JVM(Java Virtual Machine,Java虚拟机)的作用详解
JVM是Java语言实现“一次编写,到处运行”特性的基石,也是Java平台的核心组成部分,其主要作用包括平台无关性、内存管理、运行Java程序、安全性以及性能优化,通过这些功能,JVM确保了Java程序的可移植性、高效性和安全性2025-03-03
SpringBoot+Redis BitMap实现签到与统计的项目实践
最近项目里需要集成签到和统计功能,连续签到后会给用户发放一些优惠券和奖品,以此来吸引用户持续在该品台进行活跃,本文就详细的介绍一下如何实现,感兴趣的可以了解一下2023-09-09
SpringBoot在IDEA中实现热部署(JRebel实用版)
这篇文章主要介绍了SpringBoot在IDEA中实现热部署(JRebel实用版),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-05-05


最新评论