ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
java 生成表格pdf文件 --- ```java package com.ucmed.auto.util; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Date; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; 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.ucmed.common.util.DateUtil; import com.ucmed.model.User; public class PrescriptionPdfUtilThree { public static void main(String[] args) { try { String filePath = "E:/1.pdf"; OutputStream outputStream = new FileOutputStream(filePath); JSONObject patient = new JSONObject(); JSONObject diagnosis = new JSONObject(); patient.put("name", "宋小小"); patient.put("sex", "男"); patient.put("age", "24"); patient.put("treatment_card", "B14684090"); patient.put("phone", "13500000000"); patient.put("department", "呼吸内科"); diagnosis.put("before_diagnosis", "上呼吸道感染"); User user = new User(); user.setUserName("谢旭东"); user.setLoginName("7165"); createPdfContext(outputStream, patient, diagnosis, user, "E:/erCode2.png"); System.out.println("生成成功了"); } catch (Exception e) { System.out.println("生成失败"); e.printStackTrace(); } } public static void createPdfContext(OutputStream out, JSONObject patient, JSONObject diagnosis, User user, String codePath) throws Exception { Document doc = new Document(PageSize.A4, 20, 20, 20, 20); Date currentDate = DateUtil.getCurrentDate(); Font font; // BaseFont bfChinese = BaseFont.createFont("D:/java/workspace-tjpt/tjpt/tjpt/WebContent/resources/simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // BaseFont bfChineseHuawen = BaseFont.createFont("D:/java/workspace-tjpt/tjpt/tjpt/WebContent/resources/huawenkt.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); BaseFont bfChinese = BaseFont.createFont("../../fonts/simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); BaseFont bfChineseHuawen = BaseFont.createFont("../../fonts/huawenkt.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); PdfPCell cell; Image image; //ConfigService configService = (ConfigService) ApplicationContextUtil.getBean("configService"); PdfWriter.getInstance(doc, out); doc.open(); // 标题 font = new Font(bfChinese, 24, Font.BOLD); PdfPTable table = new PdfPTable(64); table.setWidthPercentage(95); PdfPTable table2 = new PdfPTable(11); cell = getPdfPCell(getTitleParagraph("浙江大学医学院附属第一医院", font, Element.TITLE), Element.ALIGN_RIGHT, bfChinese, 10); cell.setVerticalAlignment(Element.ALIGN_CENTER); table2.addCell(cell); table2.addCell(getPdfPCell("", Element.ALIGN_RIGHT, bfChinese, 1)); cell = getPdfPCell(getTitleParagraph("互联网院区处方笺", font, Element.TITLE), Element.ALIGN_RIGHT, bfChinese, 9); cell.setVerticalAlignment(Element.ALIGN_CENTER); table2.addCell(cell); table2.addCell(getPdfPCell("", Element.ALIGN_RIGHT, bfChinese, 2)); cell = new PdfPCell(table2); cell.setColspan(54); cell.setBorder(0); table.addCell(cell); //table2.addCell(getPdfPCell("", Element.TITLE, bfChinese, 3)); font = new Font(bfChinese, 18, Font.ITALIC); cell = getPdfPCell("普 通 处 方", Element.ALIGN_LEFT, bfChineseHuawen, 10, font); cell.setBorderColor(new BaseColor(120, 120, 120)); cell.setBorderWidthBottom(0.4f); cell.setBorderWidthTop(0.4f); cell.setBorderWidthLeft(0.4f); cell.setBorderWidthRight(0.4f); cell.setPaddingTop(-4f); cell.setPaddingLeft(6f); cell.setPaddingBottom(6f); table.addCell(cell); newlineTable(table, 24); doc.add(table); font = new Font(bfChinese, 11, Font.NORMAL); font.setColor(0, 0, 0); table = new PdfPTable(10); table.setWidthPercentage(89.6f); table.addCell(getPdfPCell("病历号:", Element.ALIGN_LEFT, bfChinese, 5)); table.addCell(getPdfPCell("NO.H" + DateUtil.getyyyyMMdd(currentDate) + String.format("%03d", patient.optLong("id")), Element.ALIGN_RIGHT, bfChinese, 5)); doc.add(table); //1、姓名、性别、年龄、费别 table = new PdfPTable(8); table.setWidthPercentage(100); table.addCell(getPdfPCell("姓名:", Element.ALIGN_RIGHT, bfChinese, 1)); table.addCell(getPdfPCell(StringUtils.isNotBlank(patient.optString("name")) ? patient.optString("name") : "", Element.ALIGN_LEFT, bfChinese, 1)); table.addCell(getPdfPCell("性别:", Element.ALIGN_RIGHT, bfChinese, 1)); table.addCell(getPdfPCell(StringUtils.isNotBlank(patient.optString("sex")) ? patient.optString("sex") : "", Element.ALIGN_LEFT, bfChinese, 1)); table.addCell(getPdfPCell("年 龄:", Element.ALIGN_RIGHT, bfChinese, 1)); table.addCell(getPdfPCell(StringUtils.isNotBlank(patient.optString("age")) ? patient.optString("age") : "", Element.ALIGN_LEFT, bfChinese, 1)); table.addCell(getPdfPCell("费别:", Element.ALIGN_RIGHT, bfChinese, 1)); table.addCell(getPdfPCell("自费", Element.ALIGN_LEFT, bfChinese, 1)); doc.add(table); //2、就诊卡号、科别 table = new PdfPTable(12); table.setWidthPercentage(100); table.addCell(getPdfPCell("就诊卡号:", Element.ALIGN_RIGHT, bfChinese, 2)); table.addCell(getPdfPCell(StringUtils.isNotBlank(patient.optString("treatment_card")) ? patient.optString("treatment_card") : "", Element.ALIGN_LEFT, bfChinese, 4)); table.addCell(getPdfPCell("科别(床号):" + ( StringUtils.isNotBlank(patient.optString("department")) ? patient.optString("department") : "" ), Element.ALIGN_RIGHT, bfChinese, 5)); table.addCell(getPdfPCell("", Element.ALIGN_RIGHT, bfChinese, 1)); doc.add(table); //3、 地址、手机号 table = new PdfPTable(24); table.setWidthPercentage(90); table.addCell(getPdfPCell("地址:" + "", Element.ALIGN_LEFT, bfChinese, 14)); table.addCell(getPdfPCell("手机号:" + ( StringUtils.isNotBlank(patient.optString("phone")) ? patient.optString("phone") : "" ), Element.ALIGN_RIGHT, bfChinese, 9)); table.addCell(getPdfPCell("", Element.ALIGN_RIGHT, bfChinese, 1)); doc.add(table); //4、 临床(初步)诊断 table = new PdfPTable(40); table.setWidthPercentage(90); table.addCell(getPdfPCell("临床(初步)诊断:", Element.ALIGN_RIGHT, bfChinese, 9)); table.addCell(getPdfPCell(StringUtils.isNotBlank(diagnosis.optString("before_diagnose")) ? diagnosis.optString("before_diagnose").replace("@@", "、") : "", Element.ALIGN_LEFT, bfChinese, 31)); doc.add(table); //5、 过敏史 table = new PdfPTable(180); table.setWidthPercentage(90); table.addCell(getPdfPCell("过敏史:", Element.ALIGN_RIGHT, bfChinese, 19)); table.addCell(getPdfPCell(patient.optJSONObject("history") == null ? "" : patient.optJSONObject("history").optString("allergic_history"), Element.ALIGN_LEFT, bfChinese, 161)); doc.add(table); //5、 处方日期 table = new PdfPTable(6); table.setWidthPercentage(100); table.addCell(getPdfPCell("处方日期:", Element.ALIGN_RIGHT, bfChinese, 1)); table.addCell(getPdfPCell(DateUtil.getyyyy_MM_dd(currentDate), Element.ALIGN_LEFT, bfChinese, 5)); doc.add(table); //6、 Rp font = new Font(bfChinese, 26, Font.BOLD); font.setColor(0, 0, 0); doc.add(getTitleParagraph(" Rp", font, Element.ALIGN_LEFT)); doc.add(new Paragraph("\n")); //7、处方内容 table = new PdfPTable(12); table.setWidthPercentage(80); JSONArray drugList = diagnosis.optJSONArray("recipe_list") == null ? new JSONArray() : diagnosis.optJSONArray("recipe_list"); int nullCount = 0; for (int i = 0; i < drugList.size(); i++) { JSONObject drug = drugList.optJSONObject(i); if(drug == null) { nullCount++; continue; } table.addCell(getPdfPCell(drug.optString("name"), Element.ALIGN_LEFT, bfChineseHuawen, 10, new Font(bfChineseHuawen, 14, Font.BOLD))); table.addCell(getPdfPCell(drug.optString("sl") + drug.optString("sldw"), Element.ALIGN_LEFT, bfChineseHuawen, 2, new Font(bfChineseHuawen, 14, Font.BOLD))); table.addCell(getPdfPCell("用法:", Element.ALIGN_RIGHT, bfChineseHuawen, 2)); StringBuilder sbUsingMethod = new StringBuilder(); if(StringUtils.isNotBlank(drug.optString("jl"))) { sbUsingMethod.append("每次" + drug.optString("jl") + drug.optString("ypjldw")); } if(StringUtils.isNotBlank(drug.optString("day")) && StringUtils.isNotBlank(drug.optString("pc"))) { sbUsingMethod.append(" " + drug.optString("pc") + " " + drug.optString("day") + "天"); } if(StringUtils.isNotBlank(drug.optString("yf"))) { sbUsingMethod.append(" " + drug.optString("yf")); } table.addCell(getPdfPCell(sbUsingMethod.toString(), Element.ALIGN_LEFT, bfChineseHuawen, 10)); } doc.add(table); //添加 18 - 2n行 for(int i = 0; i < 16 - 2 * (drugList.size() - nullCount); i ++) { doc.add(new Paragraph("\n")); } //左侧、签名 table = new PdfPTable(20); table.setWidthPercentage(88); table2 = new PdfPTable(1); newlineTable(table2, 1); newlineTable(table2, 1); newlineTable(table2, 1); newlineTable(table2, 1); newlineTable(table2, 1); if(StringUtils.isNotBlank(diagnosis.optString("sum")) && !"0".equals(diagnosis.optString("sum"))) { if(diagnosis.optString("sum").indexOf("元") > 0) { table2.addCell(getPdfPCell("药费:" + diagnosis.optString("sum"), Element.ALIGN_LEFT, bfChinese, 1)); } else { table2.addCell(getPdfPCell("药费:" + diagnosis.optString("sum") + "元", Element.ALIGN_LEFT, bfChinese, 1)); } } else { table2.addCell(getPdfPCell("药费:", Element.ALIGN_LEFT, bfChinese, 1)); } table2.addCell(getPdfPCell("注射费:" + "", Element.ALIGN_LEFT, bfChinese, 1)); table2.addCell(getPdfPCell("", Element.ALIGN_LEFT, bfChinese, 1)); cell = new PdfPCell(table2); cell.setColspan(8); cell.setBorder(0); table.addCell(cell); table2 = new PdfPTable(1); newlineTable(table2, 1); newlineTable(table2, 1); newlineTable(table2, 1); newlineTable(table2, 1); newlineTable(table2, 1); if(user == null || !StringUtils.isNotBlank(user.getUserName())){ table2.addCell(getPdfPCell("医师:" + patient.optString("yy_doctor") + "(" + patient.optString("doctor_id") + ")", Element.ALIGN_LEFT, bfChinese, 1)); } else { table2.addCell(getPdfPCell("医师:" + user.getUserName() + "(" + user.getLoginName() + ")", Element.ALIGN_LEFT, bfChinese, 1)); } if(StringUtils.isNotBlank(patient.optString("sh_doctor_name"))) { table2.addCell(getPdfPCell("审核药师:" + patient.optString("sh_doctor_name"), Element.ALIGN_LEFT, bfChinese, 1)); } else { table2.addCell(getPdfPCell("审核药师:", Element.ALIGN_LEFT, bfChinese, 1)); } table2.addCell(getPdfPCell("调配药师:" + "", Element.ALIGN_LEFT, bfChinese, 1)); cell = new PdfPCell(table2); cell.setColspan(7); cell.setBorder(0); table.addCell(cell); //右侧、二维码 table2 = new PdfPTable(1); image = Image.getInstance(codePath); int percent = getImagePercent(image.getHeight(), image.getWidth(), 120); image.scalePercent(percent); cell = getPdfPCellImage(image, 1, 0f); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); table2.addCell(cell); font = new Font(bfChinese, 11, Font.NORMAL); cell = getPdfPCell("扫一扫 查看电子版", Element.ALIGN_CENTER, bfChinese, 1, font); table2.addCell(cell); cell = new PdfPCell(table2); cell.setColspan(5); cell.setBorder(0); table.addCell(cell); doc.add(table); table = new PdfPTable(1); table.setWidthPercentage(95); table.addCell(getPdfPCell(getTitleParagraph("注:请勿遗失,处方当天有效。因特殊情况,该处方有效为 天(签名: )", new Font(bfChinese, 12, Font.NORMAL), Element.TITLE), Element.ALIGN_CENTER, bfChinese, 1)); doc.add(table); doc.close(); } /** * 获取pdf表格的单元格 * * @param content * @param alignment * @param baseFont * @return */ public static PdfPCell getPdfPCell(String content, int alignment,BaseFont baseFont, int colspan) { Font font = new Font(baseFont, 12, Font.NORMAL); Paragraph paragraph = new Paragraph(content, font); PdfPCell cell = new PdfPCell(paragraph); cell.setLeading(0, 1.5f); cell.setLeft(2); cell.setHorizontalAlignment(alignment); cell.setBorder(0); if (colspan > 1) { cell.setColspan(colspan); } return cell; } public static PdfPCell getPdfPCell(Paragraph paragraph, int alignment, BaseFont baseFont, int colspan) { // font.setColor(80, 80, 80); PdfPCell cell = new PdfPCell(paragraph); cell.setLeading(0, 1.5f); cell.setLeft(2); cell.setHorizontalAlignment(alignment); cell.setBorder(0); if (colspan > 1) { cell.setColspan(colspan); } return cell; } /** * 设置单元格,并可以设置字体大小 * * @Description * @param content * @param alignment * @param baseFont * @param colspan * @param font * @return */ public static PdfPCell getPdfPCell(String content, int alignment, BaseFont baseFont, int colspan, Font font) { // font.setColor(80, 80, 80); Paragraph paragraph = new Paragraph(content, font); PdfPCell cell = new PdfPCell(paragraph); cell.setLeading(0, 1.5f); cell.setLeft(2); cell.setHorizontalAlignment(alignment); cell.setBorder(0); if (colspan > 1) cell.setColspan(colspan); return cell; } /** * 获取pdf表格的单元格,并可以设置上面距离 * @param content * @param alignment * @param baseFont * @return */ public static PdfPCell getPdfPCell(String content, int alignment, BaseFont baseFont,int colspan, float paddingTop){ Font font = new Font(baseFont, 11, Font.NORMAL); //font.setColor(80, 80, 80); Paragraph paragraph = new Paragraph(content, font); PdfPCell cell = new PdfPCell(paragraph); cell.setLeading(0, 1.5f); cell.setLeft(2); cell.setUseBorderPadding(true); cell.setPaddingTop(paddingTop); cell.setHorizontalAlignment(alignment); cell.setBorder(0); if(colspan > 1) cell.setColspan(colspan); return cell; } /** * 获取pdf表格的单元格 * * @param content * @param alignment * @param baseFont * @return */ public static PdfPCell getPdfPCell2(String content, int alignment, BaseFont baseFont, int colspan) { Font font = new Font(baseFont, 14, Font.NORMAL); font.setColor(59, 142, 222); Paragraph paragraph = new Paragraph(content, font); PdfPCell cell = new PdfPCell(paragraph); // cell.setLeading(0, 1.5f); cell.setVerticalAlignment(Element.ALIGN_BOTTOM); cell.setLeft(2); cell.setHorizontalAlignment(alignment); cell.setBorder(0); if (colspan > 1) cell.setColspan(colspan); return cell; } /** * 获取pdf表格的图片单元格 * * @param image * @param colspan * @param paddingTop * @return */ public static PdfPCell getPdfPCellImage(Image image, int colspan, float paddingTop) { PdfPCell cell = new PdfPCell(image); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setUseBorderPadding(true); cell.setPaddingTop(paddingTop); cell.setBorder(0); if (colspan > 1) cell.setColspan(colspan); return cell; } /** * 获取pdf表格的图片单元格 * * @param content * @param alignment * @param baseFont * @return */ public static PdfPCell getPdfPCellImage2(Image image, int colspan, int align) { PdfPCell cell = new PdfPCell(image); cell.setHorizontalAlignment(align); cell.setVerticalAlignment(Element.ALIGN_BOTTOM); cell.setBorder(0); if (colspan > 1) cell.setColspan(colspan); return cell; } /** * 标题文字 * * @param title * @param font * @return */ public static Paragraph getTitleParagraph(String title, Font font, int element) { Paragraph paragraph = new Paragraph(title, font); paragraph.setAlignment(element); return paragraph; } /** * 画线 * * @return */ public static Chunk getLine() { Chunk underline = new Chunk( " "); underline.setUnderline(new BaseColor(100, 100, 100), 0.1f, 0f, 1f, 0f, 0); return underline; } /** * 画线 * * @return */ public static Chunk getLine2() { Chunk underline = new Chunk( " "); underline.setUnderline(new BaseColor(100, 100, 100), 0.1f, 0f, 10f, 0f, 0); return underline; } /** * 画线 * * @return */ public static Chunk getLine2(BaseColor baseColor) { Chunk underline = new Chunk( " "); underline.setUnderline(baseColor, 0.1f, 0f, 10f, 0f, 0); return underline; } /** * pdfptable 换行 * * @param table */ public static void newlineTable(PdfPTable table) { PdfPCell cell = new PdfPCell(new Paragraph(" ")); cell.setBorder(0); cell.setLeading(-1, 0); table.addCell(cell); table.addCell(cell); table.addCell(cell); table.addCell(cell); table.addCell(cell); table.addCell(cell); return; } /** * pdfptable 换行 * * @param table */ public static void newlineTable(PdfPTable table, int lineCount) { PdfPCell cell = new PdfPCell(new Paragraph(" ")); cell.setBorder(0); cell.setLeading(-1, 0); for (int i = 0; i < lineCount; i++) { table.addCell(cell); } return; } /** * 第一种解决方案 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩 * * @param h * @param w * @return */ public static int getImagePercent(float h, float w, int standard) { int p = 0; float p2 = 100.0f; if (h >= w && h > standard) { p2 = standard / h * 100; } if (h < w && w > standard) { p2 = standard / w * 100; } p = Math.round(p2); return p; } } ```