From ee04f808590c87c591757e7519ae13fc9f47c006 Mon Sep 17 00:00:00 2001 From: "15881625488@163.com" <15881625488@163.com> Date: Thu, 8 Jan 2026 20:11:51 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9Apdf=E5=AF=BC=E5=87=BA=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/hxhq/business/utils/PdfUtil.java | 179 ++++++++------------- 1 file changed, 68 insertions(+), 111 deletions(-) diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/PdfUtil.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/PdfUtil.java index 19fab9a..0f78956 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/PdfUtil.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/PdfUtil.java @@ -32,11 +32,11 @@ public class PdfUtil { /** * 导出form - * @param qm + * @param headerText * @param fileDir * @return */ - public static String exportForm(String qm,String fileDir) { + public static String exportForm(String headerText,String fileDir) { Document document = null; FileOutputStream fos = null; String filePath = ""; @@ -52,13 +52,21 @@ public class PdfUtil { // 3. 完整文件路径 filePath = Paths.get(fileDir, fileName).toString(); - // 4. 创建PDF文档 - document = new Document(PageSize.A4); + // 创建PDF文档 设置文档边距,避免内容遮挡页眉页脚 + float topMargin = 70; // 顶部边距(为页眉留出空间) + float bottomMargin = 60; // 底部边距(为页脚留出空间) + float leftMargin = 50; // 左边距 + float rightMargin = 50; // 右边距 + + document = new Document(PageSize.A4, + leftMargin, rightMargin, topMargin, bottomMargin); + + fos = new FileOutputStream(filePath); PdfWriter writer = PdfWriter.getInstance(document, fos); - // 使用完整的页脚处理器 - writer.setPageEvent(new CompletePdfFooter()); + // 设置页面事件,每页添加文字页眉 + writer.setPageEvent(new TextHeaderEvent(headerText)); // 5. 设置PDF属性 document.addTitle("华西海圻"); @@ -67,28 +75,6 @@ public class PdfUtil { document.addCreationDate(); document.open(); - // 6. 设置中文字体 - BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); - Font titleFont = new Font(bfChinese, 16, Font.BOLD); - - // 7. 添加签名 - Paragraph title = new Paragraph(qm, titleFont); - title.setAlignment(Element.ALIGN_CENTER); - title.setSpacingAfter(20); - document.add(title); - - //横线 - LineSeparator line = new LineSeparator(); - line.setLineWidth(0.5f); - // 设置100%宽度 - line.setPercentage(100f); // 百分比宽度 - // 设置对齐方式 - line.setAlignment(Element.ALIGN_CENTER); // Element.ALIGN_CENTER, Element.ALIGN_LEFT等 - // 设置颜色 - line.setLineColor(BaseColor.BLACK); - // 添加到文档 - document.add(line); - Map formData = new LinkedHashMap<>(); formData.put("姓名","张三"); formData.put("性别", "男"); @@ -117,11 +103,11 @@ public class PdfUtil { /** * 导出table - * @param qm + * @param headerText * @param fileDir * @return */ - public static String exportTable( String qm,String fileDir) { + public static String exportTable( String headerText,String fileDir) { Document document = null; FileOutputStream fos = null; String filePath = ""; @@ -138,13 +124,21 @@ public class PdfUtil { // 3. 完整文件路径 filePath = Paths.get(fileDir, fileName).toString(); - // 4. 创建PDF文档 - document = new Document(PageSize.A4); + // 创建PDF文档 设置文档边距,避免内容遮挡页眉页脚 + float topMargin = 70; // 顶部边距(为页眉留出空间) + float bottomMargin = 60; // 底部边距(为页脚留出空间) + float leftMargin = 50; // 左边距 + float rightMargin = 50; // 右边距 + + document = new Document(PageSize.A4, + leftMargin, rightMargin, topMargin, bottomMargin); + fos = new FileOutputStream(filePath); PdfWriter writer = PdfWriter.getInstance(document, fos); - // 使用完整的页脚处理器 - writer.setPageEvent(new CompletePdfFooter()); + // 设置页面事件,每页添加文字页眉 + writer.setPageEvent(new TextHeaderEvent(headerText)); + // 5. 设置PDF属性 document.addTitle("华西海圻"); document.addAuthor("华西海圻"); @@ -155,28 +149,9 @@ public class PdfUtil { // 6. 设置中文字体 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); - Font titleFont = new Font(bfChinese, 18, Font.BOLD); Font headerFont = new Font(bfChinese, 12, Font.BOLD); Font contentFont = new Font(bfChinese, 10, Font.NORMAL); - // 7. 添加签名 - Paragraph title = new Paragraph(qm, titleFont); - title.setAlignment(Element.ALIGN_CENTER); - title.setSpacingAfter(20); - document.add(title); - - //横线 - LineSeparator line = new LineSeparator(); - line.setLineWidth(0.5f); - // 设置100%宽度 - line.setPercentage(100f); // 百分比宽度 - // 设置对齐方式 - line.setAlignment(Element.ALIGN_CENTER); // Element.ALIGN_CENTER, Element.ALIGN_LEFT等 - // 设置颜色 - line.setLineColor(BaseColor.BLACK); - // 添加到文档 - document.add(line); - // 9. 创建表格 PdfPTable table = new PdfPTable(3); table.setWidthPercentage(100); @@ -311,78 +286,60 @@ public class PdfUtil { /** - * 页脚 + * 页面事件处理类 - 每页添加文字页眉 */ - public static class CompletePdfFooter extends PdfPageEventHelper { - - private Font footerFont; - private PdfTemplate total; - private int totalPages = 0; - - public CompletePdfFooter() { - try { - BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); - footerFont = new Font(baseFont, 10, Font.NORMAL, BaseColor.GRAY); - } catch (Exception e) { - footerFont = new Font(Font.FontFamily.HELVETICA, 10, Font.NORMAL, BaseColor.GRAY); - } - } - - @Override - public void onOpenDocument(PdfWriter writer, Document document) { - total = writer.getDirectContent().createTemplate(100, 16); + static class TextHeaderEvent extends PdfPageEventHelper { + private String headerText; + private BaseFont baseFont; + + public TextHeaderEvent(String headerText) throws Exception { + this.headerText = headerText; + // 创建字体(支持中文) + this.baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); } - @SneakyThrows @Override public void onEndPage(PdfWriter writer, Document document) { - PdfContentByte cb = writer.getDirectContent(); - - int currentPage = writer.getPageNumber(); - float y = document.bottom() - 20; - - // 在每一页都重新计算,确保位置准确 - String pageText = "第 " + currentPage + " 页 / 共 "; - BaseFont baseFont = footerFont.getBaseFont(); + try { + // 获取页面尺寸 + Rectangle pageSize = document.getPageSize(); + float pageWidth = pageSize.getWidth(); + float pageHeight = pageSize.getHeight(); - // 计算页面文本宽度 - float pageTextWidth = baseFont.getWidthPoint(pageText, footerFont.getSize()); + // 设置页眉参数 + float topMargin = 30; // 顶部边距 + float fontSize = 12; // 字体大小 - // 临时用估计的总页数计算位置(等文档关闭后会被替换) - String tempTotal = String.valueOf(writer.getPageNumber() + 5); // 估计值 - float totalWidth = baseFont.getWidthPoint(tempTotal, footerFont.getSize()); + // 计算文字宽度(用于居中) + float textWidth = baseFont.getWidthPoint(headerText, fontSize); - // 计算起始位置 - float totalTextWidth = pageTextWidth + totalWidth + - baseFont.getWidthPoint(" 页", footerFont.getSize()); - float startX = (document.getPageSize().getWidth() - totalTextWidth) / 2; + // 计算居中位置 + float textX = (pageWidth - textWidth) / 2; + float textY = pageHeight - topMargin; - // 写入当前页信息 - ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, - new Phrase(pageText, footerFont), startX, y, 0); + // 获取画布 + PdfContentByte canvas = writer.getDirectContent(); - // 添加总页数模板 - cb.addTemplate(total, startX + pageTextWidth, y); + // 添加页眉文字 + canvas.beginText(); + canvas.setFontAndSize(baseFont, fontSize); + canvas.setTextMatrix(textX, textY); + canvas.showText(headerText); + canvas.endText(); - // 写入"页"字 - ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, - new Phrase(" 页", footerFont), startX + pageTextWidth + totalWidth, y, 0); + // 添加页眉分隔线(可选) + addHeaderLine(canvas, pageWidth, textY - 10); + } catch (Exception e) { + e.printStackTrace(); + } } - @Override - public void onCloseDocument(PdfWriter writer, Document document) { - totalPages = writer.getPageNumber(); - String totalPagesStr = String.valueOf(totalPages); - - // 重新创建合适宽度的模板 - BaseFont baseFont = footerFont.getBaseFont(); - total.beginText(); - total.setFontAndSize(baseFont, footerFont.getSize()); - total.setTextMatrix(0, 0); - total.showText(totalPagesStr); - total.endText(); + private void addHeaderLine(PdfContentByte canvas, float pageWidth, float yPos) { + canvas.setLineWidth(0.5f); + canvas.moveTo(50, yPos); + canvas.lineTo(pageWidth - 50, yPos); + canvas.stroke(); } } - } \ No newline at end of file