From 0348f5a8184bd0a05434562f0021c2bb50628c32 Mon Sep 17 00:00:00 2001 From: zhaowenxuan <chacca165@163.com> Date: 星期一, 09 十二月 2024 13:41:20 +0800 Subject: [PATCH] 2024-12-09 --- ltkj-admin/src/main/java/com/ltkj/web/controller/his/HisPDFUtil.java | 471 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 400 insertions(+), 71 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 2a6a257..74d97e6 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,21 +23,27 @@ 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; import java.lang.reflect.Method; import java.math.BigDecimal; +import java.text.SimpleDateFormat; import java.util.*; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; /** * 妫�鏌� 妫�楠屾姤鍛妏df鐢熸垚 @@ -80,6 +91,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; @@ -103,7 +118,12 @@ } public AjaxResult hisPDF(TjOrder tjOrder, TjCustomer customer, TjReportTemplate tjReportTemplate) throws Exception { - HashMap<String, Object> pdfChangGui = makePDFInfo(tjOrder, customer, tjReportTemplate); + 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> map = makeHisJianYan(tjOrder, customer); + HashMap<String, Object> pdfChangGui = makePDFInfo(tjOrder, customer, tjReportTemplate,dictHosp,map); if (pdfChangGui == null) return AjaxResult.error(); Document document = (Document) pdfChangGui.get("document"); @@ -112,10 +132,24 @@ 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(); + log.error(String.valueOf(e)); return AjaxResult.error(); } document1.setMargins(70, 70, 40, 40); @@ -124,21 +158,19 @@ 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(); + log.error(String.valueOf(e)); return AjaxResult.error(); } // 缁勮妫�楠屾暟鎹� - 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()); @@ -162,7 +194,7 @@ copy.close(); } catch (IOException e) { log.error("鎷疯礉pdf寮傚父"); - e.printStackTrace(); + log.error(String.valueOf(e)); return AjaxResult.error(); } String outputFileName1 = customer.getCusId() + tjOrder.getTjNumber() + customer.getCusName() + "_鎶ュ憡.pdf"; @@ -171,22 +203,24 @@ 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(); finalOutPut = outputStream; } catch (IOException e) { - e.printStackTrace(); + log.error(String.valueOf(e)); return AjaxResult.error(); } savePDFFile(outputFileName1, tjOrder, customer, finalOutPut); @@ -212,7 +246,7 @@ elements.setSpacingAfter(10f); elements.setAlignment(Element.ALIGN_RIGHT); Phrase phrase = new Phrase(); - Font doctorFonts = PdfUtils.setFont(9); + Font doctorFonts = PdfUtils.setFont(11); for (LtkjGetexamreportinfo getexamreportinfo : list) { String xm = getexamreportinfo.getHzxm(); String xb = getexamreportinfo.getHzxb().equals("2") ? "濂�" : "鐢�"; @@ -377,10 +411,9 @@ * @param customer * @param finalOutPut */ - private void savePDFFile(String outputFileName1, TjOrder tjOrder, TjCustomer customer, ByteArrayOutputStream finalOutPut) throws IOException { - // 鏂囦欢閰嶇疆 -// String outputPath1 = "/Users/chacca/寮�鍙戠浉鍏�/浠g爜/"; - String outputPath1 = value + "\\"; + 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; outputStream = new BufferedOutputStream(new FileOutputStream(file)); @@ -388,7 +421,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(); } + + /** * 濉厖妫�楠岃〃鏍兼暟鎹� @@ -397,18 +453,19 @@ * @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(9); + Font doctorFonts = PdfUtils.setFont(11); String doctors = ""; Paragraph elements = new Paragraph(); elements.setLeading(5f); @@ -416,7 +473,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("|")) { @@ -442,7 +499,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 { @@ -451,9 +508,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); + } } } } @@ -465,7 +524,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()); @@ -479,6 +538,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 = {"浣撴椤圭洰", "浣撴缁撴灉", "鍗曚綅"}; // 鑾峰彇鍒楄〃鏁版嵁 //璁剧疆琛ㄥご瀛椾綋鏍峰紡 @@ -487,7 +565,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) { @@ -517,8 +595,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(); @@ -528,6 +608,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 = {"浣撴椤圭洰", "浣撴缁撴灉", "鍗曚綅"}; // 鑾峰彇鍒楄〃鏁版嵁 //璁剧疆琛ㄥご瀛椾綋鏍峰紡 @@ -536,7 +634,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) { @@ -565,9 +662,11 @@ } //鍒ゆ柇涓绘鍖诲笀鏄惁涓虹┖ 绌轰笉鑳芥樉绀簄ull zhuJianYiShi(document1, entry); - //鍒嗗壊绾� - LineSeparator objectName = new LineSeparator(); - document1.add(objectName); + if (!dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")) { + //鍒嗗壊绾� + LineSeparator objectName = new LineSeparator(); + document1.add(objectName); + } } } } @@ -634,7 +733,6 @@ LambdaQueryWrapper<LtkjHysqd> ltkjHysqdLambdaQueryWrapper = new LambdaQueryWrapper<>(); ltkjHysqdLambdaQueryWrapper.eq(LtkjHysqd::getTjh, tjOrder.getCardId()); List<LtkjHysqd> list = ltkjHysqdService.list(ltkjHysqdLambdaQueryWrapper); -// List<LtkjHysqd> list = ltkjHysqdService.list(); HashMap<String, Object> data = new HashMap<>(); for (LtkjHysqd hysqd : list) { LambdaQueryWrapper<LtkjHybgd> ltkjHybgdLambdaQueryWrapper = new LambdaQueryWrapper<>(); @@ -644,7 +742,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 +759,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 +770,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 +794,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 +803,10 @@ HashMap<String, Object> map = new HashMap<>(); map.put("data", lists); map.put("jyys", hashSet); + if (StrUtil.isNotBlank(shsj)){ + shsj = shsj.split(" ")[0]; + } + map.put("shsj",shsj); data.put(xmmc, map); } } @@ -710,7 +819,7 @@ * @param tjOrder * @param reportTemplate */ - public HashMap<String, Object> makePDFInfo(TjOrder tjOrder, TjCustomer tjCustomer, TjReportTemplate reportTemplate) { + public HashMap<String, Object> makePDFInfo(TjOrder tjOrder, TjCustomer tjCustomer, TjReportTemplate reportTemplate,DictHosp dictHosp,HashMap<String, Object> jianYanMap) { String tjNumber = tjOrder.getTjNumber(); String template = reportTemplate.getTemplate(); LambdaQueryWrapper<DictComp> wq111 = new LambdaQueryWrapper<>(); @@ -760,30 +869,70 @@ } //鎷兼帴閲嶅ぇ闃虫�х粨鏋� StringBuffer str = new StringBuffer(); - str.append("銆愰噸澶ч槼鎬ч」鐩�慭n"); - LambdaQueryWrapper<TjBigPositive> bigPositiveLambdaQueryWrapper = new LambdaQueryWrapper<>(); - bigPositiveLambdaQueryWrapper.eq(TjBigPositive::getTjNumber, tjNumber); - List<TjBigPositive> list1 = tjBigPositiveService.list(bigPositiveLambdaQueryWrapper); - if (list1 != null && list1.size() > 0) { - for (int i = 0; i < list1.size(); i++) { - str.append((i + 1) + "銆�" + list1.get(i).getProName() + ";\n"); + if (!dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")) { + str.append("銆愰噸澶ч槼鎬ч」鐩�慭n"); + LambdaQueryWrapper<TjBigPositive> bigPositiveLambdaQueryWrapper = new LambdaQueryWrapper<>(); + bigPositiveLambdaQueryWrapper.eq(TjBigPositive::getTjNumber, tjNumber); + List<TjBigPositive> list1 = tjBigPositiveService.list(bigPositiveLambdaQueryWrapper); + if (list1 != null && list1.size() > 0) { + for (int i = 0; i < list1.size(); i++) { + str.append((i + 1) + "銆�" + list1.get(i).getProName() + ";\n"); + } + } else { + str.append(" 鏃狅紱\n"); } - } else { - str.append(" 鏃狅紱\n"); } //鎷兼帴寮傚父椤圭洰 str.append("銆愬紓甯告儏鍐甸」鐩�慭n"); - if (list.size() > 0) { - for (int i = 0; i < list.size(); i++) { - if (list.get(i).getExceptionDesc() == 1) { - str.append((i + 1) + "銆�" + projectService.getById(list.get(i).getProId()).getProName() + ";\n"); + if (!dictHosp.getHospName().equals("婢勫悎鐭垮姟灞�涓績鍖婚櫌")) { + if (list.size() > 0) { + for (int i = 0; i < list.size(); i++) { + if (list.get(i).getExceptionDesc() == 1) { + str.append((i + 1) + "銆�" + projectService.getById(list.get(i).getProId()).getProName() + ";\n"); + } + } + } else { + str.append(" 鏃狅紱\n"); + } + }else { + int maxProjectLength = 0; + for (Map.Entry<String, Object> item : jianYanMap.entrySet()) { + HashMap<String, Object> entryValue = (HashMap<String, Object>) item.getValue(); + ArrayList<List<String>> lists = (ArrayList<List<String>>) entryValue.get("data"); + for (List<String> i : lists) { + maxProjectLength = Math.max(maxProjectLength, i.get(0).length()); } } - } else { - str.append(" 鏃狅紱\n"); + int projectWidth = maxProjectLength * 2; + int resultWidth = 8; + int flagWidth = 8; + int unitWidth = 8; + int rangeWidth = 15; + str.append(padRight("椤圭洰", projectWidth)) + .append(padRight("缁撴灉", resultWidth)) + .append(padRight("鏍囧織", flagWidth)) + .append(padRight("鍗曚綅", unitWidth)) + .append(padRight("鍙傝�冭寖鍥�", rangeWidth)) + .append("\n"); + for (Map.Entry<String, Object> item : jianYanMap.entrySet()) { + HashMap<String, Object> entryValue = (HashMap<String, Object>) item.getValue(); + ArrayList<List<String>> lists = (ArrayList<List<String>>) entryValue.get("data"); + for (List<String> i : lists) { + if (i.contains("鈫�") || i.contains("鈫�")) { + int currentProjectWidth = i.get(0).length() + (maxProjectLength - i.get(0).length()) * 2 + 2; + str.append(padRight(i.get(0), currentProjectWidth)); + str.append(padRight(i.get(1), resultWidth)); + str.append(padRight(i.get(2), flagWidth)); + str.append(padRight(i.get(3), unitWidth)); + str.append(padRight(i.get(4), rangeWidth)); + str.append("\n"); + } + } + } } //寮傚父椤圭洰 data.put("yichang", str.toString()); + data.put("zjrq",new SimpleDateFormat("yyyy-MM-dd").format(tjOrder.getCheckTime())); // 閬嶅巻data 缁檖df琛ㄥ崟琛ㄦ牸璧嬪�� for (String key : data.keySet()) { acroFields.setField(key, data.get(key).toString()); @@ -811,11 +960,20 @@ map.put("document", document); map.put("copy", copy); map.put("stream", finalOutPut); + map.put("form",pdfStamper); return map; } catch (IOException | DocumentException e) { e.printStackTrace(); return null; } + } + + public static String padRight(String str, int length) { + StringBuilder sb = new StringBuilder(str); + while (sb.length() < length) { + sb.append(" "); + } + return sb.toString(); } /** @@ -984,10 +1142,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); @@ -995,13 +1157,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); @@ -1010,15 +1193,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); } } @@ -1349,4 +1553,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