生成這樣的PDF
?
直接上代碼
public static void main(String[] args) {String logoPath = "Q:\\IdeaWork\\Demo\\src\\main\\webapp\\images\\logo.jpg"; // 替換為實際路徑String baseDir = "E:/Demo/TEST/problem/Generate"; // 基礎目錄int year = 2025; // 動態年份String issueId = "100002"; // 動態問題ID// 生成完整路徑(自動處理斜杠)String outputDir = Paths.get(baseDir, String.valueOf(year), issueId).toString();String outputFilePath = Paths.get(outputDir, "重大事件通報.pdf").toString();try {// 確保目錄存在,不存在則創建Path dirPath = Paths.get(outputDir);if (!Files.exists(dirPath)) {Files.createDirectories(dirPath);System.out.println("目錄創建成功: " + dirPath);}// 生成PDFgeneratePdf(logoPath, outputFilePath);System.out.println("PDF生成成功: " + outputFilePath);} catch (Exception e) {e.printStackTrace();}}
public static void generatePdf(String logoPath, String outputPath)throws DocumentException, IOException {// 其余代碼保持不變(和之前一樣)Document document = new Document(PageSize.A4, 50, 50, 50, 50);PdfWriter.getInstance(document, new FileOutputStream(outputPath));document.open();// 設置中文字體BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);Font titleFont = new Font(bfChinese, 16, Font.BOLD);Font headerFont = new Font(bfChinese, 12, Font.BOLD);Font normalFont = new Font(bfChinese, 10, Font.NORMAL);Font underlineFont = new Font(bfChinese, 16, Font.BOLD);//underlineFont.setStyle(Font.UNDERLINE);// 添加logotry {Image logo = Image.getInstance(logoPath);logo.scaleToFit(80f, 40f); // 調整logo大小logo.setAlignment(Element.ALIGN_CENTER);document.add(logo);} catch (Exception e) {System.out.println("無法加載logo圖片: " + e.getMessage());}// 添加標題Paragraph title = new Paragraph("重 大 事 件 通 報", underlineFont);title.setAlignment(Element.ALIGN_CENTER);title.setSpacingAfter(20f);document.add(title);// 添加基本信息表格// 創建表格(2列)PdfPTable infoTable = new PdfPTable(2);infoTable.setWidthPercentage(80); // 縮小表格總寬度infoTable.setHorizontalAlignment(Element.ALIGN_LEFT); // 整體左對齊// 設置列寬(左邊固定30mm,右邊自適應)infoTable.setWidths(new float[]{30, 70}); // 單位:毫米// 添加表格內容(":"后加空格)addTableCell(infoTable, "操作:", "王峰", headerFont, normalFont);addTableCell(infoTable, "職位:", "考核師", headerFont, normalFont);addTableCell(infoTable, "被通報單位名稱:", "XXX公司", headerFont, normalFont);addTableCell(infoTable, "日期:", "2025年03月07日", headerFont, normalFont);document.add(infoTable);// 添加稱呼Paragraph greeting = new Paragraph("尊敬的XXX合作伙伴:", normalFont);greeting.setSpacingAfter(10f);document.add(greeting);Paragraph content = new Paragraph("為確保雙方合作的順利進行,現就以下關鍵事項進行通知,請您知悉并按要求執行。", normalFont);content.setSpacingAfter(20f);document.add(content);// 添加事件主題Paragraph eventTitle = new Paragraph("事件主題", headerFont);eventTitle.setSpacingAfter(5f);document.add(eventTitle);Paragraph eventContent = new Paragraph("交付異常", normalFont);eventContent.setSpacingAfter(15f);document.add(eventContent);// 添加事件說明Paragraph descriptionTitle = new Paragraph("事件說明", headerFont);descriptionTitle.setSpacingAfter(5f);document.add(descriptionTitle);Paragraph descriptionContent = new Paragraph("12月收貨異常,記錄一次性開箱不良關鍵事件", normalFont);descriptionContent.setSpacingAfter(15f);document.add(descriptionContent);// 添加關鍵得分Paragraph scoreTitle = new Paragraph("關鍵得分", headerFont);scoreTitle.setSpacingAfter(5f);document.add(scoreTitle);Paragraph scoreContent = new Paragraph("-3分", normalFont);scoreContent.setSpacingAfter(15f);document.add(scoreContent);// 添加關鍵要求Paragraph requirementTitle = new Paragraph("關鍵要求", headerFont);requirementTitle.setSpacingAfter(5f);document.add(requirementTitle);Paragraph requirementContent = new Paragraph("請供應商按我司關鍵事件通報,提交整改方案及相關文件材料", normalFont);requirementContent.setSpacingAfter(15f);document.add(requirementContent);// 添加截止時間Paragraph deadlineTitle = new Paragraph("截止時間", headerFont);deadlineTitle.setSpacingAfter(5f);document.add(deadlineTitle);Paragraph deadlineContent = new Paragraph("請于2025年03月15日前組織整改回復,謝謝", normalFont);deadlineContent.setSpacingAfter(20f);document.add(deadlineContent);// 添加聯系方式Paragraph contactTitle = new Paragraph("六、聯系方式", headerFont);contactTitle.setSpacingAfter(5f);document.add(contactTitle);Paragraph contactContent = new Paragraph();contactContent.add(new Chunk("聯系人:", headerFont));contactContent.add(new Chunk("123456 王峰 (創建人)", normalFont));contactContent.add(Chunk.NEWLINE);contactContent.add(new Chunk("郵箱:", headerFont));contactContent.add(new Chunk("chenxiaogang@xxx.com", normalFont));contactContent.add(Chunk.NEWLINE);contactContent.add(new Chunk("發通報公司:", headerFont));contactContent.add(new Chunk("具體梓公司", normalFont));contactContent.setSpacingAfter(20f);document.add(contactContent);// 添加結尾Paragraph ending = new Paragraph("感謝您的配合與支持,期待我們繼續攜手共進,實現互利共贏!此致,敬禮!", normalFont);ending.setSpacingAfter(10f);document.add(ending);Paragraph company = new Paragraph("********有限公司", normalFont);company.setAlignment(Element.ALIGN_RIGHT);company.setSpacingAfter(5f);document.add(company);Paragraph date = new Paragraph("日期:2025年03月07日", normalFont);date.setAlignment(Element.ALIGN_RIGHT);document.add(date);// 關閉文檔document.close();}
private static void addTableCell(PdfPTable table, String header, String content,Font headerFont, Font contentFont) {// 左邊單元格(標簽)PdfPCell headerCell = new PdfPCell(new Phrase(header + " ", headerFont));headerCell.setBorder(Rectangle.NO_BORDER);headerCell.setPaddingRight(5f); // 標簽右側內邊距table.addCell(headerCell);// 右邊單元格(內容)PdfPCell contentCell = new PdfPCell(new Phrase(content, contentFont));contentCell.setBorder(Rectangle.NO_BORDER);contentCell.setHorizontalAlignment(Element.ALIGN_LEFT); // 內容左對齊contentCell.setPaddingRight(15f); // 內容右側內邊距table.addCell(contentCell);}
注意事項:
-
您需要將
logoPath
變量替換為實際的logo圖片路徑,確保圖片存在且可訪問。 -
此代碼使用了iText庫,您需要在項目中添加以下依賴:
-
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.3</version> </dependency>
-
代碼中使用了中文字體"STSong-Light",這是iText自帶的中文字體。如果您需要其他字體,可以指定其他支持中文的字體文件路徑。
-
生成的PDF文件將保存在
outputPath
指定的路徑。 -
如果您在Servlet環境中使用此代碼,可以將
logoPath
設置為: -
String logoPath = session.getServletContext().getRealPath("/") + "/images/envicool.jpg";