From a5edca4c62ea99418dcaa5d792947b1bab7fe8b2 Mon Sep 17 00:00:00 2001
From: fei <791364011@qq.com>
Date: 星期三, 17 十二月 2025 23:08:36 +0800
Subject: [PATCH] 修改了对应代码
---
archiveManager/src/main/java/com/ruoyi/service/impl/pdfGenerateService.java | 208 ++++++++++++++++++++++++++++++++++++---------------
1 files changed, 146 insertions(+), 62 deletions(-)
diff --git a/archiveManager/src/main/java/com/ruoyi/service/impl/pdfGenerateService.java b/archiveManager/src/main/java/com/ruoyi/service/impl/pdfGenerateService.java
index 8c8e306..d8cc7b7 100644
--- a/archiveManager/src/main/java/com/ruoyi/service/impl/pdfGenerateService.java
+++ b/archiveManager/src/main/java/com/ruoyi/service/impl/pdfGenerateService.java
@@ -7,10 +7,7 @@
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
-import com.itextpdf.text.pdf.BaseFont;
-import com.itextpdf.text.pdf.PdfPCell;
-import com.itextpdf.text.pdf.PdfPTable;
-import com.itextpdf.text.pdf.PdfWriter;
+import com.itextpdf.text.pdf.*;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.domain.ArchiveRecords;
import com.ruoyi.domain.vo.DocumentMaterialFileStyle;
@@ -18,6 +15,7 @@
import com.ruoyi.domain.vo.DocumentMaterialsVoSmall;
import com.ruoyi.service.IArchiveRecordsService;
import com.ruoyi.service.IDocumentMaterialsService;
+import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
@@ -32,6 +30,7 @@
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
+import java.util.Arrays;
import java.util.Date;
import java.util.List;
@@ -268,10 +267,79 @@
}
+ // 鑷畾涔夐〉闈簨浠剁被锛岀敤浜庡湪姣忎釜鏂伴〉闈㈡坊鍔犲嵎鍐呭皝闈㈠唴瀹�
+ private class DirectoryHeaderPageEvent extends PdfPageEventHelper {
+ private String volumeNumber;
+ private byte[] barcodeImageBytes;
+
+ public DirectoryHeaderPageEvent(String volumeNumber, byte[] barcodeImageBytes) {
+ this.volumeNumber = volumeNumber;
+ this.barcodeImageBytes = barcodeImageBytes;
+ }
+
+ @Override
+ public void onStartPage(PdfWriter writer, Document document) {
+ try {
+ // 璁剧疆涓枃瀛椾綋
+ BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
+ Font chineseFont = new Font(bfChinese, 12);
+ Font chineseFont1 = new Font(bfChinese, 16, Font.BOLD);
+
+ // 娣诲姞鏉″舰鐮�
+ Image img = Image.getInstance(barcodeImageBytes);
+ PdfPCell pdfPCell = new PdfPCell(img);
+ pdfPCell.setBorder(Rectangle.NO_BORDER);
+ pdfPCell.setMinimumHeight(40);
+ pdfPCell.setUseAscender(true);
+ pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
+ pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
+ pdfPCell.setColspan(2);
+ pdfPCell.setPaddingBottom(30);
+
+ float[] columnWidths1 = {35f, 65f};
+ PdfPTable table1 = new PdfPTable(columnWidths1);
+ table1.setWidthPercentage(80);
+ table1.setHorizontalAlignment(Element.ALIGN_LEFT);
+ table1.setSpacingBefore(30f);
+ table1.addCell(pdfPCell);
+
+ PdfContentByte canvas = writer.getDirectContent();
+ ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(" ", chineseFont),
+ document.left(), document.top() - 50, 0);
+ document.add(table1);
+
+ // 娣诲姞鏍囬
+ Paragraph title = new Paragraph("鍗� 鍐� 鐩� 褰�", chineseFont1);
+ title.setAlignment(Element.ALIGN_CENTER);
+ document.add(title);
+ Paragraph withNewLine = new Paragraph("\n");
+ document.add(withNewLine);
+ document.add(withNewLine);
+
+ // 娣诲姞鍗峰彿
+ Paragraph recordInfo = new Paragraph("鍗峰彿锛�" + volumeNumber, chineseFont);
+ recordInfo.setAlignment(Element.ALIGN_RIGHT);
+ document.add(recordInfo);
+
+ document.add(withNewLine);
+ document.add(withNewLine);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
//瀵煎嚭鍗峰唴鐩綍鐨刾df
public void generateFileDirectoryPdf(String pdfPath,List<DocumentMaterialsVo> dvss) throws DocumentException, IOException {
Document document = new Document();
- PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
+ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
+
+ // 娣诲姞椤甸潰浜嬩欢锛屽湪姣忎釜鏂伴〉闈㈣嚜鍔ㄦ坊鍔犲嵎鍐呭皝闈㈠唴瀹�
+ String volumeNumber = dvss.get(0).getRecordId();
+ byte[] barcodeImageBytes = barcodeService.generateBarcodeImage(volumeNumber);
+ writer.setPageEvent(new DirectoryHeaderPageEvent(volumeNumber, barcodeImageBytes));
+
document.open();
// 鍒涘缓琛ㄦ牸锛�5鍒楋級
@@ -279,56 +347,19 @@
// 璁剧疆琛ㄦ牸瀹藉害锛堝崰椤甸潰瀹藉害鐨�100%锛�
table.setWidthPercentage(100);
-
-
- //娣诲姞鏉″舰鐮�
- String volumeNumber = dvss.get(0).getRecordId();
- Image img = Image.getInstance(barcodeService.generateBarcodeImage(volumeNumber));
- // 璁剧疆鍥剧墖鍦≒DF涓殑浣嶇疆锛堝彲閫夛級
-// img.setAbsolutePosition(100, 100);
- // 灏嗗浘鐗囨坊鍔犲埌PDF鏂囨。涓�
- PdfPCell pdfPCell = new PdfPCell(img);
- pdfPCell.setBorder(Rectangle.NO_BORDER); // 绉婚櫎鍗曞厓鏍艰竟妗�
-
- pdfPCell.setMinimumHeight(40);
- pdfPCell.setUseAscender(true); // 璁剧疆鍙互灞呬腑
- pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); // 璁剧疆姘村钩灞呬腑
- pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 璁剧疆鍨傜洿灞呬腑
- pdfPCell.setColspan(2);
- pdfPCell.setPaddingBottom(30);
-// 鍒涘缓琛ㄦ牸骞惰缃垪瀹芥瘮渚�
- float[] columnWidths = {35f, 65f}; // 绗竴鍒�30%锛岀浜屽垪70%
- PdfPTable table1 = new PdfPTable(columnWidths);
-// PdfPTable table = new PdfPTable(2);
- table1.setWidthPercentage(80); // 澧炲ぇ琛ㄦ牸瀹藉害鐧惧垎姣�
- table1.setHorizontalAlignment(Element.ALIGN_LEFT); // 璁剧疆琛ㄦ牸鏁翠綋灞呬腑
- table1.setSpacingBefore(30f); // 璁剧疆琛ㄦ牸鍓嶉棿璺�
-
- table1.addCell(pdfPCell);
-
- document.add(table1);
-
-
- // 娣诲姞琛ㄥご
+
+ // 璁剧疆鍒楀姣斾緥锛岀4鍒楋紙鏂囦欢棰樺悕锛夎缃緱鏇村
+ float[] columnWidths = {8f, 14f, 12f, 30f, 10f, 10f, 10f}; // 璋冩暣鍒楀姣斾緥锛岀4鍒楁枃浠堕鍚嶅崰40%
+ table.setWidths(columnWidths);
+
+ // 璁剧疆涓枃瀛椾綋
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font chineseFont = new Font(bfChinese, 12);
Font chineseFont1 = new Font(bfChinese, 16, Font.BOLD);
- // 娣诲姞鏍囬
- Paragraph title = new Paragraph("鍗� 鍐� 鐩� 褰�", chineseFont1);
- title.setAlignment(Element.ALIGN_CENTER);
- document.add(title);
- Paragraph withNewLine = new Paragraph("\n");
- document.add(withNewLine);
- document.add(withNewLine);
-
-
- //娣诲姞鍗峰彿
- Paragraph recordInfo = new Paragraph("鍗峰彿锛�" + volumeNumber, chineseFont);
- recordInfo.setAlignment(Element.ALIGN_RIGHT);
- document.add(recordInfo);
-
- document.add(withNewLine);
- document.add(withNewLine);
+ // 璁剧疆琛ㄥご琛屾暟锛岃繖鏍峰垎椤垫椂姣忛〉閮戒細鑷姩閲嶅琛ㄥご
+ table.setHeaderRows(1);
+
+ // 娣诲姞琛ㄥご
String[] headers = {"搴忓彿", "鏂囦欢缂栧彿", "璐d换鑰�", "鏂囦欢棰樺悕", "鏃ユ湡", "椤靛彿", "澶囨敞"};
for (String header : headers) {
PdfPCell cell = new PdfPCell(new Paragraph(header,
@@ -496,8 +527,8 @@
PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
document.open();
- String [] tits = {"妗� 鍙�:","妗f棣�(瀹�)鍙�:","缂� 寰� 鍙�: ","鍙� 鏂� 鍙�:",
- "妗� 鍗� 棰� 鍚�:","缂� 鍒� 鏃� 鏈�:","缂� 鍒� 鍗� 浣�:","淇� 绠� 鏈� 闄�:","瀵� 绾�:"};
+ String [] tits = {"妗e彿锛�","妗f棣�(瀹�)鍙凤細","缂╁井鍙凤細","鍙戞枃鍙凤細",
+ "妗堝嵎棰樺悕锛�","缂栧埗鏃ユ湡锛�","缂栧埗鍗曚綅锛�","淇濈鏈熼檺锛�","瀵嗙骇锛�"};
ArchiveRecords ard = iArchiveRecordsService.selectArchiveRecordsById(id);
@@ -509,7 +540,7 @@
;
System.out.println("褰撳墠鏃ユ湡: " + date);
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
formattedDate = date.format(formatter);
}
@@ -535,7 +566,7 @@
float[] columnWidths = {35f, 65f}; // 绗竴鍒�30%锛岀浜屽垪70%
PdfPTable table = new PdfPTable(columnWidths);
// PdfPTable table = new PdfPTable(2);
- table.setWidthPercentage(80); // 澧炲ぇ琛ㄦ牸瀹藉害鐧惧垎姣�
+ table.setWidthPercentage(78); // 鍑忓皬琛ㄦ牸瀹藉害鐧惧垎姣斾娇涓ゅ垪鏇寸揣鍑�
table.setHorizontalAlignment(Element.ALIGN_LEFT); // 璁剧疆琛ㄦ牸鏁翠綋灞呬腑
table.setSpacingBefore(30f); // 璁剧疆琛ㄦ牸鍓嶉棿璺�
table.setSpacingAfter(90f); // 璁剧疆琛ㄦ牸鍚庨棿璺�
@@ -559,12 +590,64 @@
+ // 鍏堣绠楁墍鏈夋爣棰樼殑鏈�闀块暱搴�
+ int maxTitleLength = 0;
+ for (String tit : tits) {
+ maxTitleLength = Math.max(maxTitleLength, tit.length()-1);
+ }
+
for(int i = 0; i < tits.length; i++) {
// 浣跨敤PdfPTable瀹炵幇绮剧‘瀵归綈
// 绗竴琛岋細妗e彿
- PdfPCell labelCell1 = new PdfPCell(new Phrase(tits[i], chineseFont));
- labelCell1.setHorizontalAlignment(Element.ALIGN_RIGHT); // 璁剧疆鍙冲榻�
+ // 瀹炵幇瀵归綈锛氬湪鏂囧瓧涔嬮棿濉厖绌烘牸锛屼娇鎵�鏈夋爣棰樻�婚暱搴︾浉鍚�
+ String originalText = tits[i];
+
+ // 璁$畻闇�瑕佹坊鍔犵殑绌烘牸鏁�
+ int spacesToAdd = maxTitleLength - originalText.length();
+
+ String formattedText = originalText;
+ if (spacesToAdd > 0) {
+ // 鍦ㄥ啋鍙峰墠鐨勬枃瀛椾箣闂村潎鍖�鍒嗛厤濉厖绌烘牸
+ if (originalText.contains("锛�")) {
+ int colonIndex = originalText.indexOf("锛�");
+ String textBeforeColon = originalText.substring(0, colonIndex);
+ String textAfterColon = originalText.substring(colonIndex);
+
+ // 濡傛灉鍐掑彿鍓嶅彧鏈変竴涓瓧绗︼紝鐩存帴鍦ㄥ悗闈㈠姞绌烘牸
+ if (textBeforeColon.length() == 1) {
+ String fullWidthSpaces = StringUtils.repeat("銆�", spacesToAdd);
+ formattedText = textBeforeColon + fullWidthSpaces + textAfterColon;
+ } else if (textBeforeColon.length() > 1) {
+ // 鍦ㄦ枃瀛椾箣闂村潎鍖�鍒嗛厤绌烘牸
+ StringBuilder sb = new StringBuilder();
+ int chars = textBeforeColon.length();
+ int spacesPerGap = spacesToAdd / (chars - 1);
+ int extraSpaces = spacesToAdd % (chars - 1);
+
+ for (int j = 0; j < chars; j++) {
+ sb.append(textBeforeColon.charAt(j));
+ if (j < chars - 1) {
+ // 娣诲姞鍩烘湰绌烘牸
+ sb.append(StringUtils.repeat("銆�", spacesPerGap));
+ // 鍒嗛厤鍓╀綑绌烘牸
+ if (j < extraSpaces) {
+ sb.append("銆�");
+ }
+ }
+ }
+ sb.append(textAfterColon);
+ formattedText = sb.toString();
+ }
+ } else {
+ // 濡傛灉娌℃湁鍐掑彿锛岀洿鎺ュ湪鏈熬鍔犵┖鏍硷紙搴旇涓嶄細鍑虹幇杩欑鎯呭喌锛�
+ String fullWidthSpaces = StringUtils.repeat("銆�", spacesToAdd);
+ formattedText = originalText + fullWidthSpaces;
+ }
+ }
+
+ PdfPCell labelCell1 = new PdfPCell(new Phrase(formattedText, chineseFont));
+ labelCell1.setHorizontalAlignment(Element.ALIGN_RIGHT); // 鍗曞厓鏍煎彸瀵归綈
labelCell1.setBorder(Rectangle.NO_BORDER);
// PdfPCell valueCell1 = new PdfPCell(new Phrase(cons[i], chineseFont));
@@ -573,17 +656,18 @@
labelCell1.setPaddingTop(10f); // 涓婂唴杈硅窛10鍗曚綅
labelCell1.setPaddingBottom(10f); // 涓嬪唴杈硅窛10鍗曚綅
labelCell1.setPaddingLeft(15f); // 宸﹀唴杈硅窛15鍗曚綅
- labelCell1.setPaddingRight(15f); // 鍙冲唴杈硅窛15鍗曚綅
+ labelCell1.setPaddingRight(0f); // 鍙冲唴杈硅窛15鍗曚綅
PdfPCell valueCell1 = new PdfPCell(new Phrase(cons[i], chineseFont));
valueCell1.setBorder(Rectangle.NO_BORDER);
- valueCell1.setUseBorderPadding(true);
+ valueCell1.setUseBorderPadding(false); // 绂佺敤杈规鍐呰竟璺濊绠�
valueCell1.setBorderWidthBottom(0.5f); // 璁剧疆搴曢儴杈规浣滀负涓嬪垝绾�
valueCell1.setMinimumHeight(30);
valueCell1.setPaddingTop(10f); // 涓婂唴杈硅窛10鍗曚綅
valueCell1.setPaddingBottom(10f); // 涓嬪唴杈硅窛10鍗曚綅
- valueCell1.setPaddingLeft(15f); // 宸﹀唴杈硅窛15鍗曚綅
+ valueCell1.setPaddingLeft(0f); // 宸﹀唴杈硅窛0鍗曚綅
+ valueCell1.setExtraParagraphSpace(0f); // 绉婚櫎娈佃惤棰濆绌洪棿
valueCell1.setPaddingRight(15f); // 鍙冲唴杈硅窛15鍗曚綅
table.addCell(labelCell1);
table.addCell(valueCell1);
@@ -598,7 +682,7 @@
PdfPCell pdfPCell1 = new PdfPCell(img1);
pdfPCell1.setBorder(Rectangle.NO_BORDER); // 绉婚櫎鍗曞厓鏍艰竟妗�
- pdfPCell1.setMinimumHeight(40);
+ pdfPCell1.setMinimumHeight(20);
pdfPCell1.setUseAscender(true); // 璁剧疆鍙互灞呬腑
pdfPCell1.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); // 璁剧疆姘村钩灞呬腑
pdfPCell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 璁剧疆鍨傜洿灞呬腑
--
Gitblit v1.9.1