PdfUtil.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. package com.xunmei.file.utils;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import cn.hutool.extra.spring.SpringUtil;
  4. import com.lowagie.text.*;
  5. import com.lowagie.text.pdf.*;
  6. import com.xunmei.file.vo.ItextPdfTableVo;
  7. import com.xunmei.file.vo.PdfFilePathVo;
  8. import com.xunmei.system.api.domain.CheckDataVo;
  9. import com.xunmei.system.api.domain.ResumptionPdf;
  10. import com.xunmei.system.api.domain.SafeCheckTaskRegisterBookVo;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.context.ApplicationContext;
  15. import org.springframework.core.io.Resource;
  16. import java.io.*;
  17. import java.util.*;
  18. import java.util.List;
  19. @Slf4j
  20. public class PdfUtil {
  21. public static ItextPdfTableVo createTable(String filename, int numColumns, int fontSize) throws Exception {
  22. Document document = new Document(PageSize.A4, 0, 0, 50, 0);//SUPPRESS
  23. FileOutputStream fos = new FileOutputStream(filename);
  24. log.info("filename,{}", filename);
  25. final PdfWriter writer = PdfWriter.getInstance(document, fos);
  26. document.open();
  27. // 使用语言包字
  28. BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  29. // 外部字体
  30. BaseFont fs = BaseFont.createFont("/fonts/msyh.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  31. Font tableFont = new Font(fs, fontSize, Font.NORMAL);
  32. PdfPTable table = new PdfPTable(numColumns);
  33. // 设置各列列宽
  34. // table.setTotalWidth(new float[]{90, 100, 100, 120, 100, 100});
  35. table.setSpacingBefore(16f);
  36. table.setSplitRows(false);
  37. table.setSplitLate(true);
  38. ItextPdfTableVo itextPdfTableVo = new ItextPdfTableVo();
  39. itextPdfTableVo.setDocument(document);
  40. itextPdfTableVo.setWriter(writer);
  41. itextPdfTableVo.setAbf(abf);
  42. itextPdfTableVo.setFs(fs);
  43. itextPdfTableVo.setTableFont(tableFont);
  44. itextPdfTableVo.setTable(table);
  45. return itextPdfTableVo;
  46. }
  47. public static PdfPCell createPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan) {
  48. final PdfPCell cell = cell(tableFont, content, align, colspan, rowspan);
  49. table.addCell(cell);
  50. return cell;
  51. }
  52. public static PdfPCell createPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan, Integer lineSpacing) {
  53. final PdfPCell cell = cell(tableFont, content, align, colspan, rowspan, lineSpacing);
  54. table.addCell(cell);
  55. return cell;
  56. }
  57. //底部无边框的单元格
  58. public static PdfPCell createPDFCellWithoutBorder(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan) {
  59. final PdfPCell cell = cell(tableFont, content, align, colspan, rowspan);
  60. cell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
  61. table.addCell(cell);
  62. return cell;
  63. }
  64. private static PdfPCell cell(Font tableFont, String content, int align, Integer colspan, Integer rowspan) {
  65. PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
  66. if (colspan != null && colspan > 0) {
  67. cell.setColspan(colspan);
  68. }
  69. if (rowspan != null && rowspan > 0) {
  70. cell.setRowspan(rowspan);
  71. }
  72. if (PdfPCell.ALIGN_MIDDLE != align) {
  73. cell.setHorizontalAlignment(align);
  74. cell.setPaddingLeft(8f);
  75. cell.setPaddingRight(8f);
  76. cell.setPaddingBottom(8f);
  77. } else {
  78. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//设置单元格的垂直对齐方式
  79. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  80. }
  81. return cell;
  82. }
  83. private static PdfPCell cell(Font tableFont, String content, int align, Integer colspan, Integer rowspan, Integer lineSpacing) {
  84. PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
  85. if (colspan != null && colspan > 0) {
  86. cell.setColspan(colspan);
  87. }
  88. if (rowspan != null && rowspan > 0) {
  89. cell.setRowspan(rowspan);
  90. }
  91. cell.setLeading(lineSpacing, 0);
  92. if (PdfPCell.ALIGN_MIDDLE != align) {
  93. cell.setHorizontalAlignment(align);
  94. cell.setPaddingLeft(8f);
  95. cell.setPaddingRight(8f);
  96. cell.setPaddingBottom(8f);
  97. } else {
  98. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//设置单元格的垂直对齐方式
  99. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  100. }
  101. return cell;
  102. }
  103. /**
  104. * 生成文字类型的水印
  105. */
  106. public static void createWatermark(Document document, PdfWriter writer, Font waterMarkFont, String content) throws DocumentException, IOException {
  107. PdfContentByte waterMarkPdfContent = writer.getDirectContentUnder();
  108. Phrase phrase = new Phrase(content, waterMarkFont);
  109. float pageWidth = document.right() + document.left();//获取pdf内容正文页面宽度
  110. float pageHeight = document.top() + document.bottom();//获取pdf内容正文页面高度
  111. //两行三列
  112. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  113. pageWidth * 0.25f, pageHeight * 0.2f, 45);
  114. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  115. pageWidth * 0.25f, pageHeight * 0.5f, 45);
  116. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  117. pageWidth * 0.25f, pageHeight * 0.8f, 45);
  118. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  119. pageWidth * 0.65f, pageHeight * 0.2f, 45);
  120. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  121. pageWidth * 0.65f, pageHeight * 0.5f, 45);
  122. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  123. pageWidth * 0.65f, pageHeight * 0.8f, 45);
  124. }
  125. public static void dealHeader(Document document, BaseFont fs, String title, int fontSize) throws DocumentException {
  126. //文件title
  127. Paragraph p = new Paragraph(title, new Font(fs, fontSize, Font.NORMAL));
  128. p.setAlignment(Paragraph.ALIGN_CENTER);
  129. // 文件title 段落下空白
  130. p.setSpacingAfter(10f);
  131. p.setAlignment(Element.ALIGN_CENTER);
  132. document.add(p);
  133. }
  134. public static void dealEduBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  135. //第一行
  136. createPDFCell(tableFont, table, "时间", Element.ALIGN_CENTER, 1, 1);
  137. createPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_CENTER, 2, 1);
  138. createPDFCell(tableFont, table, "地点", Element.ALIGN_CENTER, 1, 1);
  139. createPDFCell(tableFont, table, data.get("address").toString(), Element.ALIGN_CENTER, 2, 1);
  140. //第二行
  141. createPDFCell(tableFont, table, "主持人", Element.ALIGN_CENTER, 1, 1);
  142. createPDFCell(tableFont, table, data.get("hostName").toString(), Element.ALIGN_CENTER, 2, 1);
  143. createPDFCell(tableFont, table, "记录人", Element.ALIGN_CENTER, 1, 1);
  144. createPDFCell(tableFont, table, data.get("recorderName").toString(), Element.ALIGN_CENTER, 2, 1);
  145. //内容
  146. PdfPCell contentCell = new PdfPCell();
  147. contentCell.setColspan(6);
  148. Paragraph content = new Paragraph();
  149. String text = data.get("content").toString();
  150. content.add(new Chunk(text, tableFont));
  151. contentCell.addElement(content);
  152. contentCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  153. table.addCell(contentCell);
  154. table.setSplitLate(false);
  155. table.setSplitRows(true);
  156. //总结
  157. PdfPCell noteCell = new PdfPCell();
  158. noteCell.setColspan(6);
  159. Paragraph paragraph = new Paragraph();
  160. String noteText = data.get("note").toString();
  161. paragraph.add(new Chunk(noteText, tableFont));
  162. noteCell.addElement(paragraph);
  163. noteCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  164. table.addCell(noteCell);
  165. //签字区域
  166. PdfPCell signCell = new PdfPCell();
  167. signCell.setColspan(6);
  168. Paragraph signParagraph = new Paragraph();
  169. signParagraph.add(new Chunk("参会人员签字:", tableFont));
  170. signCell.addElement(signParagraph);
  171. signCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
  172. table.addCell(signCell);
  173. //图片填充
  174. final PdfPTable imageTable = getImage((List<String>) data.get("image"), 12,100,100,5);
  175. final PdfPCell cell = new PdfPCell();
  176. cell.setNoWrap(false);
  177. cell.setPaddingLeft(8f);
  178. cell.setPaddingRight(8f);
  179. cell.setPaddingBottom(8f);
  180. cell.setPaddingTop(8f);
  181. cell.setColspan(6);
  182. cell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  183. cell.addElement(imageTable);
  184. table.addCell(cell);
  185. /* //第三行
  186. createPDFCell(tableFont, table, "参会人员签字", Element.ALIGN_CENTER, 1, 1);
  187. createPDFCell(tableFont, table, data.get("users").toString(), Element.ALIGN_CENTER, 5, 1);*/
  188. document.add(table);
  189. }
  190. public static void dealResumptionBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  191. PdfUtil.createPDFCell(tableFont, table, "检查内容", Element.ALIGN_CENTER, 6, 0);
  192. PdfUtil.createPDFCell(tableFont, table, "检查情况", Element.ALIGN_CENTER, 0, 0);
  193. PdfUtil.createPDFCell(tableFont, table, "登记人", Element.ALIGN_CENTER, 0, 0);
  194. List<String> names = new ArrayList<>();
  195. names.add("营业前");
  196. names.add("营业期间");
  197. names.add("营业终了");
  198. for (String s : names) {
  199. List<LinkedHashMap<String, Object>> lists = (List<LinkedHashMap<String, Object>>) data.get(s);
  200. if (ObjectUtil.isEmpty(lists)) {
  201. continue;
  202. }
  203. //不同的执行时刻
  204. PdfUtil.createPDFCell(tableFont, table, getLineStr(s), PdfPCell.ALIGN_MIDDLE, 0, lists.size());
  205. int o = 1;
  206. for (LinkedHashMap<String, Object> listVo : lists) {
  207. // 检查内容
  208. String rowContent = o + "、" + listVo.get("pointName");
  209. PdfUtil.createPDFCell(tableFont, table, rowContent, Element.ALIGN_LEFT, 5, 0);
  210. // 检查情况
  211. PdfUtil.createPDFCell(tableFont, table, ((Integer) listVo.get("resValue")) == 0 ? "√" : "×", Element.ALIGN_CENTER, 0, 0);
  212. // 检查人
  213. PdfUtil.createPDFCell(tableFont, table, String.valueOf(listVo.get("submitName")), Element.ALIGN_CENTER, 0, 0);
  214. o++;
  215. }
  216. }
  217. // 备注数据
  218. PdfUtil.createPDFCell(tableFont, table, getLineStr("备注"), PdfPCell.ALIGN_MIDDLE, 0, 0);
  219. PdfUtil.createPDFCell(tableFont, table, data.get("remark").toString(), Element.ALIGN_LEFT, 7, 0);
  220. document.add(table);
  221. Paragraph foot = new Paragraph(" 注:检查情况正常打“√”;发现问题打“×”,并在备注中具体说明。", tableFont);
  222. // Paragraph foot = new Paragraph(" 注:检查情况正常打“√”;发现问题打“×”,并在备注中具体说明。", new Font(fs, 8, Font.NORMAL));
  223. foot.setAlignment(Paragraph.ALIGN_LEFT);
  224. //在后方加入16个空格
  225. document.add(foot);
  226. }
  227. public static String getLineStr(String str) {
  228. StringBuilder result = new StringBuilder();
  229. for (int i = 0; i < str.length(); i++) {
  230. result.append(str.charAt(i)).append("\r");
  231. }
  232. return result.toString();
  233. }
  234. public static void dealOutInBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  235. table.setSplitLate(false);
  236. table.setSplitRows(true);
  237. //第一行
  238. createPDFCell(tableFont, table, "接待机构", Element.ALIGN_CENTER, 1, 1);
  239. createPDFCell(tableFont, table, data.get("inOrg").toString(), Element.ALIGN_CENTER, 2, 1);
  240. createPDFCell(tableFont, table, "接待日期", Element.ALIGN_CENTER, 1, 1);
  241. createPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_CENTER, 2, 1);
  242. //第二行
  243. createPDFCell(tableFont, table, "来访事由", Element.ALIGN_CENTER, 1, 1);
  244. createPDFCell(tableFont, table, data.get("reasons").toString(), Element.ALIGN_CENTER, 2, 1);
  245. createPDFCell(tableFont, table, "审批人", Element.ALIGN_CENTER, 1, 1);
  246. createPDFCell(tableFont, table, data.get("approveUser").toString(), Element.ALIGN_CENTER, 2, 1);
  247. //第三行
  248. createPDFCell(tableFont, table, "来访单位", Element.ALIGN_CENTER, 1, 1);
  249. createPDFCell(tableFont, table, data.get("outOrgName").toString(), Element.ALIGN_CENTER, 2, 1);
  250. createPDFCell(tableFont, table, "来访人员", Element.ALIGN_CENTER, 1, 1);
  251. createPDFCell(tableFont, table, data.get("userName").toString(), Element.ALIGN_CENTER, 2, 1);
  252. //第四行
  253. createPDFCell(tableFont, table, "证件类型", Element.ALIGN_CENTER, 1, 1);
  254. createPDFCell(tableFont, table, data.get("idType").toString(), Element.ALIGN_CENTER, 2, 1);
  255. createPDFCell(tableFont, table, "证件号码", Element.ALIGN_CENTER, 1, 1);
  256. createPDFCell(tableFont, table, data.get("idCard").toString(), Element.ALIGN_CENTER, 2, 1);
  257. //第五行
  258. createPDFCell(tableFont, table, "进入时间", Element.ALIGN_CENTER, 1, 1);
  259. createPDFCell(tableFont, table, data.get("inTime").toString(), Element.ALIGN_CENTER, 2, 1);
  260. createPDFCell(tableFont, table, "离开时间", Element.ALIGN_CENTER, 1, 1);
  261. createPDFCell(tableFont, table, data.get("outTime").toString(), Element.ALIGN_CENTER, 2, 1);
  262. // /statics/2023/12/05/20231205183106A001.png
  263. //证件图片
  264. createPDFCell(tableFont, table, "证件图片", Element.ALIGN_CENTER, 1, 70);
  265. //演练情况 图片填充
  266. final PdfPTable imageTable1 = getImage((List<String>) data.get("imageFile"), 10,80,80,4);
  267. final PdfPCell cell1 = new PdfPCell();
  268. cell1.setNoWrap(false);
  269. cell1.setPaddingLeft(8f);
  270. cell1.setPaddingRight(8f);
  271. cell1.setPaddingBottom(8f);
  272. cell1.setPaddingTop(8f);
  273. cell1.setColspan(5);
  274. cell1.setRowspan(70);
  275. //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  276. cell1.addElement(imageTable1);
  277. table.addCell(cell1);
  278. createPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_CENTER, 1, 70);
  279. final PdfPTable imageTable2 = getImage((List<String>) data.get("file"), 10,80,80,4);
  280. final PdfPCell cell2 = new PdfPCell();
  281. cell2.setNoWrap(false);
  282. cell2.setPaddingLeft(8f);
  283. cell2.setPaddingRight(8f);
  284. cell2.setPaddingBottom(8f);
  285. cell2.setPaddingTop(8f);
  286. cell2.setColspan(5);
  287. cell2.setRowspan(70);
  288. //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  289. cell2.addElement(imageTable2);
  290. table.addCell(cell2);
  291. createPDFCell(tableFont, table, "身份核验材料", Element.ALIGN_CENTER, 1, 70);
  292. final PdfPTable imageTable3 = getImage((List<String>) data.get("checkImage"), 10,80,80,4);
  293. final PdfPCell cell3 = new PdfPCell();
  294. cell3.setNoWrap(false);
  295. cell3.setPaddingLeft(8f);
  296. cell3.setPaddingRight(8f);
  297. cell3.setPaddingBottom(8f);
  298. cell3.setPaddingTop(8f);
  299. cell3.setColspan(5);
  300. cell3.setRowspan(70);
  301. //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  302. cell3.addElement(imageTable3);
  303. table.addCell(cell3);
  304. document.add(table);
  305. }
  306. public static void dealDrillBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  307. table.setSplitLate(false);
  308. table.setSplitRows(true);
  309. //第一行
  310. createPDFCell(tableFont, table, "演练单位", Element.ALIGN_CENTER, 1, 1);
  311. createPDFCell(tableFont, table, data.get("orgName").toString(), Element.ALIGN_CENTER, 2, 1);
  312. createPDFCell(tableFont, table, "地点", Element.ALIGN_CENTER, 1, 1);
  313. createPDFCell(tableFont, table, data.get("drillSite").toString(), Element.ALIGN_CENTER, 2, 1);
  314. //第二行
  315. createPDFCell(tableFont, table, "演练时间", Element.ALIGN_CENTER, 1, 1);
  316. createPDFCell(tableFont, table, data.get("drillTime").toString(), Element.ALIGN_CENTER, 2, 1);
  317. createPDFCell(tableFont, table, "指挥人", Element.ALIGN_CENTER, 1, 1);
  318. createPDFCell(tableFont, table, data.get("hostName").toString(), Element.ALIGN_CENTER, 2, 1);
  319. //第三行
  320. createPDFCell(tableFont, table, "演练项目", Element.ALIGN_CENTER, 1, 1);
  321. createPDFCell(tableFont, table, data.get("typeText").toString(), Element.ALIGN_CENTER, 5, 1);
  322. //预设案由
  323. PdfPCell contentCell = new PdfPCell();
  324. contentCell.setColspan(6);
  325. Paragraph content = new Paragraph();
  326. String text = data.get("presetCase").toString();
  327. content.add(new Chunk(text, tableFont));
  328. contentCell.addElement(content);
  329. contentCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  330. table.addCell(contentCell);
  331. //演练情况
  332. PdfPCell noteCell = new PdfPCell();
  333. noteCell.setColspan(6);
  334. Paragraph paragraph = new Paragraph();
  335. String noteText = data.get("drillSituation").toString();
  336. paragraph.add(new Chunk(noteText, tableFont));
  337. noteCell.addElement(paragraph);
  338. noteCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
  339. table.addCell(noteCell);
  340. //演练情况 图片填充
  341. final PdfPTable imageTable1 = getImage((List<String>) data.get("imageData"), 6,100,100,3);
  342. final PdfPCell cell1 = new PdfPCell();
  343. cell1.setNoWrap(false);
  344. cell1.setPaddingLeft(8f);
  345. cell1.setPaddingRight(8f);
  346. cell1.setPaddingBottom(8f);
  347. cell1.setPaddingTop(8f);
  348. cell1.setColspan(6);
  349. cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  350. cell1.addElement(imageTable1);
  351. table.addCell(cell1);
  352. //签字区域
  353. PdfPCell signCell = new PdfPCell();
  354. signCell.setColspan(6);
  355. Paragraph signParagraph = new Paragraph();
  356. signParagraph.add(new Chunk("参会演练人员(签字):", tableFont));
  357. signCell.addElement(signParagraph);
  358. signCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
  359. table.addCell(signCell);
  360. //图片填充
  361. final PdfPTable imageTable = getImage((List<String>) data.get("image"), 12,50,50,5);
  362. final PdfPCell cell = new PdfPCell();
  363. cell.setNoWrap(false);
  364. cell.setPaddingLeft(8f);
  365. cell.setPaddingRight(8f);
  366. cell.setPaddingBottom(8f);
  367. cell.setPaddingTop(8f);
  368. cell.setColspan(6);
  369. cell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  370. cell.addElement(imageTable);
  371. table.addCell(cell);
  372. //第三行
  373. createPDFCell(tableFont, table, "记录人", Element.ALIGN_CENTER, 1, 1);
  374. createPDFCell(tableFont, table, data.get("recorderName").toString(), Element.ALIGN_CENTER, 5, 1);
  375. /* //第三行
  376. createPDFCell(tableFont, table, "参会人员签字", Element.ALIGN_CENTER, 1, 1);
  377. createPDFCell(tableFont, table, data.get("users").toString(), Element.ALIGN_CENTER, 5, 1);*/
  378. document.add(table);
  379. }
  380. private static PdfPTable getImage(List<String> images, int totalImages,float imageWidth, float imageHeight,Integer ImageNumsOfRow) throws Exception {
  381. if (images == null) {
  382. images = new ArrayList<>();
  383. }
  384. PdfPTable innerTable = new PdfPTable(ImageNumsOfRow);
  385. innerTable.setSplitRows(false);
  386. innerTable.setSplitLate(true);
  387. //这里根据实际图片数量来判断是否需要补充白色图片,保证每行显示3张图片,用以填充空白
  388. final int reallySize = images.size();
  389. List<String> list = new ArrayList<>(images);
  390. for (int i = 0; i < totalImages - reallySize && totalImages > reallySize; i++) {
  391. list.add("black.png");
  392. }
  393. //分割,每行显示3张图片,获取分割的行数
  394. List<List<String>> rows = new ArrayList<>();
  395. for (int i = 0; i < list.size(); i += ImageNumsOfRow) {
  396. List<String> row = list.subList(i, Math.min(i + ImageNumsOfRow, list.size()));
  397. rows.add(row);
  398. }
  399. for (List<String> row : rows) {
  400. for (String image : row) {
  401. Image imageData = null;
  402. imageData = convertFileToByteArray(new File(image));
  403. if(imageData != null ){
  404. imageData.scaleAbsolute(imageWidth, imageHeight);
  405. }
  406. PdfPCell cell = new PdfPCell(imageData);
  407. cell.setBorder(Rectangle.NO_BORDER);
  408. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  409. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  410. innerTable.addCell(cell);
  411. }
  412. //实际上这儿永远不会存在不满足3个的情况,因为上面会根据图片数量补全到十二个
  413. //具体补全数量可以根据实际图片大小与页面尺寸来调整
  414. int size = row.size();
  415. while (size < 3) {
  416. PdfPCell emptyCell = new PdfPCell();
  417. emptyCell.setBorder(Rectangle.NO_BORDER);
  418. innerTable.addCell(emptyCell);
  419. size++;
  420. }
  421. }
  422. PdfPTable outerTable = new PdfPTable(1);
  423. PdfPCell innerCell = new PdfPCell(innerTable);
  424. innerCell.setBorder(Rectangle.NO_BORDER);
  425. outerTable.setSplitRows(false);
  426. outerTable.setSplitLate(true);
  427. outerTable.addCell(innerCell);
  428. return outerTable;
  429. }
  430. public static Image convertFileToByteArray(File file) throws Exception {
  431. try {
  432. FileInputStream fis = new FileInputStream(file);
  433. byte[] byteArray = new byte[(int) file.length()];
  434. fis.read(byteArray);
  435. fis.close();
  436. return Image.getInstance(byteArray);
  437. } catch (IOException e) {
  438. return getLocalImage();
  439. }
  440. }
  441. private static Image getLocalImage() {
  442. try {
  443. final ApplicationContext applicationContext = SpringUtil.getApplicationContext();
  444. final Resource[] resources = applicationContext.getResources("classpath:file/black.png");
  445. if(resources == null || resources.length == 0){
  446. return null;
  447. }
  448. return Image.getInstance(resources[0].getURL());
  449. } catch (IOException | BadElementException e) {
  450. throw new RuntimeException(e);
  451. }
  452. }
  453. public static void dealSafeCheckPBody(Document document, PdfPTable table, Font tableFont, Font tableTitleFont, List<CheckDataVo> data) throws DocumentException {
  454. PdfUtil.createPDFCell(tableTitleFont, table, "序号", Element.ALIGN_MIDDLE, 2, 0);
  455. PdfUtil.createPDFCell(tableTitleFont, table, "检查内容", Element.ALIGN_MIDDLE, 5, 0);
  456. PdfUtil.createPDFCell(tableTitleFont, table, "主要指标描述情况", Element.ALIGN_MIDDLE, 17, 0);
  457. PdfUtil.createPDFCell(tableTitleFont, table, "检查情况", Element.ALIGN_MIDDLE, 5, 0);
  458. PdfUtil.createPDFCell(tableTitleFont, table, "存在的问题及整改意见", Element.ALIGN_MIDDLE, 15, 0);
  459. PdfUtil.createPDFCell(tableTitleFont, table, "整改期限", Element.ALIGN_MIDDLE, 2, 0);
  460. // List<Object> dataList = (List<Object>) data.get("content");
  461. for (int i = 1; i <= data.size(); i++) {
  462. PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 0, 10);
  463. PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getItemName(), Element.ALIGN_LEFT, 5, 0, 10);
  464. PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getPointName(), Element.ALIGN_LEFT, 17, 0, 10);
  465. PdfUtil.createPDFCell(tableFont, table, StringUtils.isEmpty(data.get(i - 1).getResRemark()) ? "√" : "×", Element.ALIGN_MIDDLE, 5, 0, 10);
  466. PdfUtil.createPDFCell(tableFont, table, StringUtils.isEmpty(data.get(i - 1).getResRemark()) ? "" : data.get(i - 1).getResRemark(), Element.ALIGN_LEFT, 15, 0, 10);
  467. PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getRectificationDeadline(), Element.ALIGN_MIDDLE, 2, 0, 25);
  468. }
  469. document.add(table);
  470. }
  471. }