From 38553de8fe4a824919563db827019909caa65f9c Mon Sep 17 00:00:00 2001
From: fei <791364011@qq.com>
Date: 星期三, 24 十二月 2025 10:34:59 +0800
Subject: [PATCH] 修改了对应代码

---
 archiveManager/src/main/java/com/ruoyi/service/impl/pdfGenerateService.java |  267 +++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 195 insertions(+), 72 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..41e1fe2 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;
 
@@ -48,7 +47,7 @@
     public byte[] createQrCodeN(String content, int width, int height) throws IOException {
         QrConfig config = new QrConfig(width, height);
 
-        config.setMargin(3);
+        config.setMargin(0);
         // 楂樼籂閿欑骇鍒�
         config.setErrorCorrection(ErrorCorrectionLevel.H);
         // 璁剧疆鍓嶆櫙鑹诧紝鏃簩缁寸爜棰滆壊锛堣嚜琛岄�夋嫨棰滆壊淇敼锛�
@@ -268,67 +267,130 @@
     }
 
 
+    // 鑷畾涔夐〉闈簨浠剁被锛岀敤浜庡湪姣忎釜鏂伴〉闈㈡坊鍔犲嵎鍐呭皝闈㈠拰鍗峰彿
+    private class DirectoryHeaderPageEvent extends PdfPageEventHelper {
+        private String volumeNumber;
+        private BaseFont bfChinese;
+        private Font chineseFont;
+        private Font chineseFont1;
+        private Image barcodeImage;
+        
+        public DirectoryHeaderPageEvent(String volumeNumber, BaseFont bfChinese, Font chineseFont, Font chineseFont1, Image barcodeImage) {
+            this.volumeNumber = volumeNumber;
+            this.bfChinese = bfChinese;
+            this.chineseFont = chineseFont;
+            this.chineseFont1 = chineseFont1;
+            this.barcodeImage = barcodeImage;
+        }
+        
+        @Override
+        public void onStartPage(PdfWriter writer, Document document) {
+            try {
+                PdfContentByte cb = writer.getDirectContent();
+                float pageWidth = document.getPageSize().getWidth();
+                float pageHeight = document.getPageSize().getHeight();
+                
+                // 1. 娣诲姞鏉″舰鐮侊紙灞呬腑鏄剧ず锛岄〉闈㈤《閮級
+                float barcodeWidth = barcodeImage.getScaledWidth();
+                float barcodeHeight = barcodeImage.getScaledHeight();
+                float barcodeX = (pageWidth - barcodeWidth) / 2;
+                float barcodeY = pageHeight - 50 - barcodeHeight; // 椤甸潰椤堕儴涓嬫柟50鐐�
+                barcodeImage.setAbsolutePosition(barcodeX, barcodeY);
+                cb.addImage(barcodeImage);
+                
+                // 2. 娣诲姞鍗峰唴鐩綍鏍囬锛堝眳涓樉绀猴紝鏉″舰鐮佷笅鏂癸級
+                String title = "鍗�    鍐�    鐩�    褰�";
+                float titleX = pageWidth / 2;
+                float titleY = pageHeight - 100; // 椤甸潰椤堕儴涓嬫柟100鐐�
+                cb.beginText();
+                // 鍒涘缓骞惰缃爣棰樹负鍔犵矖瀛椾綋
+                Font boldTitleFont = new Font(bfChinese, 16, Font.BOLD);
+                cb.setFontAndSize(boldTitleFont.getBaseFont(), boldTitleFont.getSize());
+                cb.setColorFill(BaseColor.BLACK);
+                cb.showTextAligned(PdfContentByte.ALIGN_CENTER, title, titleX, titleY, 0);
+                cb.endText();
+                
+                // 3. 娣诲姞鍗峰彿锛堝眳鍙虫樉绀猴紝鏍囬涓嬫柟锛�
+                String label = "鍗峰彿锛�";
+                String value = volumeNumber;
+                float recordInfoX = pageWidth - 30;
+                float recordInfoY = pageHeight - 130; // 椤甸潰椤堕儴涓嬫柟130鐐�
+                
+                // 鍏堢粯鍒舵爣绛�"鍗峰彿锛�"
+                cb.beginText();
+                // 璁剧疆鍔犵矖瀛椾綋锛岄�氳繃Font.BOLD鍙傛暟
+                Font boldFont = new Font(bfChinese, 16, Font.BOLD);
+                cb.setFontAndSize(boldFont.getBaseFont(), boldFont.getSize());
+                cb.setColorFill(BaseColor.BLACK);
+                cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, label + value, recordInfoX, recordInfoY, 0);
+                cb.endText();
+                
+                // 璁$畻鍊肩殑浣嶇疆骞剁粯鍒朵笅鍒掔嚎
+                float labelWidth = bfChinese.getWidthPoint(label, 12);
+                float valueWidth = bfChinese.getWidthPoint(value, 12);
+                float underlineStartX = recordInfoX - valueWidth;
+                float underlineEndX = recordInfoX;
+                float underlineY = recordInfoY - 2;
+                
+                cb.setColorStroke(BaseColor.BLACK);
+                cb.setLineWidth(0.5f);
+                cb.moveTo(underlineStartX, underlineY);
+                cb.lineTo(underlineEndX, underlineY);
+                cb.stroke();
+                
+            } 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));
-        document.open();
-
-        // 鍒涘缓琛ㄦ牸锛�5鍒楋級
-        PdfPTable table = new PdfPTable(7);
-
-        // 璁剧疆琛ㄦ牸瀹藉害锛堝崰椤甸潰瀹藉害鐨�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);
-
-
-        // 娣诲姞琛ㄥご
+        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
+        
+        // 璁剧疆涓枃瀛椾綋
         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);
