引入依赖:
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.7</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.6</version>
</dependency>
代码:
public void doPrintInput(HttpServletRequest request, HttpServletResponse response, List<ItemAddition> list) {
/**
* 创建Document文档对象(页面的大小为A4,左、右、上、下的页边距)
* rotate():pdf横向显示
*/
Document document = new Document(PageSize.A4.rotate(), 20, 20, 10, 10);
try {
FileOutputStream fileOutputStream = new FileOutputStream("HelloWorld.pdf");
response.setContentType("application/pdf;charset=UTF8");
//比较关键的是Content-Disposition是inline而不是attachment,这样提示浏览器来显示文档而不是下载
response.setHeader("Content-Disposition", "inline;fileName=文档.pdf");
//如果是下载而不是预览的话,将下边的注释打开即可(把上边的注释掉)
//response.setHeader("Content-Disposition", "attachment;filename=aaa.pdf");
//PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
String emptyTemplateUrl = "resources/common/font/SIMYOU.TTF";
BaseFont bfChinese = BaseFont.createFont(basePath + emptyTemplateUrl, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bfChinese, 12, Font.NORMAL);
HeaderFooter footer = new HeaderFooter(new Phrase("-", font), new Phrase("-", font));
footer.setBorder(Rectangle.UNDEFINED);
/**
* 设置页码的显示位置
* 0是靠左
* 1是居中
* 2是居右
*/
footer.setAlignment(1);
document.setFooter(footer);
document.open();
//设置表格的标题
Paragraph title = new Paragraph("事业单位收费目录清单", new Font(bfChinese, 15));
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
PdfPTable table = new PdfPTable(7); // 7 列
//设置行数
table.setWidthPercentage(100); // Width 100%
table.setSpacingBefore(10f); // Space before table
table.setSpacingAfter(10f); // Space after table
// 设置单元格宽度
float[] columnWidths = {
0.3f, 0.8f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f};
table.setWidths(columnWidths);
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
cell1.setBorderColor(Color.BLACK);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setPhrase(new Paragraph("序号", font));
//cell1.setRowspan(2); 设置一个单元格占几行
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
cell2.setBorderColor(Color.BLACK);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell2.setPhrase(new Paragraph("收费项目代码", font));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
cell3.setBorderColor(Color.BLACK);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setPhrase(new Paragraph("收费项目名称", font));
PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 4"));
cell4.setBorderColor(Color.BLACK);
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setPhrase(new Paragraph("收费标准", font));
PdfPCell cell5 = new PdfPCell(new Paragraph("Cell 5"));
cell5.setBorderColor(Color.BLACK);
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setPhrase(new Paragraph("标准确定方式", font));
PdfPCell cell6 = new PdfPCell(new Paragraph("Cell 6"));
cell6.setBorderColor(Color.BLACK);
cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell6.setPhrase(new Paragraph("政策依据", font));
PdfPCell cell7 = new PdfPCell(new Paragraph("Cell 7"));
cell7.setBorderColor(Color.BLACK);
cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
cell7.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell7.setPhrase(new Paragraph("资金管理方式", font));
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
//循环获取数据并添加导表格里
for (int i = 0; i < list.size(); i++) {
//content.get(i).get
String num = String.valueOf(i + 1);
//序号
PdfPCell cell11 = new PdfPCell(new Paragraph("Cell 11" + num));
//收费项目代码
PdfPCell cell22 = new PdfPCell(new Paragraph("Cell 22" + num));
//收费项目名称
PdfPCell cell33 = new PdfPCell(new Paragraph("Cell 33" + num));
//收费标准
PdfPCell cell44 = new PdfPCell(new Paragraph("Cell 44" + num));
//标准确定方式
PdfPCell cell55 = new PdfPCell(new Paragraph("Cell 55" + num));
//政策依据
PdfPCell cell66 = new PdfPCell(new Paragraph("Cell 66" + num));
//资金管理方式
PdfPCell cell77 = new PdfPCell(new Paragraph("Cell 77" + num));
cell11.setBorderColor(Color.BLACK);
cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell11.setPhrase(new Paragraph(num, font));
cell22.setBorderColor(Color.BLACK);
cell22.setHorizontalAlignment(Element.ALIGN_CENTER);
cell22.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell22.setPhrase(new Paragraph(list.get(i).getFitemCode(), font));
cell33.setBorderColor(Color.BLACK);
cell33.setHorizontalAlignment(Element.ALIGN_CENTER);
cell33.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell33.setPhrase(new Paragraph(list.get(i).getFitemName(), font));
cell44.setBorderColor(Color.BLACK);
cell44.setHorizontalAlignment(Element.ALIGN_CENTER);
cell44.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell44.setPhrase(new Paragraph(list.get(i).getFchargeStd(), font));
cell55.setBorderColor(Color.BLACK);
cell55.setHorizontalAlignment(Element.ALIGN_CENTER);
cell55.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell55.setPhrase(new Paragraph(list.get(i).getFstdConfirmWayName(), font));
cell66.setBorderColor(Color.BLACK);
cell66.setHorizontalAlignment(Element.ALIGN_CENTER);
cell66.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell66.setPhrase(new Paragraph(list.get(i).getPolicys(), font));
cell77.setBorderColor(Color.BLACK);
// cell77.setPaddingLeft(10);
cell77.setHorizontalAlignment(Element.ALIGN_CENTER);
cell77.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell77.setPhrase(new Paragraph(list.get(i).getFfundManageWayName(), font));
table.addCell(cell11);
table.addCell(cell22);
table.addCell(cell33);
table.addCell(cell44);
table.addCell(cell55);
table.addCell(cell66);
table.addCell(cell77);
}
document.add(table);
document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}