From 497c4100c511e6880cf67817a4966196680b38cb Mon Sep 17 00:00:00 2001
From: zhaowenxuan <chacca165@163.com>
Date: 星期六, 07 十二月 2024 17:19:53 +0800
Subject: [PATCH] 调整澄合矿务局中心医院pdf

---
 ltkj-admin/src/main/java/com/ltkj/web/controller/his/HisPDFUtil.java |  369 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 323 insertions(+), 46 deletions(-)

diff --git a/ltkj-admin/src/main/java/com/ltkj/web/controller/his/HisPDFUtil.java b/ltkj-admin/src/main/java/com/ltkj/web/controller/his/HisPDFUtil.java
index e1955ac..fbeaef0 100644
--- a/ltkj-admin/src/main/java/com/ltkj/web/controller/his/HisPDFUtil.java
+++ b/ltkj-admin/src/main/java/com/ltkj/web/controller/his/HisPDFUtil.java
@@ -3,11 +3,16 @@
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.itextpdf.text.*;
+import com.itextpdf.text.Font;
+import com.itextpdf.text.Image;
+import com.itextpdf.text.Rectangle;
 import com.itextpdf.text.pdf.*;
 import com.itextpdf.text.pdf.draw.LineSeparator;
 import com.ltkj.common.core.domain.AjaxResult;
+import com.ltkj.common.core.domain.entity.SysDictData;
 import com.ltkj.common.core.domain.entity.SysUser;
 import com.ltkj.common.utils.DateUtils;
 import com.ltkj.framework.config.MatchUtils;
@@ -18,14 +23,17 @@
 import com.ltkj.hosp.sqlDomain.LtkjHybgd;
 import com.ltkj.hosp.sqlDomain.LtkjHysqd;
 import com.ltkj.system.service.ISysConfigService;
+import com.ltkj.system.service.ISysDictDataService;
 import com.ltkj.system.service.ISysUserService;
 import com.ltkj.web.config.pdfutils.PdfUtils;
+import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.awt.*;
 import java.io.*;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
@@ -34,6 +42,7 @@
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * 妫�鏌� 妫�楠屾姤鍛妏df鐢熸垚
@@ -81,6 +90,10 @@
     private LtkjExamJcbgdService jcbgdService;
     @Resource
     private LtkjExamJcsqdService jcsqdService;
+    @Autowired
+    private IDictHospService dictHospService;
+    @Autowired
+    private ISysDictDataService sysDictDataService;
 
     private List<Map<String, Object>> makePDFInfo(HashMap<String, Object> map, AjaxResult ajaxResult, String key) {
         List<Map<String, Object>> data = null;
@@ -104,6 +117,10 @@
     }
 
     public AjaxResult hisPDF(TjOrder tjOrder, TjCustomer customer, TjReportTemplate tjReportTemplate) throws Exception {
+        DictHosp dictHosp = dictHospService.list().get(0);
+        String color = sysConfigService.selectConfigByKey("report_title_color");
+//        list.get(0).getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")
+        List<SysDictData> sysUserSex = sysDictDataService.list(new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getDictType, "sys_user_sex"));
         HashMap<String, Object> pdfChangGui = makePDFInfo(tjOrder, customer, tjReportTemplate);
         if (pdfChangGui == null)
             return AjaxResult.error();
@@ -113,8 +130,22 @@
         ByteArrayOutputStream byteArrayOutputStream1 = new ByteArrayOutputStream();
         Document document1 = new Document(PageSize.A4);
         PdfWriter pdfWriter = null;
