Java中创建pdf文件并在pdf文件中创建表格
try {
//生产单文件夹名称
File dir = new File(basePath+FILE_PATH);
if(!dir.exists()){
dir.mkdirs();
}
String fileName = FILE_PATH + UUID.randomUUID().toString().replace("-", "")+".pdf";
//页面大小:纸张类型,页边距
Document document = new Document(PageSize.A4, 5, 5, -10, -10);
//建立书写器
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(basePath+fileName));
//设置标题字体样式
BaseFont bfChinese = BaseFont.createFont(fontPath + "simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//打开文档
document.open();
Font titleFont = new Font(bfChinese, 40, Font.NORMAL);
Paragraph docTitle = new Paragraph("生产单", titleFont);
docTitle.setAlignment(Element.ALIGN_CENTER);//对齐方式
docTitle.setSpacingAfter(-10);
document.add(docTitle);
Font titleFont2 = new Font(bfChinese, 15, Font.NORMAL);
Paragraph title2 = new Paragraph("单号:" + production.getProductionManageNo() + " 发货时间:" + production.getExpectDeliveryTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 制表时间:" + LocalDate.now(), titleFont2);
title2.setAlignment(Element.ALIGN_LEFT);//对齐方式
//title2.setIndentationLeft(40);
title2.setSpacingBefore(20);
title2.setSpacingAfter(-10);
document.add(title2);
Font font2 = new Font(bfChinese, 28, Font.NORMAL);
Table table = createProductionTable(production,font2,bfChinese);
document.add(table);
// 关闭文档
document.close();
writer.close();
return fileName;
} catch (IOException | DocumentException e) {
throw new OperationException(e.getMessage());
}
}
/**
* 创建表格
*
*/
public Table createProductionTable(ProductionManage production,Font font,BaseFont bfChinese) throws DocumentException {
List<ProductionManageDetails> detailsList = productionManageDetailsService.list(new LambdaQueryWrapper<ProductionManageDetails>().eq(ProductionManageDetails::getManageId, production.getId()));
//创建一个有4列的表格
Table table = new Table(4);
int width[] = {40, 19, 19, 22};
table.setWidths(width);//设置每列所占比例
table.setWidth(100);//占页面宽度 96%
table.setBorderWidth(0);//边框宽度
table.setBorderColor(new Color(0, 0, 0));//边框颜色
table.setPadding(2);//行距
table.setSpacing(0);//单元格间距
// 创建表头
Cell headCell2 = setCellContent("SKU", font);
Cell headCell3 = setCellContent("货号", font);
Cell headCell4 = setCellContent("颜色", font);
Cell headCell5 = setCellContent("总数量", font);
table.addCell(headCell2);
table.addCell(headCell3);
table.addCell(headCell4);
table.addCell(headCell5);
// 添加此代码后每页都会显示表头
table.endHeaders();
font = new Font(bfChinese, 20, Font.NORMAL);
for (int i = 0; i < detailsList.size(); i++) {
//Cell cell1 = setCellContent(String.valueOf(i + 1), font);
Cell cell2 = setCellContent(detailsList.get(i).getWarehouseSku(), font);
Cell cell3 = setCellContent(detailsList.get(i).getSupplierItemNo(), font);
Cell cell4 = setCellContent(production.getColor(), font);
Cell cell5 = setCellContent(String.valueOf(detailsList.get(i).getBuyAmount()), font);
//table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
}
return table;
}
Itext基础知识和PDF解析操作
https://www.cnblogs.com/mlgm/articles/9324370.html