记录一下:项目中遇到一个需求,根据读取到数据生成一个符合模板要求的pdf文件,以下是自己开发中的一些测试代码
Document document = new Document(PageSize.A4);
//pdfSavePath是pdf文件生成的保存路径
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfSavePath));
//固定话术
String str = "无人机室内巡检" + "\n" + "任务类型:%s" + " " +
"巡检区域:%S" + "\n" + "开放时间:%s" + " " + "结束时间:%s";
String routeTask = "本次任务巡检点位%d个,告警点位%d个,正常点位%d个";
// 内容字体
BaseFont bfComic = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//支持中文
Font font = new Font(bfComic, 16, Font.NORMAL);//文字字体类型、大小设置
// 创建表格
PdfPTable table = new PdfPTable(8);
// 设置表格宽度百分比
table.setWidthPercentage(100);
// 设置表格列宽度
float[] columnWidths = {0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 1.8f, 1.8f};
table.setWidths(columnWidths);
// 添加表格标题
//单元格已合并,并居中
PdfPCell cell = new PdfPCell(new Paragraph(str, font));
cell.setColspan(8);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell cell2 = new PdfPCell(new Paragraph(routeTask, font2));
cell2.setColspan(8);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Paragraph(FileTypeCode.POINT_STATION, font2));
cell3.setColspan(8);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell3);
// 添加
cellFillChar(table, FileTypeCode.TABLE_ONE, font2);
cellFillChar(table, FileTypeCode.TABLE_TWO, font2);
cellFillChar(table, FileTypeCode.TABLE_THREE, font2);
cellFillChar(table, FileTypeCode.TABLE_FOUR, font2);
cellFillChar(table, FileTypeCode.TABLE_FIVE, font2);
cellFillChar(table, FileTypeCode.TABLE_SIX, font2);
cellFillChar(table, FileTypeCode.TABLE_SEVEN, font2);
cellFillImage(table,"C:\\Users\\administered\\Desktop\\20230621084053.jpg");
document.add(table);
document.close();
System.out.println("PDF created successfully.");
代码中封装的一些常量,如下
static final String POINT_STATION = "告警点位";
static final String TABLE_ONE = "序号";
static final String TABLE_TWO = "巡检点";
static final String TABLE_THREE = "位置";
static final String TABLE_FOUR = "巡检项";
static final String TABLE_FIVE= "识别结果";
static final String TABLE_SIX = "识别时间";
static final String TABLE_SEVEN = "正常图片";
static final String TABLE_EIGHT = "巡检图片";
代码中封的方法,如下
/**
* 填充数据
* @param table 表
* @param str 填充的数据
* @param font 字体设置
*/
private void cellFillChar(PdfPTable table,String str,Font font){
//如果str为空,不会报错,这个单元格为空
PdfPCell cell = new PdfPCell(new Paragraph(str,font));
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
}
/**
* 图片填充方法
* @param table 表
* @param savePath 图片存在路径
*/
private void cellFillImage(PdfPTable table,String path){
// 创建图片对象
Image image = null;
try {
image = Image.getInstance(path);
if (image != null){
image.scaleToFit(135f, 100f);
// 创建包含图片的单元格
PdfPCell cellWithImage = new PdfPCell();
cellWithImage.addElement(image);
table.addCell(cellWithImage);
}
} catch (BadElementException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
生成的pdf文件如下图显示
使用的依赖是
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>