java 生成表格pdf文件

  1. package com.ucmed.auto.util;
  2. import java.io.FileOutputStream;
  3. import java.io.OutputStream;
  4. import java.util.Date;
  5. import net.sf.json.JSONArray;
  6. import net.sf.json.JSONObject;
  7. import org.apache.commons.lang.StringUtils;
  8. import com.itextpdf.text.BaseColor;
  9. import com.itextpdf.text.Chunk;
  10. import com.itextpdf.text.Document;
  11. import com.itextpdf.text.Element;
  12. import com.itextpdf.text.Font;
  13. import com.itextpdf.text.Image;
  14. import com.itextpdf.text.PageSize;
  15. import com.itextpdf.text.Paragraph;
  16. import com.itextpdf.text.pdf.BaseFont;
  17. import com.itextpdf.text.pdf.PdfPCell;
  18. import com.itextpdf.text.pdf.PdfPTable;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20. import com.ucmed.common.util.DateUtil;
  21. import com.ucmed.model.User;
  22. public class PrescriptionPdfUtilThree {
  23. public static void main(String[] args) {
  24. try {
  25. String filePath = "E:/1.pdf";
  26. OutputStream outputStream = new FileOutputStream(filePath);
  27. JSONObject patient = new JSONObject();
  28. JSONObject diagnosis = new JSONObject();
  29. patient.put("name", "宋小小");
  30. patient.put("sex", "男");
  31. patient.put("age", "24");
  32. patient.put("treatment_card", "B14684090");
  33. patient.put("phone", "13500000000");
  34. patient.put("department", "呼吸内科");
  35. diagnosis.put("before_diagnosis", "上呼吸道感染");
  36. User user = new User();
  37. user.setUserName("谢旭东");
  38. user.setLoginName("7165");
  39. createPdfContext(outputStream, patient, diagnosis, user, "E:/erCode2.png");
  40. System.out.println("生成成功了");
  41. } catch (Exception e) {
  42. System.out.println("生成失败");
  43. e.printStackTrace();
  44. }
  45. }
  46. public static void createPdfContext(OutputStream out, JSONObject patient, JSONObject diagnosis, User user, String codePath) throws Exception {
  47. Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
  48. Date currentDate = DateUtil.getCurrentDate();
  49. Font font;
  50. // BaseFont bfChinese = BaseFont.createFont("D:/java/workspace-tjpt/tjpt/tjpt/WebContent/resources/simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  51. // BaseFont bfChineseHuawen = BaseFont.createFont("D:/java/workspace-tjpt/tjpt/tjpt/WebContent/resources/huawenkt.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  52. BaseFont bfChinese = BaseFont.createFont("../../fonts/simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  53. BaseFont bfChineseHuawen = BaseFont.createFont("../../fonts/huawenkt.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  54. PdfPCell cell;
  55. Image image;
  56. //ConfigService configService = (ConfigService) ApplicationContextUtil.getBean("configService");
  57. PdfWriter.getInstance(doc, out);
  58. doc.open();
  59. // 标题
  60. font = new Font(bfChinese, 24, Font.BOLD);
  61. PdfPTable table = new PdfPTable(64);
  62. table.setWidthPercentage(95);
  63. PdfPTable table2 = new PdfPTable(11);
  64. cell = getPdfPCell(getTitleParagraph("浙江大学医学院附属第一医院", font, Element.TITLE), Element.ALIGN_RIGHT, bfChinese, 10);
  65. cell.setVerticalAlignment(Element.ALIGN_CENTER);
  66. table2.addCell(cell);
  67. table2.addCell(getPdfPCell("", Element.ALIGN_RIGHT, bfChinese, 1));
  68. cell = getPdfPCell(getTitleParagraph("互联网院区处方笺", font, Element.TITLE), Element.ALIGN_RIGHT, bfChinese, 9);
  69. cell.setVerticalAlignment(Element.ALIGN_CENTER);
  70. table2.addCell(cell);
  71. table2.addCell(getPdfPCell("", Element.ALIGN_RIGHT, bfChinese, 2));
  72. cell = new PdfPCell(table2);
  73. cell.setColspan(54);
  74. cell.setBorder(0);
  75. table.addCell(cell);
  76. //table2.addCell(getPdfPCell("", Element.TITLE, bfChinese, 3));
  77. font = new Font(bfChinese, 18, Font.ITALIC);
  78. cell = getPdfPCell("普 通 处 方", Element.ALIGN_LEFT, bfChineseHuawen, 10, font);
  79. cell.setBorderColor(new BaseColor(120, 120, 120));
  80. cell.setBorderWidthBottom(0.4f);
  81. cell.setBorderWidthTop(0.4f);
  82. cell.setBorderWidthLeft(0.4f);
  83. cell.setBorderWidthRight(0.4f);
  84. cell.setPaddingTop(-4f);
  85. cell.setPaddingLeft(6f);
  86. cell.setPaddingBottom(6f);
  87. table.addCell(cell);
  88. newlineTable(table, 24);
  89. doc.add(table);
  90. font = new Font(bfChinese, 11, Font.NORMAL);
  91. font.setColor(0, 0, 0);
  92. table = new PdfPTable(10);
  93. table.setWidthPercentage(89.6f);
  94. table.addCell(getPdfPCell("病历号:", Element.ALIGN_LEFT, bfChinese, 5));
  95. table.addCell(getPdfPCell("NO.H" + DateUtil.getyyyyMMdd(currentDate) + String.format("%03d", patient.optLong("id")), Element.ALIGN_RIGHT, bfChinese, 5));
  96. doc.add(table);
  97. //1、姓名、性别、年龄、费别
  98. table = new PdfPTable(8);
  99. table.setWidthPercentage(100);
  100. table.addCell(getPdfPCell("姓名:", Element.ALIGN_RIGHT, bfChinese, 1));
  101. table.addCell(getPdfPCell(StringUtils.isNotBlank(patient.optString("name")) ? patient.optString("name") : "", Element.ALIGN_LEFT, bfChinese, 1));
  102. table.addCell(getPdfPCell("性别:", Element.ALIGN_RIGHT, bfChinese, 1));
  103. table.addCell(getPdfPCell(StringUtils.isNotBlank(patient.optString("sex")) ? patient.optString("sex") : "", Element.ALIGN_LEFT, bfChinese, 1));
  104. table.addCell(getPdfPCell("年 龄:", Element.ALIGN_RIGHT, bfChinese, 1));
  105. table.addCell(getPdfPCell(StringUtils.isNotBlank(patient.optString("age")) ? patient.optString("age") : "", Element.ALIGN_LEFT, bfChinese, 1));
  106. table.addCell(getPdfPCell("费别:", Element.ALIGN_RIGHT, bfChinese, 1));
  107. table.addCell(getPdfPCell("自费", Element.ALIGN_LEFT, bfChinese, 1));
  108. doc.add(table);
  109. //2、就诊卡号、科别
  110. table = new PdfPTable(12);
  111. table.setWidthPercentage(100);
  112. table.addCell(getPdfPCell("就诊卡号:", Element.ALIGN_RIGHT, bfChinese, 2));
  113. table.addCell(getPdfPCell(StringUtils.isNotBlank(patient.optString("treatment_card")) ? patient.optString("treatment_card") : "", Element.ALIGN_LEFT, bfChinese, 4));
  114. table.addCell(getPdfPCell("科别(床号):" + ( StringUtils.isNotBlank(patient.optString("department")) ? patient.optString("department") : "" ), Element.ALIGN_RIGHT, bfChinese, 5));
  115. table.addCell(getPdfPCell("", Element.ALIGN_RIGHT, bfChinese, 1));
  116. doc.add(table);
  117. //3、 地址、手机号
  118. table = new PdfPTable(24);
  119. table.setWidthPercentage(90);
  120. table.addCell(getPdfPCell("地址:" + "", Element.ALIGN_LEFT, bfChinese, 14));
  121. table.addCell(getPdfPCell("手机号:" + ( StringUtils.isNotBlank(patient.optString("phone")) ? patient.optString("phone") : "" ), Element.ALIGN_RIGHT, bfChinese, 9));
  122. table.addCell(getPdfPCell("", Element.ALIGN_RIGHT, bfChinese, 1));
  123. doc.add(table);
  124. //4、 临床(初步)诊断
  125. table = new PdfPTable(40);
  126. table.setWidthPercentage(90);
  127. table.addCell(getPdfPCell("临床(初步)诊断:", Element.ALIGN_RIGHT, bfChinese, 9));
  128. table.addCell(getPdfPCell(StringUtils.isNotBlank(diagnosis.optString("before_diagnose")) ? diagnosis.optString("before_diagnose").replace("@@", "、") : "", Element.ALIGN_LEFT, bfChinese, 31));
  129. doc.add(table);
  130. //5、 过敏史
  131. table = new PdfPTable(180);
  132. table.setWidthPercentage(90);
  133. table.addCell(getPdfPCell("过敏史:", Element.ALIGN_RIGHT, bfChinese, 19));
  134. table.addCell(getPdfPCell(patient.optJSONObject("history") == null ? "" : patient.optJSONObject("history").optString("allergic_history"), Element.ALIGN_LEFT, bfChinese, 161));
  135. doc.add(table);
  136. //5、 处方日期
  137. table = new PdfPTable(6);
  138. table.setWidthPercentage(100);
  139. table.addCell(getPdfPCell("处方日期:", Element.ALIGN_RIGHT, bfChinese, 1));
  140. table.addCell(getPdfPCell(DateUtil.getyyyy_MM_dd(currentDate), Element.ALIGN_LEFT, bfChinese, 5));
  141. doc.add(table);
  142. //6、 Rp
  143. font = new Font(bfChinese, 26, Font.BOLD);
  144. font.setColor(0, 0, 0);
  145. doc.add(getTitleParagraph(" Rp", font, Element.ALIGN_LEFT));
  146. doc.add(new Paragraph("\n"));
  147. //7、处方内容
  148. table = new PdfPTable(12);
  149. table.setWidthPercentage(80);
  150. JSONArray drugList = diagnosis.optJSONArray("recipe_list") == null ? new JSONArray() : diagnosis.optJSONArray("recipe_list");
  151. int nullCount = 0;
  152. for (int i = 0; i < drugList.size(); i++) {
  153. JSONObject drug = drugList.optJSONObject(i);
  154. if(drug == null) {
  155. nullCount++;
  156. continue;
  157. }
  158. table.addCell(getPdfPCell(drug.optString("name"), Element.ALIGN_LEFT, bfChineseHuawen, 10, new Font(bfChineseHuawen, 14, Font.BOLD)));
  159. table.addCell(getPdfPCell(drug.optString("sl") + drug.optString("sldw"), Element.ALIGN_LEFT, bfChineseHuawen, 2, new Font(bfChineseHuawen, 14, Font.BOLD)));
  160. table.addCell(getPdfPCell("用法:", Element.ALIGN_RIGHT, bfChineseHuawen, 2));
  161. StringBuilder sbUsingMethod = new StringBuilder();
  162. if(StringUtils.isNotBlank(drug.optString("jl"))) {
  163. sbUsingMethod.append("每次" + drug.optString("jl") + drug.optString("ypjldw"));
  164. }
  165. if(StringUtils.isNotBlank(drug.optString("day")) && StringUtils.isNotBlank(drug.optString("pc"))) {
  166. sbUsingMethod.append(" " + drug.optString("pc") + " " + drug.optString("day") + "天");
  167. }
  168. if(StringUtils.isNotBlank(drug.optString("yf"))) {
  169. sbUsingMethod.append(" " + drug.optString("yf"));
  170. }
  171. table.addCell(getPdfPCell(sbUsingMethod.toString(), Element.ALIGN_LEFT, bfChineseHuawen, 10));
  172. }
  173. doc.add(table);
  174. //添加 18 - 2n行
  175. for(int i = 0; i < 16 - 2 * (drugList.size() - nullCount); i ++) {
  176. doc.add(new Paragraph("\n"));
  177. }
  178. //左侧、签名
  179. table = new PdfPTable(20);
  180. table.setWidthPercentage(88);
  181. table2 = new PdfPTable(1);
  182. newlineTable(table2, 1);
  183. newlineTable(table2, 1);
  184. newlineTable(table2, 1);
  185. newlineTable(table2, 1);
  186. newlineTable(table2, 1);
  187. if(StringUtils.isNotBlank(diagnosis.optString("sum")) && !"0".equals(diagnosis.optString("sum"))) {
  188. if(diagnosis.optString("sum").indexOf("元") > 0) {
  189. table2.addCell(getPdfPCell("药费:" + diagnosis.optString("sum"), Element.ALIGN_LEFT, bfChinese, 1));
  190. } else {
  191. table2.addCell(getPdfPCell("药费:" + diagnosis.optString("sum") + "元", Element.ALIGN_LEFT, bfChinese, 1));
  192. }
  193. } else {
  194. table2.addCell(getPdfPCell("药费:", Element.ALIGN_LEFT, bfChinese, 1));
  195. }
  196. table2.addCell(getPdfPCell("注射费:" + "", Element.ALIGN_LEFT, bfChinese, 1));
  197. table2.addCell(getPdfPCell("", Element.ALIGN_LEFT, bfChinese, 1));
  198. cell = new PdfPCell(table2);
  199. cell.setColspan(8);
  200. cell.setBorder(0);
  201. table.addCell(cell);
  202. table2 = new PdfPTable(1);
  203. newlineTable(table2, 1);
  204. newlineTable(table2, 1);
  205. newlineTable(table2, 1);
  206. newlineTable(table2, 1);
  207. newlineTable(table2, 1);
  208. if(user == null || !StringUtils.isNotBlank(user.getUserName())){
  209. table2.addCell(getPdfPCell("医师:" + patient.optString("yy_doctor") + "(" + patient.optString("doctor_id") + ")", Element.ALIGN_LEFT, bfChinese, 1));
  210. } else {
  211. table2.addCell(getPdfPCell("医师:" + user.getUserName() + "(" + user.getLoginName() + ")", Element.ALIGN_LEFT, bfChinese, 1));
  212. }
  213. if(StringUtils.isNotBlank(patient.optString("sh_doctor_name"))) {
  214. table2.addCell(getPdfPCell("审核药师:" + patient.optString("sh_doctor_name"), Element.ALIGN_LEFT, bfChinese, 1));
  215. } else {
  216. table2.addCell(getPdfPCell("审核药师:", Element.ALIGN_LEFT, bfChinese, 1));
  217. }
  218. table2.addCell(getPdfPCell("调配药师:" + "", Element.ALIGN_LEFT, bfChinese, 1));
  219. cell = new PdfPCell(table2);
  220. cell.setColspan(7);
  221. cell.setBorder(0);
  222. table.addCell(cell);
  223. //右侧、二维码
  224. table2 = new PdfPTable(1);
  225. image = Image.getInstance(codePath);
  226. int percent = getImagePercent(image.getHeight(), image.getWidth(), 120);
  227. image.scalePercent(percent);
  228. cell = getPdfPCellImage(image, 1, 0f);
  229. cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
  230. table2.addCell(cell);
  231. font = new Font(bfChinese, 11, Font.NORMAL);
  232. cell = getPdfPCell("扫一扫 查看电子版", Element.ALIGN_CENTER, bfChinese, 1, font);
  233. table2.addCell(cell);
  234. cell = new PdfPCell(table2);
  235. cell.setColspan(5);
  236. cell.setBorder(0);
  237. table.addCell(cell);
  238. doc.add(table);
  239. table = new PdfPTable(1);
  240. table.setWidthPercentage(95);
  241. table.addCell(getPdfPCell(getTitleParagraph("注:请勿遗失,处方当天有效。因特殊情况,该处方有效为 天(签名: )", new Font(bfChinese, 12, Font.NORMAL), Element.TITLE), Element.ALIGN_CENTER, bfChinese, 1));
  242. doc.add(table);
  243. doc.close();
  244. }
  245. /**
  246. * 获取pdf表格的单元格
  247. *
  248. * @param content
  249. * @param alignment
  250. * @param baseFont
  251. * @return
  252. */
  253. public static PdfPCell getPdfPCell(String content, int alignment,BaseFont baseFont, int colspan) {
  254. Font font = new Font(baseFont, 12, Font.NORMAL);
  255. Paragraph paragraph = new Paragraph(content, font);
  256. PdfPCell cell = new PdfPCell(paragraph);
  257. cell.setLeading(0, 1.5f);
  258. cell.setLeft(2);
  259. cell.setHorizontalAlignment(alignment);
  260. cell.setBorder(0);
  261. if (colspan > 1) {
  262. cell.setColspan(colspan);
  263. }
  264. return cell;
  265. }
  266. public static PdfPCell getPdfPCell(Paragraph paragraph, int alignment, BaseFont baseFont, int colspan) {
  267. // font.setColor(80, 80, 80);
  268. PdfPCell cell = new PdfPCell(paragraph);
  269. cell.setLeading(0, 1.5f);
  270. cell.setLeft(2);
  271. cell.setHorizontalAlignment(alignment);
  272. cell.setBorder(0);
  273. if (colspan > 1) {
  274. cell.setColspan(colspan);
  275. }
  276. return cell;
  277. }
  278. /**
  279. * 设置单元格,并可以设置字体大小
  280. *
  281. * @Description
  282. * @param content
  283. * @param alignment
  284. * @param baseFont
  285. * @param colspan
  286. * @param font
  287. * @return
  288. */
  289. public static PdfPCell getPdfPCell(String content, int alignment, BaseFont baseFont, int colspan, Font font) {
  290. // font.setColor(80, 80, 80);
  291. Paragraph paragraph = new Paragraph(content, font);
  292. PdfPCell cell = new PdfPCell(paragraph);
  293. cell.setLeading(0, 1.5f);
  294. cell.setLeft(2);
  295. cell.setHorizontalAlignment(alignment);
  296. cell.setBorder(0);
  297. if (colspan > 1) cell.setColspan(colspan);
  298. return cell;
  299. }
  300. /**
  301. * 获取pdf表格的单元格,并可以设置上面距离
  302. * @param content
  303. * @param alignment
  304. * @param baseFont
  305. * @return
  306. */
  307. public static PdfPCell getPdfPCell(String content, int alignment, BaseFont baseFont,int colspan, float paddingTop){
  308. Font font = new Font(baseFont, 11, Font.NORMAL);
  309. //font.setColor(80, 80, 80);
  310. Paragraph paragraph = new Paragraph(content, font);
  311. PdfPCell cell = new PdfPCell(paragraph);
  312. cell.setLeading(0, 1.5f);
  313. cell.setLeft(2);
  314. cell.setUseBorderPadding(true);
  315. cell.setPaddingTop(paddingTop);
  316. cell.setHorizontalAlignment(alignment);
  317. cell.setBorder(0);
  318. if(colspan > 1) cell.setColspan(colspan);
  319. return cell;
  320. }
  321. /**
  322. * 获取pdf表格的单元格
  323. *
  324. * @param content
  325. * @param alignment
  326. * @param baseFont
  327. * @return
  328. */
  329. public static PdfPCell getPdfPCell2(String content, int alignment,
  330. BaseFont baseFont, int colspan) {
  331. Font font = new Font(baseFont, 14, Font.NORMAL);
  332. font.setColor(59, 142, 222);
  333. Paragraph paragraph = new Paragraph(content, font);
  334. PdfPCell cell = new PdfPCell(paragraph);
  335. // cell.setLeading(0, 1.5f);
  336. cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
  337. cell.setLeft(2);
  338. cell.setHorizontalAlignment(alignment);
  339. cell.setBorder(0);
  340. if (colspan > 1)
  341. cell.setColspan(colspan);
  342. return cell;
  343. }
  344. /**
  345. * 获取pdf表格的图片单元格
  346. *
  347. * @param image
  348. * @param colspan
  349. * @param paddingTop
  350. * @return
  351. */
  352. public static PdfPCell getPdfPCellImage(Image image, int colspan, float paddingTop) {
  353. PdfPCell cell = new PdfPCell(image);
  354. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  355. cell.setUseBorderPadding(true);
  356. cell.setPaddingTop(paddingTop);
  357. cell.setBorder(0);
  358. if (colspan > 1) cell.setColspan(colspan);
  359. return cell;
  360. }
  361. /**
  362. * 获取pdf表格的图片单元格
  363. *
  364. * @param content
  365. * @param alignment
  366. * @param baseFont
  367. * @return
  368. */
  369. public static PdfPCell getPdfPCellImage2(Image image, int colspan, int align) {
  370. PdfPCell cell = new PdfPCell(image);
  371. cell.setHorizontalAlignment(align);
  372. cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
  373. cell.setBorder(0);
  374. if (colspan > 1)
  375. cell.setColspan(colspan);
  376. return cell;
  377. }
  378. /**
  379. * 标题文字
  380. *
  381. * @param title
  382. * @param font
  383. * @return
  384. */
  385. public static Paragraph getTitleParagraph(String title, Font font, int element) {
  386. Paragraph paragraph = new Paragraph(title, font);
  387. paragraph.setAlignment(element);
  388. return paragraph;
  389. }
  390. /**
  391. * 画线
  392. *
  393. * @return
  394. */
  395. public static Chunk getLine() {
  396. Chunk underline = new Chunk(
  397. " ");
  398. underline.setUnderline(new BaseColor(100, 100, 100), 0.1f, 0f, 1f, 0f,
  399. 0);
  400. return underline;
  401. }
  402. /**
  403. * 画线
  404. *
  405. * @return
  406. */
  407. public static Chunk getLine2() {
  408. Chunk underline = new Chunk(
  409. " ");
  410. underline.setUnderline(new BaseColor(100, 100, 100), 0.1f, 0f, 10f, 0f,
  411. 0);
  412. return underline;
  413. }
  414. /**
  415. * 画线
  416. *
  417. * @return
  418. */
  419. public static Chunk getLine2(BaseColor baseColor) {
  420. Chunk underline = new Chunk(
  421. " ");
  422. underline.setUnderline(baseColor, 0.1f, 0f, 10f, 0f,
  423. 0);
  424. return underline;
  425. }
  426. /**
  427. * pdfptable 换行
  428. *
  429. * @param table
  430. */
  431. public static void newlineTable(PdfPTable table) {
  432. PdfPCell cell = new PdfPCell(new Paragraph(" "));
  433. cell.setBorder(0);
  434. cell.setLeading(-1, 0);
  435. table.addCell(cell);
  436. table.addCell(cell);
  437. table.addCell(cell);
  438. table.addCell(cell);
  439. table.addCell(cell);
  440. table.addCell(cell);
  441. return;
  442. }
  443. /**
  444. * pdfptable 换行
  445. *
  446. * @param table
  447. */
  448. public static void newlineTable(PdfPTable table, int lineCount) {
  449. PdfPCell cell = new PdfPCell(new Paragraph(" "));
  450. cell.setBorder(0);
  451. cell.setLeading(-1, 0);
  452. for (int i = 0; i < lineCount; i++) {
  453. table.addCell(cell);
  454. }
  455. return;
  456. }
  457. /**
  458. * 第一种解决方案 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩
  459. *
  460. * @param h
  461. * @param w
  462. * @return
  463. */
  464. public static int getImagePercent(float h, float w, int standard) {
  465. int p = 0;
  466. float p2 = 100.0f;
  467. if (h >= w && h > standard) {
  468. p2 = standard / h * 100;
  469. }
  470. if (h < w && w > standard) {
  471. p2 = standard / w * 100;
  472. }
  473. p = Math.round(p2);
  474. return p;
  475. }
  476. }