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();
}效果

- 加上图片之后:

有兴趣的读者可自行美化
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论