+        int pageSize = 0;
         try {
             pdfWriter = PdfWriter.getInstance(document1, byteArrayOutputStream1);
+            if (dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")){
+                String sex = "";
+                if (customer.getCusSex() != null) {
+                    for (SysDictData userSex : sysUserSex) {
+                        if (userSex.getDictValue().equals(String.valueOf(customer.getCusSex()))) {
+                            sex = userSex.getDictLabel();
+                        }
+                    }
+                }
+                if (StrUtil.isBlank(sex)) sex = "鏈煡";
+                pdfWriter.setPageEvent(new FooterHeaderPageEvent("婢勫悎鐭垮姟灞�涓績鍖婚櫌",customer.getCusName(),tjOrder.getTjNumber(),sex,String.valueOf(DateUtil.ageOfNow(customer.getCusBrithday()))));
+                pageSize = pdfWriter.getPageNumber();
+            }
         } catch (DocumentException e) {
             e.printStackTrace();
             return AjaxResult.error();
@@ -125,7 +156,7 @@
         Map<TjOrderRemark, List<TjPdfVO>> printReport = new HashMap<>();
         notHuaYanData(tjOrder.getTjNumber(), printReport, tjOrder, customer);
         try {
-            notHuaYanTable(document1, printReport);
+            notHuaYanTable(document1, printReport,dictHosp,color);
         } catch (DocumentException e) {
             log.error("闈炲寲楠岄」鐩~鍏呰〃鏍煎紓甯�");
             e.printStackTrace();
@@ -133,13 +164,12 @@
         }
         // 缁勮妫�楠屾暟鎹�
         HashMap<String, Object> map = makeHisJianYan(tjOrder, customer);
-        makeHisJianYanTable(document1, map);
+        makeHisJianYanTable(document1, map,dictHosp,color);
 
 
-        List<ByteArrayOutputStream> jianchas = makeHisJianChaTable(tjOrder);
+        List<ByteArrayOutputStream> jianchas =  makeHisJianChaTable(tjOrder);
 //        List<ByteArrayOutputStream> jianchas = makeCcgcHisJianChaTable(tjOrder);
         document1.close();
-        pdfWriter.close();
         PdfReader reader = null;
         try {
             reader = new PdfReader(byteArrayOutputStream1.toByteArray());
@@ -172,16 +202,18 @@
             int pages = pdfReader.getNumberOfPages();
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
             PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);
-            PdfContentByte contentByte;
-            BaseFont baseFont = PdfUtils.createBaseFont(null);
-            for (int i = 2; i <= pages; i++) {
-                contentByte = pdfStamper.getOverContent(i);
-                contentByte.beginText();
-                contentByte.setFontAndSize(baseFont, 11);
-                float x = contentByte.getPdfDocument().getPageSize().getWidth() / 2;
-                float y = 20;
-                contentByte.showTextAligned(PdfContentByte.ALIGN_CENTER, String.valueOf(i - 1), x, y, 0);
-                contentByte.endText();
+            if (!dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")){
+                PdfContentByte contentByte;
+                BaseFont baseFont = PdfUtils.createBaseFont(null);
+                for (int i = 2; i <= pages; i++) {
+                    contentByte = pdfStamper.getOverContent(i);
+                    contentByte.beginText();
+                    contentByte.setFontAndSize(baseFont, 11);
+                    float x = contentByte.getPdfDocument().getPageSize().getWidth() / 2;
+                    float y = 20;
+                    contentByte.showTextAligned(PdfContentByte.ALIGN_CENTER, String.valueOf(i - 1), x, y, 0);
+                    contentByte.endText();
+                }
             }
             pdfStamper.close();
             pdfReader.close();
@@ -378,9 +410,8 @@
      * @param customer
      * @param finalOutPut
      */
-    private void savePDFFile(String outputFileName1, TjOrder tjOrder, TjCustomer customer, ByteArrayOutputStream finalOutPut) throws IOException {
-        // 鏂囦欢閰嶇疆
-//        String outputPath1 = "/Users/chacca/寮�鍙戠浉鍏�/浠g爜/";
+    private void savePDFFile(String outputFileName1, TjOrder tjOrder, TjCustomer customer, ByteArrayOutputStream finalOutPut) throws IOException, DocumentException, InterruptedException {
+        // 绗竴姝ワ細鐢熸垚鏂囦欢骞朵繚瀛�
         String outputPath1 = value + File.separator;
         File file = new File(outputPath1 + outputFileName1);
         BufferedOutputStream outputStream = null;
@@ -389,7 +420,30 @@
         outputStream.flush();
         outputStream.close();
         finalOutPut.close();
+
+//        Thread.sleep(2000);
+//
+//        FileInputStream fileInputStream = new FileInputStream(file);
+//        FileOutputStream fileOutputStream = new FileOutputStream(file);
+//
+//        PdfReader pdfReader = new PdfReader(fileInputStream);
+//        PdfStamper pdfStamper = new PdfStamper(pdfReader, fileOutputStream);
+//        AcroFields form = pdfStamper.getAcroFields();
+//
+//        List<String> fieldNames = new ArrayList<>(form.getFields().keySet());
+//        if (fieldNames.contains("page")) {
+//            form.setField("page", "1111");
+//        }
+//
+//        pdfStamper.setFormFlattening(true);  // 璁剧疆涓� true 鍚庤〃鍗曞瓧娈典笉鍙紪杈�
+//
+//        pdfStamper.close();
+//        pdfReader.close();
+//        fileInputStream.close();
+//        fileOutputStream.close();
     }
+
+
 
     /**
      * 濉厖妫�楠岃〃鏍兼暟鎹�
@@ -398,16 +452,17 @@
      * @param map
      * @throws DocumentException
      */
-    private void makeHisJianYanTable(Document document1, HashMap<String, Object> map) throws DocumentException {
+    private void makeHisJianYanTable(Document document1, HashMap<String, Object> map,DictHosp dictHosp,String colorStr) throws DocumentException {
         if (!map.isEmpty()) {
-            String[] titles = new String[]{"浣撴椤圭洰", "浣撴缁撴灉", "鍗曚綅", "鍙傝�冭寖鍥�"};
-            float[] titlesWidth = new float[]{120, 120, 120, 120};
+            String[] titles = new String[]{"浣撴椤圭洰", "浣撴缁撴灉","鏍囧織", "鍗曚綅", "鍙傝�冭寖鍥�"};
+            float[] titlesWidth = new float[]{270, 80, 40, 80, 100};
             for (Map.Entry<String, Object> entry : map.entrySet()) {
                 String titleName = entry.getKey().trim();
                 HashMap<String, Object> entryValue = (HashMap<String, Object>) entry.getValue();
                 HashSet<String> jyys = (HashSet<String>) entryValue.get("jyys");
                 ArrayList<List<String>> lists = (ArrayList<List<String>>) entryValue.get("data");
-                addJianYanTable(document1, titleName, titles, titlesWidth, lists);
+                String shsj = entryValue.get("shsj").toString();
+                addJianYanTable(document1, titleName, titles, titlesWidth, lists,dictHosp,colorStr);
                 if (!jyys.isEmpty()) {
                     Font doctorFonts = PdfUtils.setFont(11);
                     String doctors = "";
@@ -417,7 +472,7 @@
                     elements.setSpacingAfter(10f);
                     elements.setAlignment(Element.ALIGN_RIGHT);
                     Phrase phrase = new Phrase();
-                    phrase.add(new Chunk("瀹℃牳鍖诲笀锛�",doctorFonts));
+                    phrase.add(new Chunk("妫�鏌ユ棩鏈燂細"+shsj+"         瀹℃牳鍖诲笀锛�",doctorFonts));
                     boolean flag = false;
                     for (String s : jyys) {
                         if (s.contains("|")) {
@@ -443,7 +498,7 @@
                     }
                     if (!flag) {
                         String substring = doctors.substring(0, doctors.length() - 1).trim();
-                        Paragraph df = PdfUtils.setParagraph(doctorFonts, "瀹℃牳鍖诲笀锛�" + substring);
+                        Paragraph df = PdfUtils.setParagraph(doctorFonts, "妫�鏌ユ棩鏈燂細"+shsj+"         瀹℃牳鍖诲笀锛�" + substring);
                         df.setAlignment(Element.ALIGN_RIGHT);
                         document1.add(df);
                     } else {
@@ -452,9 +507,11 @@
                     }
                 }
                 //鍒嗗壊绾�
-                LineSeparator objectName = new LineSeparator();
-                objectName.setOffset(-5);
-                document1.add(objectName);
+                if (!dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")) {
+                    LineSeparator objectName = new LineSeparator();
+                    objectName.setOffset(-5);
+                    document1.add(objectName);
+                }
             }
         }
     }
@@ -466,7 +523,7 @@
      * @param printReport
      * @throws DocumentException
      */
-    private void notHuaYanTable(Document document1, Map<TjOrderRemark, List<TjPdfVO>> printReport) throws DocumentException {
+    private void notHuaYanTable(Document document1, Map<TjOrderRemark, List<TjPdfVO>> printReport,DictHosp dictHosp,String colorStr) throws DocumentException {
         for (Map.Entry<TjOrderRemark, List<TjPdfVO>> entry : printReport.entrySet()) {
             LambdaQueryWrapper<TjProject> wqqq = new LambdaQueryWrapper<>();
             wqqq.eq(TjProject::getProId, entry.getKey().getProId());
@@ -480,6 +537,25 @@
                     // 璁剧疆鏍囬瀛椾綋鏍峰紡
                     Font titleFonts = PdfUtils.setFont(9);
                     Paragraph paragraph = PdfUtils.setParagraph(titleFonts, titleName);
+
+                    if (dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")) {
+                        Font titleFont = PdfUtils.setFont(11);
+                        titleFont.setStyle(Font.BOLD);
+                        PdfPCell cellTitle = new PdfPCell(PdfUtils.setParagraph(titleFont, titleName));
+                        cellTitle.setBorder(Rectangle.NO_BORDER);
+                        Color color = Color.decode(colorStr);
+                        cellTitle.setBackgroundColor(new BaseColor(color.getRed(), color.getGreen(), color.getBlue())); // 璁剧疆鑳屾櫙棰滆壊锛堝崄鍏繘鍒� RGB锛�
+                        // 鍒涘缓琛ㄦ牸骞舵坊鍔犲崟鍏冩牸
+                        PdfPTable tableTitle = new PdfPTable(1);
+                        tableTitle.setWidthPercentage(100);
+                        tableTitle.addCell(cellTitle);
+                        // 灏嗚〃鏍兼坊鍔犲埌鏂囨。涓�
+                        document1.add(tableTitle);
+                    }else {
+                        document1.add(paragraph);
+                    }
+
+
                     String[] titles = {"浣撴椤圭洰", "浣撴缁撴灉", "鍗曚綅"};
                     // 鑾峰彇鍒楄〃鏁版嵁
                     //璁剧疆琛ㄥご瀛椾綋鏍峰紡
@@ -488,7 +564,7 @@
                     Font textFont = PdfUtils.setFont(9);
                     //鍒涘缓琛ㄦ牸  灏嗚〃澶村瓧浣撳拰姝f枃瀛椾綋鏀捐繘鍘�
                     PdfPTable table = PdfUtils.setTable2(headFont, textFont, titles, value);
-                    document1.add(paragraph);
+
                     document1.add(table);
                     //鍒ゆ柇澶囨敞鏄惁涓虹┖  绌轰笉鑳芥樉绀簄ull
                     if (entry.getKey().getRemark() == null) {
@@ -518,8 +594,10 @@
                     //鍒ゆ柇涓绘鍖诲笀鏄惁涓虹┖  绌轰笉鑳芥樉绀簄ull
                     zhuJianYiShi(document1, entry);
                     //鍒嗗壊绾�
-                    LineSeparator objectName = new LineSeparator();
-                    document1.add(objectName);
+                    if (!dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")) {
+                        LineSeparator objectName = new LineSeparator();
+                        document1.add(objectName);
+                    }
                 } else {
                     if (null != one11) {
                         titleName = one11.getProName();
@@ -529,6 +607,24 @@
                     // 璁剧疆鏍囬瀛椾綋鏍峰紡
                     Font titleFonts = PdfUtils.setFont(9);
                     Paragraph paragraph = PdfUtils.setParagraph(titleFonts, titleName);
+
+                    if (dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")) {
+                        Font titleFont = PdfUtils.setFont(11);
+                        titleFont.setStyle(Font.BOLD);
+                        PdfPCell cellTitle = new PdfPCell(PdfUtils.setParagraph(titleFont, titleName));
+                        cellTitle.setBorder(Rectangle.NO_BORDER);
+                        Color color = Color.decode("#B4CDCD");
+                        cellTitle.setBackgroundColor(new BaseColor(color.getRed(), color.getGreen(), color.getBlue())); // 璁剧疆鑳屾櫙棰滆壊锛堝崄鍏繘鍒� RGB锛�
+                        // 鍒涘缓琛ㄦ牸骞舵坊鍔犲崟鍏冩牸
+                        PdfPTable tableTitle = new PdfPTable(1);
+                        tableTitle.setWidthPercentage(100);
+                        tableTitle.addCell(cellTitle);
+                        // 灏嗚〃鏍兼坊鍔犲埌鏂囨。涓�
+                        document1.add(tableTitle);
+                    }else {
+                        document1.add(paragraph);
+                    }
+
                     String[] titles = {"浣撴椤圭洰", "浣撴缁撴灉", "鍗曚綅"};
                     // 鑾峰彇鍒楄〃鏁版嵁
                     //璁剧疆琛ㄥご瀛椾綋鏍峰紡
@@ -537,7 +633,6 @@
                     Font textFont = PdfUtils.setFont(9);
                     //鍒涘缓琛ㄦ牸  灏嗚〃澶村瓧浣撳拰姝f枃瀛椾綋鏀捐繘鍘�
                     PdfPTable table = PdfUtils.setTable(headFont, textFont, titles, value);
-                    document1.add(paragraph);
                     document1.add(table);
                     //鍒ゆ柇澶囨敞鏄惁涓虹┖  绌轰笉鑳芥樉绀簄ull
                     if (entry.getKey().getRemark() == null) {
@@ -566,9 +661,11 @@
                     }
                     //鍒ゆ柇涓绘鍖诲笀鏄惁涓虹┖  绌轰笉鑳芥樉绀簄ull
                     zhuJianYiShi(document1, entry);
-                    //鍒嗗壊绾�
-                    LineSeparator objectName = new LineSeparator();
-                    document1.add(objectName);
+                    if (!dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")) {
+                        //鍒嗗壊绾�
+                        LineSeparator objectName = new LineSeparator();
+                        document1.add(objectName);
+                    }
                 }
             }
         }
@@ -644,7 +741,9 @@
             String xmmc = hysqd.getXmmc();
             ArrayList<List<String>> lists = new ArrayList<>();
             HashSet<String> hashSet = new HashSet<>();
+            String shsj = "";
             for (LtkjHybgd hybgd : hybgds) {
+                shsj = hybgd.getShsj();
                 // 妫�鏌ラ」鐩�
                 String jcxm = hybgd.getJcxm() != null ? hybgd.getJcxm().trim() : null;
                 // 妫�楠岀粨鏋�
@@ -659,6 +758,8 @@
                 String shysmc = hybgd.getShysmc() != null ? hybgd.getShysmc().trim() : null;
                 // 瀹℃牳鍖诲笀缂栫爜
                 String shys = hybgd.getShys() != null ? hybgd.getShys().trim() : null;
+                String ycbz = "";
+
                 if (jyjg != null && StrUtil.isNotBlank(jyjg) && fwz != null && StrUtil.isNotBlank(fwz)) {
                     try {
                         String[] fwzs = fwz.split("-");
@@ -668,10 +769,12 @@
                         // 姣旇緝妫�楠岀粨鏋滃拰鑼冨洿鍊�
                         if (jyjgval.compareTo(min) < 0) {
                             // jyjg 灏忎簬鑼冨洿鏈�灏忓�硷紝娣诲姞涓嬬澶�
-                            jyjg = jyjg + "   鈫�";
+//                            jyjg = jyjg + "   鈫�";
+                            ycbz = "鈫�";
                         } else if (jyjgval.compareTo(max) > 0) {
                             // jyjg 澶т簬鑼冨洿鏈�澶у�硷紝娣诲姞涓婄澶�
-                            jyjg = jyjg + "   鈫�";
+//                            jyjg = jyjg + "   鈫�";
+                            ycbz = "鈫�";
                         }
                     } catch (Exception ignored) {
                     }
@@ -690,6 +793,7 @@
                 ArrayList<String> strings = new ArrayList<>();
                 strings.add(jcxm);
                 strings.add(jyjg);
+                strings.add(ycbz);
                 strings.add(jgdw);
                 strings.add(fwz);
                 lists.add(strings);
@@ -698,6 +802,7 @@
                 HashMap<String, Object> map = new HashMap<>();
                 map.put("data", lists);
                 map.put("jyys", hashSet);
+                map.put("shsj",shsj);
                 data.put(xmmc, map);
             }
         }
@@ -790,7 +895,7 @@
                 acroFields.setField(key, data.get(key).toString());
             }
             //璁剧疆涓烘棤娉曠紪杈�
-            pdfStamper.setFormFlattening(true);
+//            pdfStamper.setFormFlattening(true);
             pdfStamper.close();
             reader.close();
             // 灏嗕慨鏀瑰悗鐨凱DF鍐呭鍐欏叆
@@ -812,6 +917,7 @@
             map.put("document", document);
             map.put("copy", copy);
             map.put("stream", finalOutPut);
+            map.put("form",pdfStamper);
             return map;
         } catch (IOException | DocumentException e) {
             e.printStackTrace();
@@ -985,10 +1091,14 @@
         document.add(table);
     }
 
-    private static void addJianYanTable(Document document, String titleName, String[] titles, float[] titlesWidth, ArrayList<List<String>> lists) throws DocumentException {
-        Font titleFont = PdfUtils.setFont(9);
+    private static void addJianYanTable(Document document, String titleName, String[] titles, float[] titlesWidth, ArrayList<List<String>> lists,DictHosp dictHosp,String colorStr) throws DocumentException {
+        // 澶ч」鏍囬
+        Font titleFont = PdfUtils.setFont(10);
+        if (dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")) {
+            titleFont.setSize(11);
+            titleFont.setStyle(Font.BOLD);
+        }
         Paragraph paragraph = new Paragraph(titleName, titleFont);
-        // 璁剧疆鏂囧瓧灞呬腑
         paragraph.setAlignment(Element.ALIGN_LEFT);
         // 琛岄棿璺�
         paragraph.setLeading(5f);
@@ -996,13 +1106,34 @@
         paragraph.setSpacingBefore(10f);
         // 璁剧疆娈佃惤涓嬬┖鐧�
         paragraph.setSpacingAfter(10f);
-        document.add(paragraph);
+        if (dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")) {
+            // 琛岄棿璺�
+            paragraph.setLeading(7f);
+            // 璁剧疆娈佃惤涓婄┖鐧�
+            paragraph.setSpacingBefore(12f);
+            // 璁剧疆娈佃惤涓嬬┖鐧�
+            paragraph.setSpacingAfter(12f);
+            // 澶ч」
+            PdfPCell cellTitle = new PdfPCell(paragraph);
+            cellTitle.setBorder(Rectangle.NO_BORDER);
+            Color color = Color.decode(colorStr);
+            cellTitle.setBackgroundColor(new BaseColor(color.getRed(), color.getGreen(), color.getBlue())); // 璁剧疆鑳屾櫙棰滆壊锛堝崄鍏繘鍒� RGB锛�
+            // 鍒涘缓琛ㄦ牸骞舵坊鍔犲崟鍏冩牸
+            PdfPTable tableTitle = new PdfPTable(1);
+            tableTitle.setWidthPercentage(100);
+            tableTitle.addCell(cellTitle);
+
+            // 灏嗚〃鏍兼坊鍔犲埌鏂囨。涓�
+            document.add(tableTitle);
+        }else {
+            document.add(paragraph);
+        }
         // 鍒涘缓涓�涓〃鏍煎苟娣诲姞鍒版枃妗�
-        Font headFont = PdfUtils.setFont(9);
-        Font textFont = PdfUtils.setFont(9);
+        Font headFont = PdfUtils.setFont(10);
+        Font textFont = PdfUtils.setFont(10);
         PdfPTable table = new PdfPTable(titlesWidth);
-        table.setTotalWidth(PageSize.A4.getWidth() - 100); // 璁剧疆琛ㄦ牸鎬诲搴�
-        table.setLockedWidth(true); // 閿佸畾琛ㄦ牸瀹藉害
+        table.setTotalWidth(PageSize.A4.getWidth() - 10); // 璁剧疆琛ㄦ牸鎬诲搴�
+        //table.setLockedWidth(true); // 閿佸畾琛ㄦ牸瀹藉害
         table.setHorizontalAlignment(Element.ALIGN_LEFT);
         table.getDefaultCell().setBorder(0);
         table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
@@ -1011,15 +1142,36 @@
             cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
             cell.setHorizontalAlignment(Element.ALIGN_LEFT);
             cell.setBorder(Rectangle.NO_BORDER); // 绉婚櫎鍗曞厓鏍艰竟妗�
+
+//            Color color = Color.decode(colorStr);
+//            cell.setBackgroundColor(new BaseColor(color.getRed(), color.getGreen(), color.getBlue())); // 璁剧疆鑳屾櫙棰滆壊锛堝崄鍏繘鍒� RGB锛�
+
             table.addCell(cell);
         }
+
         for (List<String> list : lists) {
+            boolean isYc = false;
+            textFont = PdfUtils.setFont(10);
+            if (dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")){
+                if (list.contains("鈫�") || list.contains("鈫�")){
+                    isYc = true;
+                }
+            }
             for (String s : list) {
                 PdfPCell nameCell = new PdfPCell();
                 nameCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                 nameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
-                nameCell.setPhrase(new Paragraph(s, textFont));
+
                 nameCell.setBorder(Rectangle.NO_BORDER); // 绉婚櫎鍗曞厓鏍艰竟妗�
+                if (isYc){
+                    textFont.setStyle(Font.BOLD);
+                    nameCell.setPhrase(new Paragraph(s, textFont));
+                    nameCell.setBorder(Rectangle.NO_BORDER);
+//                    Color color = Color.decode("#0033cc");
+//                    nameCell.setBackgroundColor(new BaseColor(color.getRed(), color.getGreen(), color.getBlue())); // 璁剧疆鑳屾櫙棰滆壊锛堝崄鍏繘鍒� RGB锛�
+                }else {
+                    nameCell.setPhrase(new Paragraph(s, textFont));
+                }
                 table.addCell(nameCell);
             }
         }
@@ -1350,4 +1502,129 @@
             footer.writeSelectedRows(0, -1, 36, 50, writer.getDirectContent());
         }
     }
+
+    // 鑷畾涔夐〉鑴氬拰椤电湁鐨勭粯鍒�
+    static class FooterHeaderPageEvent extends PdfPageEventHelper {
+
+        private String name; // 椤电湁鐨勫悕瀛�
+        private String tjName;
+        private String tjNumber;
+        private String sex;
+        private String age;
+
+        public FooterHeaderPageEvent(String name,String tjName,String tjNumber,String sex,String age) {
+            this.name = name;
+            this.tjNumber = tjNumber;
+            this.tjName = tjName;
+            this.sex = sex;
+            this.age = age;
+        }
+
+        // 鍦ㄦ瘡涓�椤电殑椤堕儴缁樺埗椤电湁
+        @Override
+        public void onStartPage(PdfWriter writer, Document document) {
+            // 鑾峰彇椤甸潰瀹藉害鍜岄珮搴�
+            float width = document.getPageSize().getWidth();
+            float margin = document.leftMargin();
+
+            // 鍒涘缓椤电湁琛ㄦ牸锛�2琛岋紝绗竴琛屽寘鍚鍚嶃�佹�у埆銆佸勾榫勫拰浣撴鍙凤紝绗簩琛屾槸鍒嗗壊绾�
+            PdfPTable headerTable = new PdfPTable(2);  // 2鍒楋細绗竴鍒椾负濮撳悕銆佹�у埆銆佸勾榫勶紝绗簩鍒椾负浣撴鍙�
+            headerTable.setWidthPercentage(100); // 璁剧疆瀹藉害涓�100%
+            headerTable.setSpacingAfter(10); // 椤电湁涓庡唴瀹逛箣闂寸殑闂磋窛
+
+            // 璁剧疆鍒楀搴�
+            float[] columnWidths = {0.6f, 0.4f}; // 绗竴鍒楀崰 60%锛岀浜屽垪鍗� 40%
+            try {
+                headerTable.setWidths(columnWidths);
+            } catch (DocumentException e) {
+                e.printStackTrace();
+            }
+
+            // 绗竴琛岋細濮撳悕銆佹�у埆銆佸勾榫勪互鍙婁綋妫�鍙�
+            PdfPCell infoCell = new PdfPCell(new Phrase("濮撳悕锛�" + tjName + " 鎬у埆锛�" + sex + " 骞撮緞锛�" + age, PdfUtils.setFont(8)));
+            infoCell.setBorder(Rectangle.NO_BORDER);
+            infoCell.setHorizontalAlignment(Element.ALIGN_LEFT); // 绗竴鍒楀乏瀵归綈
+            infoCell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 鍨傜洿灞呬腑
+            headerTable.addCell(infoCell);
+
+            PdfPCell idCell = new PdfPCell(new Phrase("浣撴鍙凤細" + tjNumber, PdfUtils.setFont(8)));
+            idCell.setBorder(Rectangle.NO_BORDER);
+            idCell.setHorizontalAlignment(Element.ALIGN_RIGHT); // 绗簩鍒楀彸瀵归綈
+            idCell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 鍨傜洿灞呬腑
+            headerTable.addCell(idCell);
+
+            // 绗簩琛岋細娣$伆鑹插垎鍓茬嚎
+            PdfPCell separatorCell = new PdfPCell();
+            separatorCell.setBorder(Rectangle.TOP);  // 璁剧疆涓婅竟妗�
+            separatorCell.setBorderColor(BaseColor.LIGHT_GRAY); // 璁剧疆涓烘贰鐏拌壊
+            separatorCell.setBorderWidthTop(0.5f); // 璁剧疆涓婅竟妗嗙殑瀹藉害
+            separatorCell.setColspan(2); // 鍚堝苟涓ゅ垪
+            headerTable.addCell(separatorCell);
+
+            // 灏嗛〉鐪夎〃鏍兼坊鍔犲埌鏂囨。
+            try {
+                document.add(headerTable);
+            } catch (DocumentException e) {
+                e.printStackTrace();
+            }
+        }
+
+
+        // 鍦ㄦ瘡涓�椤电殑搴曢儴缁樺埗椤佃剼
+        @SneakyThrows
+        @Override
+        public void onEndPage(PdfWriter writer, Document document) {
+            // 鑾峰彇椤甸潰瀹藉害鍜岄珮搴�
+            float width = document.getPageSize().getWidth();
+            float height = document.getPageSize().getHeight();
+            float margin = document.leftMargin();
+
+            // 椤佃剼璺濈搴曢儴鐨勪綅缃紝纭繚鍏朵綅浜庨〉闈㈠簳閮� 澧炲姞鏄笂绉� 鍑忓皯鏄笅绉�
+            float footerPositionY = document.bottomMargin();
+
+            // 椤佃剼琛ㄦ牸锛�2琛�2鍒楋紝绗竴琛屽尰闄㈠悕鍜岄〉鐮侊紝绗簩琛岀绂忚
+            PdfPTable footerTable = new PdfPTable(2); // 2鍒楋紝绗竴鍒楀尰闄㈠悕锛岀浜屽垪椤电爜
+            footerTable.setWidthPercentage(100); // 璁剧疆瀹藉害涓�100%锛屼娇鍏舵按骞冲眳涓�
+            footerTable.setTotalWidth(width - margin * 2 + 10); // 璁剧疆鎬诲搴︼紝閬垮厤涓庨〉杈硅窛閲嶅彔
+            footerTable.setSpacingBefore(0); // 鍘婚櫎琛ㄦ牸椤堕儴鐨勯棿璺�
+            footerTable.setSpacingAfter(0); // 鍘婚櫎琛ㄦ牸搴曢儴鐨勯棿璺�
+            footerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER); // 鍘婚櫎鎵�鏈夊崟鍏冩牸鐨勮竟妗�
+            float[] columnWidths = { 2.5f, 2f };  // 绗竴鍒楀崰 1锛岀浜屽垪鍗� 2
+            footerTable.setWidths(columnWidths); // 璁剧疆鍒楀姣斾緥
+
+            // 绗竴琛岋細鍖婚櫌鍚嶅拰椤电爜
+            PdfPCell hospitalNameCell = new PdfPCell(new Phrase(name, PdfUtils.setFont(8)));
+            hospitalNameCell.setHorizontalAlignment(Element.ALIGN_RIGHT); // 姘村钩灞呬腑
+            hospitalNameCell.setVerticalAlignment(Element.ALIGN_TOP); // 鍨傜洿瀵归綈鍒板簳閮�
+            hospitalNameCell.setMinimumHeight(12f); // 璁剧疆鏈�灏忛珮搴︼紝閬垮厤楂樺害杩囧ぇ
+            hospitalNameCell.setBorder(Rectangle.TOP); // 璁剧疆涓婅竟妗�
+            hospitalNameCell.setBorderColor(BaseColor.LIGHT_GRAY); // 璁剧疆涓婅竟妗嗕负娣$伆鑹�
+            hospitalNameCell.setBorderWidthTop(0.5f); // 璁剧疆涓婅竟妗嗗搴�
+            footerTable.addCell(hospitalNameCell);
+
+            // 鍒涘缓椤电爜鐨勫崟鍏冩牸锛氬彸瀵归綈
+//            String pageText = "绗� " + writer.getPageNumber() + 1 + " 椤碉紝鍏� " + writer.getPageNumber() + " 椤�"; // 椤电爜
+            String pageText = "绗� " + (writer.getPageNumber() + 1) + " 椤�"; // 椤电爜
+            PdfPCell pageCell = new PdfPCell(new Phrase(pageText, PdfUtils.setFont(8)));
+            pageCell.setHorizontalAlignment(Element.ALIGN_RIGHT); // 鍙冲榻�
+            pageCell.setVerticalAlignment(Element.ALIGN_TOP); // 鍨傜洿瀵归綈鍒板簳閮�
+            pageCell.setMinimumHeight(12f); // 璁剧疆鏈�灏忛珮搴︼紝閬垮厤楂樺害杩囧ぇ
+            pageCell.setBorder(Rectangle.TOP); // 璁剧疆涓婅竟妗�
+            pageCell.setBorderColor(BaseColor.LIGHT_GRAY); // 璁剧疆涓婅竟妗嗕负娣$伆鑹�
+            pageCell.setBorderWidthTop(0.5f); // 璁剧疆涓婅竟妗嗗搴�
+            footerTable.addCell(pageCell);
+
+            // 绗簩琛岋細绁濈璇紝鍚堝苟涓ゅ垪
+            PdfPCell footerTextCell = new PdfPCell(new Phrase("鏈綋妫�鎶ュ憡浠呬緵涓村簥鍙傝�冿紝涓嶄綔涓轰复搴婁緷鎹紝绁濇偍韬綋鍋ュ悍锛侊紒锛�", PdfUtils.setFont(8)));
+            footerTextCell.setColspan(2); // 鍚堝苟涓ゅ垪
+            footerTextCell.setHorizontalAlignment(Element.ALIGN_CENTER); // 灞呬腑
+            footerTextCell.setVerticalAlignment(Element.ALIGN_TOP); // 鍨傜洿瀵归綈鍒板簳閮�
+            footerTextCell.setMinimumHeight(14f); // 璁剧疆鏈�灏忛珮搴︼紝閬垮厤楂樺害杩囧ぇ
+            footerTextCell.setBorder(Rectangle.NO_BORDER); // 鍘婚櫎鎵�鏈夎竟妗�
+            footerTable.addCell(footerTextCell);
+
+            // 椤佃剼琛ㄦ牸鐨勪綅缃細灏嗚〃鏍煎啓鍏ラ〉鑴氾紝Y 鍧愭爣闇�瑕佽缃负璺濈椤甸潰搴曢儴鎸囧畾鐨勪綅缃�
+            footerTable.writeSelectedRows(0, -1, margin, footerPositionY, writer.getDirectContent());
+        }
+    }
 }

--
Gitblit v1.8.0