最近有个任务是动态的创建pdf根据获取到的内容,百度到的知识点都比较零散,官方文档想必大家也不容易看懂。下文是我做出的汇总``
public class CreatePdfUtils {public static void create(){//准备File file = new File("C:\\code\\base-project-backend\\pdf\\"+"1.pdf");Document document = new Document();PdfWriter.getInstance(document,new FileOutputStream(file));document.open();document.setPageSize(PageSize.A4); //这是第一页的尺寸大小document.add(new Paragraph("hello")); //这是第一页的内容Rectangle rectangle = new Rectangle(1111, 1111); //这是第二页的尺寸大小rectangle.setBackgroundColor(new BaseColor(11,11,11));//这是第二页的背景颜色document.setPageSize(rectangle); //在这里设置第二页document.newPage(); //这里是真正创建第二页document.add(new Paragraph("wordld"));//第二页增加内容//结束document.close();}
}
接下来还会继续完善
public static void createPdf(){File file = new File("C:\\code\\base-project-backend\\shiting-module-system\\shiting-module-system-biz\\src\\main\\java\\com\\shiting\\basic\\module\\system\\controller\\admin\\pdf1.pdf");Document document = new Document(PageSize.B4.rotate());try {//引入中文字体,一般从c:\\windows\\Fonts下找到BaseFont bfChinese = BaseFont.createFont("shiting-module-system/shiting-module-system-biz/src/main/java/com/shiting/basic/module/system/util/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font fontH1 = new Font(bfChinese, 25, Font.BOLD);//字体1Font fontH2 = new Font(bfChinese, 12, Font.NORMAL);//字体2PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));document.open();Paragraph p3 = new Paragraph("黄河流域历史遗留矿山调查送检样品交接表单",fontH1);p3.setAlignment(Element.ALIGN_CENTER);p3.setSpacingAfter(25);document.add(p3);PdfPTable pdfPTable = new PdfPTable(4);pdfPTable.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPTable.setWidthPercentage(100);pdfPTable.setTotalWidth(new float[]{200,200,240,240});pdfPTable.setLockedWidth(true);//开始填充表格PdfPCell pdfPCell1 = new PdfPCell(new Phrase("批次编号:" + "20231019172338", fontH2));pdfPCell1.setBorderColor(new BaseColor(255,255,255));pdfPTable.addCell(pdfPCell1);PdfPCell pdfPCell2 = new PdfPCell(new Phrase("样品总数:" + "12", fontH2));pdfPCell2.setBorderColor(new BaseColor(255,255,255));pdfPTable.addCell(pdfPCell2 );PdfPCell pdfPCell3 = new PdfPCell(new Phrase("开始采样开始时间:" + "2023-10-10 13:31:12", fontH2));pdfPCell3.setBorderColor(new BaseColor(255,255,255));pdfPTable.addCell( pdfPCell3);PdfPCell pdfPCell4 = new PdfPCell(new Phrase("收样完成时间:" + "2024-10-10 13:31:12", fontH2));pdfPCell4.setBorderColor(new BaseColor(255,255,255));pdfPTable.addCell( pdfPCell4);PdfPCell pdfPCell5 = new PdfPCell(new Phrase("固体废物样品总数:" + "10", fontH2));pdfPCell5.setBorderColor(new BaseColor(255,255,255));pdfPTable.addCell( pdfPCell5);PdfPCell pdfPCell6 = new PdfPCell(new Phrase("酸性废水样品总数:" + "1", fontH2));pdfPCell6.setBorderColor(new BaseColor(255,255,255));pdfPTable.addCell( pdfPCell6);PdfPCell pdfPCell7 = new PdfPCell(new Phrase("灌溉水总样:" + "0", fontH2));pdfPCell7.setBorderColor(new BaseColor(255,255,255));pdfPTable.addCell(pdfPCell7);PdfPCell pdfPCell8 = new PdfPCell(new Phrase("底泥样总数:" + "0", fontH2));pdfPCell8.setBorderColor(new BaseColor(255,255,255));pdfPTable.addCell(pdfPCell8);PdfPCell pdfPCell9 = new PdfPCell(new Phrase("土壤样总数:" + "0", fontH2));pdfPCell9.setBorderColor(new BaseColor(255,255,255));pdfPTable.addCell( pdfPCell9);PdfPCell pdfPCell10 = new PdfPCell(new Phrase("收样退回重新采样数量:" + "0", fontH2));pdfPCell10.setBorderColor(new BaseColor(255,255,255));pdfPTable.addCell( pdfPCell10);PdfPCell pdfPCell11 = new PdfPCell(new Phrase("承运单位:" + "", fontH2));pdfPCell11.setBorderColor(new BaseColor(255,255,255));pdfPTable.addCell(pdfPCell11);PdfPCell pdfPCell12 = new PdfPCell(new Phrase("运输方式:" + "汽车运输", fontH2));pdfPCell12.setBorderColor(new BaseColor(255,255,255));pdfPTable.addCell(pdfPCell12);pdfPTable.setSpacingAfter(10);document.add(pdfPTable);PdfPTable pdfPTable2 = new PdfPTable(10);pdfPTable2.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPTable2.setWidthPercentage(100);pdfPTable2.setTotalWidth(new float[]{20,120,70,40,95,95,95,95,50,200});pdfPTable2.setLockedWidth(true);PdfPCell pCell1 = new PdfPCell(new Phrase("序号", fontH2));pCell1.setVerticalAlignment(Element.ALIGN_CENTER);pCell1.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell(pCell1 );PdfPCell pCell2 = new PdfPCell(new Phrase("字样编号", fontH2));pCell2.setVerticalAlignment(Element.ALIGN_CENTER);pCell2.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell(pCell2 );PdfPCell pCell3 = new PdfPCell(new Phrase("样品类型", fontH2));pCell3.setVerticalAlignment(Element.ALIGN_CENTER);pCell3.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell( pCell3);PdfPCell pCell4 = new PdfPCell(new Phrase("容器与保护剂", fontH2));pCell4.setHorizontalAlignment(Element.ALIGN_CENTER);pCell4.setVerticalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell(pCell4 );PdfPCell pCell5 = new PdfPCell(new Phrase("测试项目分类", fontH2));pCell5.setVerticalAlignment(Element.ALIGN_CENTER);pCell5.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell( pCell5);PdfPCell pCell6 = new PdfPCell(new Phrase("取样时间", fontH2));pCell6.setVerticalAlignment(Element.ALIGN_CENTER);pCell6.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell( pCell6);PdfPCell pCell7 = new PdfPCell(new Phrase("收样时间", fontH2));pCell7.setHorizontalAlignment(Element.ALIGN_CENTER);pCell7.setVerticalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell(pCell7);PdfPCell pCell8 = new PdfPCell(new Phrase("有效保存时间", fontH2));pCell8.setVerticalAlignment(Element.ALIGN_CENTER);pCell8.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell( pCell8);PdfPCell pCell9 = new PdfPCell(new Phrase("是否合格样品", fontH2));pCell9.setHorizontalAlignment(Element.ALIGN_CENTER);pCell9.setVerticalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell( pCell9);PdfPCell pCell10 = new PdfPCell(new Phrase("不合格情况说明", fontH2));pCell10.setVerticalAlignment(Element.ALIGN_CENTER);pCell10.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell( pCell10);ArrayList<jiaojiebiao> jiaojiebiaos = new ArrayList<>();for (int i = 0; i <12 ; i++) {jiaojiebiao jiaojiebiao = new jiaojiebiao();jiaojiebiao.setId(String.valueOf(i));jiaojiebiao.setChildNum("522023z4G2j8Zjx"+i);jiaojiebiao.setType("酸性废水");jiaojiebiao.setCategory("酸性废水pH和重金属八项"+i);jiaojiebiao.setDate1("2023-10-10 14:12:48");jiaojiebiao.setDate2("2023-11-10 14:12:48");jiaojiebiao.setDate3("2023-12-10 14:12:48");jiaojiebiao.setPass("是");if(i==6){jiaojiebiao.setPass("否");jiaojiebiao.setDetail("这个不合格!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");}jiaojiebiaos.add(jiaojiebiao);}for (jiaojiebiao jiaojiebiao : jiaojiebiaos) {PdfPCell cell1 = new PdfPCell(new Phrase(jiaojiebiao.getId(), fontH2));cell1.setHorizontalAlignment(Element.ALIGN_CENTER);cell1.setVerticalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell(cell1);PdfPCell cell2 = new PdfPCell(new Phrase(jiaojiebiao.getChildNum(), fontH2));cell2.setHorizontalAlignment(Element.ALIGN_CENTER);cell2.setVerticalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell(cell2);PdfPCell cell3 = new PdfPCell(new Phrase(jiaojiebiao.getType(), fontH2));cell3.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell(cell3);pdfPTable2.addCell(new Phrase(jiaojiebiao.getContainer(),fontH2));pdfPTable2.addCell(new Phrase(jiaojiebiao.getCategory(),fontH2));PdfPCell cell6 = new PdfPCell(new Phrase(String.valueOf(jiaojiebiao.getDate1()), fontH2));cell6.setHorizontalAlignment(Element.ALIGN_CENTER);cell6.setVerticalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell(cell6);PdfPCell cell7 = new PdfPCell(new Phrase(String.valueOf(jiaojiebiao.getDate2()), fontH2));cell7.setHorizontalAlignment(Element.ALIGN_CENTER);cell7.setVerticalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell(cell7);PdfPCell cell8 = new PdfPCell(new Phrase(String.valueOf(jiaojiebiao.getDate3()), fontH2));cell8.setHorizontalAlignment(Element.ALIGN_CENTER);cell8.setVerticalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell(cell8);PdfPCell cell9= new PdfPCell(new Phrase(jiaojiebiao.getPass(), fontH2));cell9.setVerticalAlignment(Element.ALIGN_CENTER);cell9.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPTable2.addCell(cell9);pdfPTable2.addCell(new Phrase(jiaojiebiao.getDetail(),fontH2));}pdfPTable2.setSpacingAfter(10);document.add(pdfPTable2);PdfPTable pdfPTable3 = new PdfPTable(4);pdfPTable3.setHorizontalAlignment(Element.ALIGN_CENTER);pdfPTable3.setWidthPercentage(100);pdfPTable3.setTotalWidth(new float[]{70,470,200,140});pdfPTable3.setLockedWidth(true);PdfPCell bCell1 = new PdfPCell(new Phrase("收样单位:",fontH2));bCell1.setBorderColor(new BaseColor(255,255,255));pdfPTable3.addCell(bCell1);PdfPCell bCell2 = new PdfPCell(new Phrase("贵州绿环科技检测有限公司",fontH2));bCell2.setBorderColor(new BaseColor(255,255,255));pdfPTable3.addCell(bCell2);PdfPCell bCell3 = new PdfPCell(new Phrase("收样人(签字):",fontH2));bCell3.setBorderColor(new BaseColor(255,255,255));pdfPTable3.addCell(bCell3);PdfPCell bCell4 = new PdfPCell(new Phrase(""));bCell4.setBorderColor(new BaseColor(255,255,255));pdfPTable3.addCell(bCell4);PdfPCell bCell5 = new PdfPCell(new Phrase("送样单位:",fontH2));bCell5.setBorderColor(new BaseColor(255,255,255));pdfPTable3.addCell(bCell5);PdfPCell bCell6 = new PdfPCell(new Phrase("贵州黔大生态环境与健康研究院有限公司",fontH2));bCell6.setBorderColor(new BaseColor(255,255,255));pdfPTable3.addCell(bCell6);PdfPCell bCell7 = new PdfPCell(new Phrase("送样人(签字):",fontH2));bCell7.setBorderColor(new BaseColor(255,255,255));pdfPTable3.addCell(bCell7);PdfPCell bCell8= new PdfPCell(new Phrase(""));bCell8.setBorderColor(new BaseColor(255,255,255));pdfPTable3.addCell(bCell8);document.add(pdfPTable3);document.close();} catch (DocumentException e) {throw new RuntimeException(e);} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}
}