依赖:
<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();
}
效果:
加上图片之后:
有兴趣的读者可自行美化