Java 动态生成PDF表格:
相关内容可根据需求自己修改
引入itextpdf依赖:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
代码实现:
/**
* 生成结算单pdf
*
* @param folderName 生成文件的文件夹名称
* @param fileName 生成文件的全路径文件名
* @param titleName 文件内容标题
* @param contentName 文件内容
* @param data 文件表格数据
* @return
*/
public static File generatePdf(String folderName, String fileName, String titleName, String contentName, JSONObject data) {
try {
//页面大小
Rectangle rect = new Rectangle(PageSize.A4);
//创建文档对象
Document document = new Document(rect, 60, 60, 30, 30);
File folderFile = new File(folderName);
if (!folderFile.exists()) {
log.info("准备创建文件夹. ");
folderFile.mkdirs();// 如果不存在,创建目录
log.info("成功创建文件夹. ");
}
//设置输出流
PdfWriter.getInstance(document, new FileOutputStream(fileName));
document.open();
// 本地调试用这个字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontTitle = new Font(bfChinese, 18, Font.BOLD, BaseColor.BLACK);
Font fontContent = new Font(bfChinese, 10, Font.NORMAL, BaseColor.BLACK);
Paragraph title = new Paragraph(titleName, fontTitle);
//居中
title.setAlignment(1);
document.add(title);
// 空一行
Paragraph emptyRow = new Paragraph(10f, " ", fontContent);
document.add(emptyRow);
Paragraph content = new Paragraph(contentName, fontContent);
document.add(content);
// 空一行
document.add(emptyRow);
int columnNum = 5;
PdfPTable headerTable = new PdfPTable(columnNum);
headerTable.setWidthPercentage(100);
int headerwidths[] = {20, 20, 20, 20, 20};
headerTable.setWidths(headerwidths);
// 构建表格头 (根据需求修改,也可作为入参传进来)
List<String> headList = new ArrayList<>();
headList.add("提货日期");
headList.add("货物规格");
headList.add("提货吨数");
headList.add("单价");
headList.add("实际货值");
for (int i = 0; i < headList.size(); i++) {
createTableCell(headList.get(i), fontContent, headerTable);
}
JSONArray centerStatementsItemVOS = data.getJSONArray("centerStatementsItemVOS");
List<String> contentList = new ArrayList<>();
contentList.add("pickupDate");
contentList.add("productSpecs");
contentList.add("weight");
contentList.add("taxedStatementsPrice");
contentList.add("sumTaxedStatementsPrice");
// 外层循环构建行,内层循环构建列
for (int i = 0; i < centerStatementsItemVOS.size(); i++) {
for (int j = 0; j < contentList.size(); j++) {
createTableCell(centerStatementsItemVOS.getJSONObject(i).getString(contentList.get(j)), fontContent, headerTable);
}
}
// 构建表格尾(根据需求修改,也可作为入参传进来)
List<String> endList = new ArrayList<>();
endList.add("合计");
endList.add(" ");
endList.add(data.getString("totalWeight"));
endList.add(" ");
endList.add(data.getString("totalSumTaxedStatementsPrice"));
for (int i = 0; i < endList.size(); i++) {
createTableCell(endList.get(i), fontContent, headerTable);
}
document.add(headerTable);
document.close();
File file = new File(fileName);
return file;
} catch (DocumentException e) {
throw new RuntimeException(e);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 创建表格单元格
*
* @param content 内容
* @param fontContent 字体
* @param headerTable 表格对象
*/
private static void createTableCell(String content, Font fontContent, PdfPTable headerTable) {
Phrase phrase = new Phrase(content, fontContent);
PdfPCell pdfPCell = new PdfPCell(phrase);
pdfPCell.setFixedHeight(18);
pdfPCell.setHorizontalAlignment(1);
pdfPCell.setVerticalAlignment(1);
headerTable.addCell(pdfPCell);
}