+        
+        // 鑾峰彇鍗峰彿
+        String volumeNumber = dvss.get(0).getRecordId();
+        
+        // 鍒涘缓鏉″舰鐮�
+        Image barcodeImage = Image.getInstance(barcodeService.generateBarcodeImage(volumeNumber));
+        barcodeImage.scaleToFit(80, 40); // 璁剧疆鏉″舰鐮佸ぇ灏�
+        
+        // 娉ㄥ唽椤甸潰浜嬩欢
+        DirectoryHeaderPageEvent pageEvent = new DirectoryHeaderPageEvent(volumeNumber, bfChinese, chineseFont1, chineseFont1, barcodeImage);
+        writer.setPageEvent(pageEvent);
+        
+        // 璁剧疆閫傚綋鐨勮竟璺濓紝涓哄嵎鍐呭皝闈€�佹潯褰㈢爜鍜屽嵎鍙烽鐣欑┖闂�
+        // setMargins(left, right, top, bottom) - 鍙傛暟椤哄簭锛氬乏銆佸彸銆佷笂銆佷笅
+        // 澧炲ぇ涓婅竟璺濅负160锛岀‘淇濋〉鐪夊唴瀹规湁瓒冲绌洪棿
+        document.setMargins(30, 30, 160, 30);
+        document.open();
+        
+        // 杩欓噷涓嶅啀闇�瑕佸湪绗竴椤垫墜鍔ㄦ坊鍔犲嵎鍐呭皝闈㈠唴瀹癸紝椤甸潰浜嬩欢浼氬鐞�
+        
+        // 娣诲姞涓�涓┖鐧芥钀斤紝纭繚琛ㄦ牸鍐呭涓嶄細涓庨《閮ㄥ厓绱犻噸鍙�
+        Paragraph blankParagraph = new Paragraph("");
+        document.add(blankParagraph);
+        
+        // 鍒涘缓琛ㄦ牸锛�7鍒楋級
+        PdfPTable table = new PdfPTable(7);
 
