本次参考的文章:
https://www.cnblogs.com/qlqwjy/p/8213989.html、
https://blog.csdn.net/darkjazz11/article/details/79647252
iText7版本的官方API文档:https://api.itextpdf.com/iText7/7.0.3/
一、使用Maven管理所需的jar包
<properties>
<itext.version>RELEASE</itext.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.0.3</version>
</dependency>
</dependencies>
</project>
二、编写函数导出pdf
//1、创建流对象
PdfWriter pdfWriter=new PdfWriter(new File("pdf样品.pdf"));
//2、创建文档对象
PdfDocument pdfDocument=new PdfDocument(pdfWriter);
//3、创建内容文档对象
Document document=new Document(pdfDocument, PageSize.A4);
PdfFont font= PdfFontFactory.createFont("STSongStd-Light","UniGB-UCS2-H",true);
Table table=new Table(new float[]{1,3,1,1,1});
table.setWidthPercent(100);
//创建表头
Cell head=new Cell(1,5); //一行四列
Paragraph paragraph = new Paragraph("2018年第一季度键教科考核结果");
paragraph.setTextAlignment(TextAlignment.CENTER).setFont(font).setBold();
head.add(paragraph).setTextAlignment(TextAlignment.CENTER).setBorder(Border.NO_BORDER);
//加入表格
table.addHeaderCell(head);
//表格第一行
Cell cell1_1=new Cell();
cell1_1.add(new Paragraph("督导项目").setTextAlignment(TextAlignment.CENTER).setFont(font));
Cell cell1_2=new Cell();
cell1_2.add(new Paragraph("存在问题/奖惩/亮点").setTextAlignment(TextAlignment.CENTER).setFont(font));
Cell cell1_3=new Cell();
cell1_3.add(new Paragraph("扣分").setTextAlignment(TextAlignment.CENTER).setFont(font));
Cell cell1_4=new Cell();
cell1_4.add(new Paragraph("奖励(元)").setTextAlignment(TextAlignment.CENTER).setFont(font));
Cell cell1_5=new Cell();
cell1_5.add(new Paragraph("处罚(元)").setTextAlignment(TextAlignment.CENTER).setFont(font));
table.addCell(cell1_1);
table.addCell(cell1_2);
table.addCell(cell1_3);
table.addCell(cell1_4);
table.addCell(cell1_5);
//表格第二行
Cell cell2_1=new Cell();
cell2_1.add(new Paragraph("安全生产管理").setTextAlignment(TextAlignment.CENTER).setFont(font));
Cell cell2_2=new Cell();
cell2_2.add(new Paragraph("成立科室消防管理小组").setTextAlignment(TextAlignment.CENTER).setFont(font));
Cell cell2_3=new Cell();
cell2_3.add(new Paragraph("-").setTextAlignment(TextAlignment.CENTER).setFont(font));
Cell cell2_4=new Cell();
cell2_4.add(new Paragraph("200").setTextAlignment(TextAlignment.CENTER).setFont(font));
Cell cell2_5=new Cell();
cell2_5.add(new Paragraph("-").setTextAlignment(TextAlignment.CENTER).setFont(font));
table.addCell(cell2_1);
table.addCell(cell2_2);
table.addCell(cell2_3);
table.addCell(cell2_4);
table.addCell(cell2_5);
//第三行
Cell cell3_1=new Cell(1,2);
cell3_1.add(new Paragraph("合计(扣分、奖励、处罚)").setTextAlignment(TextAlignment.CENTER).setFont(font));
Cell cell3_2=new Cell();
cell3_2.add(new Paragraph("-").setTextAlignment(TextAlignment.CENTER).setFont(font));
Cell cell3_3=new Cell();
cell3_3.add(new Paragraph("200").setTextAlignment(TextAlignment.CENTER).setFont(font));
Cell cell3_4=new Cell();
cell3_4.add(new Paragraph("-").setTextAlignment(TextAlignment.CENTER).setFont(font));
table.addCell(cell3_1);
table.addCell(cell3_2);
table.addCell(cell3_3);
table.addCell(cell3_4);
//第四行
Cell cell4_1=new Cell(1,2);
cell4_1.add(new Paragraph("合计(总分)").setTextAlignment(TextAlignment.CENTER).setFont(font));
Cell cell4_2=new Cell(1,3);
cell4_2.add(new Paragraph("100").setTextAlignment(TextAlignment.CENTER).setFont(font));
table.addCell(cell4_1);
table.addCell(cell4_2);
document.add(table);
document.close();
三、结果如下