pom文件引入
<!-- pdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.11</version>
</dependency>
<!-- 支持中文 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
public static void pdfUtils() throws DocumentException, IOException {
//创建文档对象
Document document = new Document();
FileOutputStream fileOutputStream = new FileOutputStream("E://巡检报告.pdf");
//设置输出流
PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);
//打开文档
document.open();
//使用iTextAsian.jar中的字体、并设置编码
BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font font20 = new Font(font, 20,Font.NORMAL);
Font font21 = new Font(font, 24,Font.NORMAL);
Font font18 = new Font(font, 18,Font.NORMAL);
Font font14 = new Font(font, 14,Font.NORMAL);
Font font12 = new Font(font, 12,Font.NORMAL);
Font font14Under = new Font(font, 14, Font.UNDERLINE);
//空行
Paragraph b1 = new Paragraph("\n", font14);
//向文档中添加内容
//标题
document.add(b1); document.add(b1); document.add(b1); document.add(b1); document.add(b1);
Paragraph paragraphs = new Paragraph("建 筑 消 防 设 施 巡 查", font20);
paragraphs.setAlignment(Element.ALIGN_CENTER);
document.add(paragraphs);
document.add(b1);
document.add(b1);
document.add(b1);
document.add(b1);
Paragraph paragraph1= new Paragraph("报 告 书", font21);
paragraph1.setAlignment(Element.ALIGN_CENTER);
paragraph1.setLeading(55);
document.add(paragraph1);
document.add(b1);
document.add(b1);
document.add(b1);
document.add(b1);
//下划线
Paragraph a1 = new Paragraph("任 务 计 划:日常巡检",font14Under);
a1.setAlignment(Paragraph.ALIGN_LEFT);
a1.setSpacingAfter(50);
document.add(a1);
document.add(b1);
Paragraph a2 = new Paragraph("任 务 名 称:日常巡检星期二__星期四",font14Under);
a2.setAlignment(Paragraph.ALIGN_LEFT);
a2.setSpacingAfter(50);
document.add(a2);
document.add(b1);
Paragraph a3 = new Paragraph("任 务 日 期:2023-09-19",font14Under);
a3.setAlignment(Paragraph.ALIGN_LEFT);
a3.setSpacingAfter(50);
document.add(a3);
document.add(b1);
Paragraph a4 = new Paragraph("报 告 日 期:2023年09月22日 11:29:12",font14Under);
a4.setAlignment(Paragraph.ALIGN_LEFT);
a4.setSpacingAfter(50);
document.add(a4);
//添加Page,第二页
document.newPage();
document.add(new Paragraph("第二页 样式测试",font14));
//实线
Paragraph p1 = new Paragraph("左",font14);
p1.add(new Chunk(new LineSeparator()));
p1.add("右");
document.add(p1);
//下划线
Paragraph p3 = new Paragraph("潇洒来去山水间,谈笑声中江湖远; 码到功成人未老,白发归来仍少年....潇洒来去山水间,谈笑声中江湖远; 码到功成人未老,白发归来仍少年....",font14Under);
document.add(p3);
//第三页
document.newPage();
document.add(new Paragraph("第三页 表格测试",font14));
document.add(new Paragraph("\n",font14));
//创建表格
PdfPTable table = new PdfPTable(6);
table.setWidthPercentage(100);
table.setTotalWidth(500);
float[] widths = {20, 15, 15, 15, 15, 20};//百分比
table.setWidths(widths);
table.setLockedWidth(true);
table.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);//表格整体 水平居左
//设置标题
PdfPCell cell = new PdfPCell();
cell.setColspan(6);//必须设置跨列
cell.setMinimumHeight(30);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);//水平居中
cell.setUseAscender(true);//垂直居中
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//垂直居中
Paragraph paragraph = new Paragraph("考试成绩单", font14);
cell.setPhrase(paragraph);
table.addCell(cell);
//第一列
cell = new PdfPCell();
cell.setColspan(1);//设置跨列,所有列数总和必须是行列数的整数倍
cell.setMinimumHeight(50);
paragraph=new Paragraph(" 科目\n\n 姓名 成绩",font14);
cell.setPhrase(paragraph);
table.addCell(cell);
//单元格画斜线
PdfContentByte cb = writer.getDirectContent();
float xFrom=40;
float yFrom=document.getPageSize().getHeight()-112;
float xTo=100;
float yTo=yFrom - 50;//加行高
drawLine(cb, xFrom, yFrom, xTo, yTo);
xTo=xFrom + 500*widths[0]/100;//加第一列宽
yTo=yTo+20;
drawLine(cb, xFrom,yFrom, xTo, yTo);
//第二列
cell = new PdfPCell();
cell.setColspan(1);
cell.setMinimumHeight(50);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);//水平居中
cell.setUseAscender(true);//垂直居中
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//垂直居中
paragraph=new Paragraph("语文",font14);
cell.setPhrase(paragraph);
table.addCell(cell);
//第三列
cell = new PdfPCell();
cell.setColspan(1);
cell.setMinimumHeight(50);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);//水平居中
cell.setUseAscender(true);//垂直居中
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//垂直居中
paragraph=new Paragraph("数学",font14);
cell.setPhrase(paragraph);
table.addCell(cell);
//第四列
cell = new PdfPCell();
cell.setColspan(1);
cell.setMinimumHeight(50);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);//水平居中
cell.setUseAscender(true);//垂直居中
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//垂直居中
paragraph=new Paragraph("英语",font14);
cell.setPhrase(paragraph);
table.addCell(cell);
//第五列
cell = new PdfPCell();
cell.setColspan(1);
cell.setMinimumHeight(50);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);//水平居中
cell.setUseAscender(true);//垂直居中
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//垂直居中
paragraph=new Paragraph("综合",font14);
cell.setPhrase(paragraph);
table.addCell(cell);
//第六列
cell = new PdfPCell();
cell.setColspan(1);
cell.setMinimumHeight(50);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);//水平居中
cell.setUseAscender(true);//垂直居中
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//垂直居中
paragraph=new Paragraph("合计",font14);
cell.setPhrase(paragraph);
table.addCell(cell);
//加数据
addCellData(table, "张三", 30);
addCellData(table, "130", 30);
addCellData(table, "130", 30);
addCellData(table, "130", 30);
addCellData(table, "300", 30);
addCellData(table, "690", 30);
document.add(table);
document.close();
}
/**
* 表格划线设置表头
* @param cb
* @param xFrom
* @param yFrom
* @param xTo
* @param yTo
*/
public static void drawLine(PdfContentByte cb, float xFrom, float yFrom, float xTo, float yTo) {
cb.saveState();
cb.moveTo(xFrom,yFrom);//起点
cb.lineTo(xTo,yTo);//终点
cb.stroke();
cb.restoreState();
}
/**
* 添加表格数据
* @param table
* @param cont
* @param i
*/
private static void addCellData(PdfPTable table, String cont, int i) throws DocumentException, IOException {
PdfPCell cell1 = new PdfPCell();
cell1.setColspan(1);
cell1.setMinimumHeight(i);
cell1.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);//水平居中
cell1.setUseAscender(true);//垂直居中
cell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//垂直居中
BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font font14 = new Font(font, 14,Font.NORMAL);
Paragraph paragraph1 = new Paragraph(cont, font14);
cell1.setPhrase(paragraph1);
table.addCell(cell1);
}
public static void main(String[] args) throws DocumentException, IOException{
pdfUtils();
}