PdfUtil.java 24 KB

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