-
-        //娣诲姞鍗峰彿
-        Paragraph recordInfo = new Paragraph("鍗峰彿锛�" + volumeNumber, chineseFont);
-        recordInfo.setAlignment(Element.ALIGN_RIGHT);
-        document.add(recordInfo);
-
-        document.add(withNewLine);
-        document.add(withNewLine);
+        // 璁剧疆琛ㄦ牸瀹藉害锛堝崰椤甸潰瀹藉害鐨�80%锛屽眳涓樉绀猴級
+        table.setWidthPercentage(90);
+        table.setHorizontalAlignment(Element.ALIGN_CENTER);
+        
+        // 璁剧疆鍒楀姣斾緥锛岀4鍒楋紙鏂囦欢棰樺悕锛夊拰绗�2鍒楋紙鏂囦欢缂栧彿锛夎缃緱鏇村
+        float[] columnWidths = {10f, 16f, 14f, 30f, 10f, 10f, 10f}; // 璋冩暣鍒楀姣斾緥锛屽鍔犳枃浠剁紪鍙峰垪鐨勫搴�
+        table.setWidths(columnWidths);
+        // 璁剧疆琛ㄥご琛屾暟锛岃繖鏍峰垎椤垫椂姣忛〉閮戒細鑷姩閲嶅琛ㄥご
+        table.setHeaderRows(1);
+        
+        // 娣诲姞琛ㄥご
         String[] headers = {"搴忓彿", "鏂囦欢缂栧彿", "璐d换鑰�", "鏂囦欢棰樺悕", "鏃ユ湡", "椤靛彿", "澶囨敞"};
         for (String header : headers) {
             PdfPCell cell = new PdfPCell(new Paragraph(header,
@@ -355,9 +417,9 @@
                 table.addCell(cell);
 
 
-                //搴忓彿
+                //鏂囦欢缂栧彿锛堟。鍙凤級
                 PdfPCell cell1 = new PdfPCell(new Paragraph(cellData.getDocumentNumber()==null?"":cellData.getDocumentNumber().toString(),
-                        chineseFont));
+                        new Font(bfChinese, 12)));
                 cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                 cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
 
@@ -439,7 +501,15 @@
 
        // 娣诲姞鍐呭
      //  String volumeNumber = "D3.4.1-05-2024-0002";
-       Paragraph recordInfo = new Paragraph("鍗峰彿锛�" + volumeNumber, chineseFont);
+       
+       // 鍒涘缓鍗峰彿娈佃惤锛屼娇鐢–hunk鏉ュ崟鐙鐞嗗嵎鍙峰�煎苟娣诲姞涓嬪垝绾�
+       Paragraph recordInfo = new Paragraph();
+       Chunk labelChunk = new Chunk("鍗峰彿锛�", chineseFont);
+       Chunk valueChunk = new Chunk(volumeNumber, chineseFont);
+       valueChunk.setUnderline(0.5f, -2f); // 娣诲姞涓嬪垝绾匡紝0.5f鏄嚎瀹斤紝-2f鏄嚎涓庢枃瀛楃殑璺濈
+       
+       recordInfo.add(labelChunk);
+       recordInfo.add(valueChunk);
        recordInfo.setAlignment(Element.ALIGN_RIGHT);
        document.add(recordInfo);
        int pcc = dmfs.size()<=1?0: dmfs.get(1).getCnt();
@@ -496,8 +566,8 @@
         PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
         document.open();
 
-        String [] tits = {"妗�        鍙�:","妗f棣�(瀹�)鍙�:","缂�  寰�  鍙�: ","鍙�  鏂�  鍙�:",
-                "妗�  鍗�  棰�  鍚�:","缂�  鍒�  鏃�  鏈�:","缂�  鍒�  鍗�  浣�:","淇�  绠�  鏈�  闄�:","瀵�     绾�:"};
+        String [] tits = {"妗e彿锛�","妗f棣�(瀹�)鍙凤細","缂╁井鍙凤細","鍙戞枃鍙凤細",
+                "妗堝嵎棰樺悕锛�","缂栧埗鏃ユ湡锛�","缂栧埗鍗曚綅锛�","淇濈鏈熼檺锛�","瀵嗙骇锛�"};
         ArchiveRecords ard = iArchiveRecordsService.selectArchiveRecordsById(id);
 
 
@@ -509,7 +579,7 @@
             ;
             System.out.println("褰撳墠鏃ユ湡: " + date);
 
-        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
       formattedDate = date.format(formatter);
         }
 
@@ -535,7 +605,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 +629,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 +695,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 +721,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