首先在pom.xml中加入下列依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>1.5.17.RELEASE</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version> 5.5.13</version>
</dependency>
下面开始生成PDF的代码
public void createPDF() {
//设置纸张大小
Document document = new Document(PageSize.A4);
try {
//设置PDF存放路径
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\DJLFTPPDF\\PDFDEMO/testTable.pdf"));
document.open();
//生成一个表格
PdfPTable table = this.createTable();
//生成一张图片
Image png = this.createImage();
//将表格添加PDF进去
document.add(table);
//将图片添加到PDF
document.add(png);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
document.close();
}
}
//生成表格方法
public static PdfPTable createTable() throws DocumentException, IOException{
PdfPTable table = new PdfPTable(2);//生成一个两列的表格
//渎系统中的字体
Font font = new Font(BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED));
PdfPCell cell;
//设置每列高度
int size = 50;
//按顺序向表格中添加值(输入汉字要加font)
cell = new PdfPCell(new Phrase("董津林你真帅",font));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Phrase("two"));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Phrase("three"));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Phrase("four"));
cell.setFixedHeight(size);
table.addCell(cell);
//最后一行给合并起来
cell = new PdfPCell(new Phrase("five"));
cell.setColspan(2);//设置所占列数
cell.setFixedHeight(size*2);//设置高度
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
table.addCell(cell);
return table;
}
public Image createImage() throws Exception{
//寻找图片路径
Image png = Image.getInstance("D:\\dongjinlin\\360wallpaper.jpg");
//图片大小
png.scaleToFit(500,500);
//图片位置
png.setAbsolutePosition(50,300);
return png;
}
生成效果