导出效果
引入的maven
<!-- 实际工具 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.1</version>
</dependency>
<!-- 汉化字体 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
本地导出java代码
//全局表格高度
private static final float TABLE_HEIGHT=30;
String FILE_DIR = "D:\\work\\2019\\idea\\company\\zq\\export\\";
@Test
public void test7(){
//页面大小
Rectangle rect = new Rectangle(PageSize.A4);
Document document = new Document(rect);
PdfWriter writer;
try(FileOutputStream fileOutputStream = new FileOutputStream(FILE_DIR + "createSamplePDF7.pdf")){
writer = PdfWriter.getInstance(document,fileOutputStream);
//代码设置字体(这样才能让中文显示)
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 设置字体的不同样式
// 普通字体
Font normalFont = new Font(bfChinese, 14, Font.NORMAL, BaseColor.BLACK);
// 黑色粗体
Font BlodFont = new Font(bfChinese, 14, Font.BOLD, BaseColor.BLACK);
// 标题
Font hBlodFont = new Font(bfChinese, 30, Font.BOLD, BaseColor.BLACK);
// 打开新的一页
document.open();
// 标题
Paragraph paragraphRemark = new Paragraph();
Chunk id = new Chunk("itext导出pdf文字段落和表格的模板", hBlodFont);
// 文字背景色
id.setBackground(BaseColor.LIGHT_GRAY, 50f, 15f, 50f, 15f);
// 居中
paragraphRemark.setAlignment(Element.ALIGN_CENTER);
paragraphRemark.add(id);
// 添加段落
document.add(paragraphRemark);
// 换行
document.add(Chunk.NEWLINE);
document.add(Chunk.NEWLINE);
Paragraph paragraphRemark0 = new Paragraph();
Font remarkFont0 = new Font(bfChinese, 20, Font.NORMAL);
paragraphRemark0.add(new Chunk("副标题" , BlodFont));
// 居中
paragraphRemark0.setAlignment(Element.ALIGN_CENTER);
// 副标题 日期事件
document.add(paragraphRemark0);
document.add(Chunk.NEWLINE);
Paragraph paragraphRemark1 = new Paragraph();
// 缩进
paragraphRemark1.setIndentationLeft(50);
// 标题一
paragraphRemark1.add(new Chunk("标题一:", BlodFont));
paragraphRemark1.add(new Chunk("不要温和地走进那良夜,老年应当在日暮时燃烧咆哮;咆哮吧咆哮,痛斥那光的退缩。", normalFont));
paragraphRemark1.add(Chunk.NEWLINE);
paragraphRemark1.add(Chunk.NEWLINE);
document.add(paragraphRemark1);
// 标题二
Paragraph paragraphRemark2 = new Paragraph();
paragraphRemark2.add(new Chunk("标题二:", BlodFont));
paragraphRemark2.add(new Chunk("这是段文字用于测试内容", normalFont));
paragraphRemark2.add(Chunk.NEWLINE);
paragraphRemark2.add(Chunk.NEWLINE);
// 缩进
paragraphRemark2.setIndentationLeft(50);
document.add(paragraphRemark2);
// 换行
document.add(Chunk.NEWLINE);
document.add(Chunk.NEWLINE);
// 表格
PdfPTable table = new PdfPTable(3); // 列数
table.addCell(new Phrase("销售片区",BlodFont));
table.addCell(new Phrase("片区负责人",BlodFont));
table.addCell(new Phrase("任务栋数",BlodFont));
PdfPCell cell;
table.addCell(new Phrase("第一片区",normalFont));
table.addCell(new Phrase("张三",normalFont));
// 合并单元格: 行
cell = new PdfPCell(new Phrase("31",normalFont));
cell.setRowspan(2);
table.addCell(cell);
// 最后一行
cell = new PdfPCell(new Phrase("总计",normalFont));
// 合并单元格: 列
cell.setColspan(2);
table.addCell(cell);
document.add(table);
// 打开文件
Desktop.getDesktop().open(new File(FILE_DIR + "createSamplePDF7.pdf"));
}catch (Exception e){
e.printStackTrace();
}finally {
document.close();
}
}
/**
* @Author GuoZG
* @Description 设置样式
* @Date 17:16 2019/7/30
* @Param [pdfPCell]
* @return com.itextpdf.text.pdf.PdfPCell
**/
public static PdfPCell putStyle(PdfPCell pdfPCell){
//设置字体居中
pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfPCell.setVerticalAlignment(Element.ALIGN_CENTER);
pdfPCell.setFixedHeight(TABLE_HEIGHT);
return pdfPCell;
}
web端导出java代码
实际就构建流和输出的方式有些不同
在最开始创建的时候, 使用 response
最后输出的时候, 调整下返回头参数
// 开头
// 这里使用 response.getOutputStream() 代替 上面的自己构建的输出流
OutputStream out = response.getOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, out );
// 中间document构建pdf文件过程省略
...
// 最后的
String fileName = LocalDate.now().toString() + "_导出pdf文件测试.pdf";
response.setContentType("application/pdf; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + EncodeUtils.urlEncode(fileName));
document.close();
writer.close();
out.flush();
response.flushBuffer();
out.close();