PdfUtil.java 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. package com.xunmei.file.utils;
  2. import cn.hutool.core.collection.ListUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import cn.hutool.extra.spring.SpringUtil;
  5. import com.lowagie.text.*;
  6. import com.lowagie.text.pdf.*;
  7. import com.xunmei.common.core.constant.DictConstants;
  8. import com.xunmei.common.security.utils.DictUtils;
  9. import com.xunmei.file.vo.ItextPdfTableVo;
  10. import com.xunmei.system.api.domain.AccessDataVo;
  11. import com.xunmei.system.api.domain.CheckDataVo;
  12. import com.xunmei.system.api.domain.SafeCheckTaskRegisterBookVo;
  13. import com.xunmei.system.api.domain.SysDictData;
  14. import io.netty.util.internal.StringUtil;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.apache.commons.io.FileUtils;
  17. import org.springframework.context.ApplicationContext;
  18. import org.springframework.core.io.Resource;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.util.List;
  24. import java.util.*;
  25. import java.util.stream.Collectors;
  26. @Slf4j
  27. public class PdfUtil {
  28. public static Document createDocument(float marginLeft, float marginRight, float marginTop, float marginBottom) {
  29. return new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom);
  30. }
  31. public static ItextPdfTableVo createTable(String filename, int numColumns, int fontSize) throws Exception {
  32. Document document = createDocument(0, 0, 50, 50);
  33. File file = FileUtils.getFile(filename);
  34. FileOutputStream fos = new FileOutputStream(file);
  35. final PdfWriter writer = PdfWriter.getInstance(document, fos);
  36. document.open();
  37. // 使用语言包字
  38. BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  39. // 外部字体
  40. BaseFont fs = BaseFont.createFont("/fonts/msyh.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  41. Font tableFont = new Font(fs, fontSize, Font.NORMAL);
  42. PdfPTable table = new PdfPTable(numColumns);
  43. // 设置各列列宽
  44. // table.setTotalWidth(new float[]{90, 100, 100, 120, 100, 100});
  45. table.setSpacingBefore(16f);
  46. table.setSplitRows(true);
  47. table.setSplitLate(false);
  48. ItextPdfTableVo itextPdfTableVo = new ItextPdfTableVo();
  49. itextPdfTableVo.setDocument(document);
  50. itextPdfTableVo.setWriter(writer);
  51. itextPdfTableVo.setAbf(abf);
  52. itextPdfTableVo.setFs(fs);
  53. itextPdfTableVo.setTableFont(tableFont);
  54. itextPdfTableVo.setTable(table);
  55. return itextPdfTableVo;
  56. }
  57. public static PdfPCell createPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan) {
  58. final PdfPCell cell = cell(tableFont, content, align, colspan, rowspan);
  59. table.addCell(cell);
  60. return cell;
  61. }
  62. public static PdfPCell createPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan, Integer lineSpacing) {
  63. final PdfPCell cell = cell(tableFont, content, align, colspan, rowspan, lineSpacing);
  64. table.addCell(cell);
  65. return cell;
  66. }
  67. private static PdfPCell cell(Font tableFont, String content, int align, Integer colspan, Integer rowspan) {
  68. PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
  69. if (colspan != null && colspan > 0) {
  70. cell.setColspan(colspan);
  71. }
  72. if (rowspan != null && rowspan > 0) {
  73. cell.setRowspan(rowspan);
  74. }
  75. // cell.setPaddingTop(8f);
  76. cell.setPaddingLeft(8f);
  77. cell.setPaddingRight(8f);
  78. cell.setPaddingBottom(8f);
  79. cell.setLeading(1F, 1.5F);
  80. if (PdfPCell.ALIGN_MIDDLE != align) {
  81. cell.setHorizontalAlignment(align);
  82. } else {
  83. //设置单元格的垂直方向对齐方式
  84. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
  85. //设置单元格的水平方向对齐方式
  86. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  87. }
  88. return cell;
  89. }
  90. private static PdfPCell cell(Font tableFont, String content, int align, Integer colspan, Integer rowspan, Integer lineSpacing) {
  91. PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
  92. if (colspan != null && colspan > 0) {
  93. cell.setColspan(colspan);
  94. }
  95. if (rowspan != null && rowspan > 0) {
  96. cell.setRowspan(rowspan);
  97. }
  98. cell.setNoWrap(false);
  99. cell.setLeading(lineSpacing, 0);
  100. if (PdfPCell.ALIGN_MIDDLE != align) {
  101. cell.setHorizontalAlignment(align);
  102. cell.setPaddingLeft(8f);
  103. cell.setPaddingRight(8f);
  104. cell.setPaddingBottom(8f);
  105. } else {
  106. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//设置单元格的垂直对齐方式
  107. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  108. }
  109. return cell;
  110. }
  111. /**
  112. * 生成文字类型的水印
  113. */
  114. public static void createWatermark(Document document, PdfWriter writer, Font waterMarkFont, String content) throws DocumentException, IOException {
  115. PdfContentByte waterMarkPdfContent = writer.getDirectContentUnder();
  116. Phrase phrase = new Phrase(content, waterMarkFont);
  117. float pageWidth = document.right() + document.left();//获取pdf内容正文页面宽度
  118. float pageHeight = document.top() + document.bottom();//获取pdf内容正文页面高度
  119. //两行三列
  120. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  121. pageWidth * 0.25f, pageHeight * 0.2f, 45);
  122. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  123. pageWidth * 0.25f, pageHeight * 0.5f, 45);
  124. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  125. pageWidth * 0.25f, pageHeight * 0.8f, 45);
  126. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  127. pageWidth * 0.65f, pageHeight * 0.2f, 45);
  128. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  129. pageWidth * 0.65f, pageHeight * 0.5f, 45);
  130. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  131. pageWidth * 0.65f, pageHeight * 0.8f, 45);
  132. }
  133. public static void dealHeader(Document document, BaseFont fs, String title, int fontSize) throws DocumentException {
  134. //文件title
  135. Paragraph p = new Paragraph(title, new Font(fs, fontSize, Font.NORMAL));
  136. p.setAlignment(Paragraph.ALIGN_CENTER);
  137. // 文件title 段落下空白
  138. p.setSpacingAfter(10f);
  139. p.setAlignment(Element.ALIGN_CENTER);
  140. document.add(p);
  141. }
  142. public static void dealEduBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  143. //第一行
  144. createPDFCell(tableFont, table, "时间", Element.ALIGN_MIDDLE, 1, 1);
  145. createPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_MIDDLE, 2, 1);
  146. createPDFCell(tableFont, table, "地点", Element.ALIGN_MIDDLE, 1, 1);
  147. createPDFCell(tableFont, table, data.get("address").toString(), Element.ALIGN_MIDDLE, 2, 1);
  148. //第二行
  149. createPDFCell(tableFont, table, "主持人", Element.ALIGN_MIDDLE, 1, 1);
  150. createPDFCell(tableFont, table, data.get("hostName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  151. createPDFCell(tableFont, table, "记录人", Element.ALIGN_MIDDLE, 1, 1);
  152. createPDFCell(tableFont, table, data.get("recorderName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  153. //内容
  154. createPDFCell(tableFont, table, "内容", Element.ALIGN_MIDDLE, 1, 1);
  155. createPDFCell(tableFont, table, data.get("content").toString(), Element.ALIGN_LEFT, 5, 1);
  156. createPDFCell(tableFont, table, "总结", Element.ALIGN_MIDDLE, 1, 1);
  157. createPDFCell(tableFont, table, data.get("note").toString(), Element.ALIGN_LEFT, 5, 1);
  158. createPDFCell(tableFont, table, "参会人员签字", Element.ALIGN_MIDDLE, 1, 1);
  159. dealEduImageCell((List<String>) data.get("signImage"), table, 5, 40, 40);
  160. document.add(table);
  161. //第二页
  162. //按6份等分图片数组,一页只显示6张
  163. final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 6);
  164. for (List<String> stringList : listList) {
  165. PdfPTable innerTable = new PdfPTable(6);
  166. createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
  167. //一行展示一张图片
  168. dealEduImageCell(new ArrayList<>(stringList), innerTable, 2, 185, 200);
  169. document.newPage();
  170. document.add(innerTable);
  171. }
  172. }
  173. public static void dealResumptionBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  174. // 第一行
  175. PdfUtil.createPDFCell(tableFont, table, "单位名称", Element.ALIGN_MIDDLE, 2, 0);
  176. PdfUtil.createPDFCell(tableFont, table, data.get("orgName").toString(), Element.ALIGN_MIDDLE, 3, 0);
  177. PdfUtil.createPDFCell(tableFont, table, "检查时间", Element.ALIGN_MIDDLE, 2, 0);
  178. PdfUtil.createPDFCell(tableFont, table, ObjectUtil.isNotEmpty(data.get("submiterNames")) ? data.get("dateStr").toString() : "", Element.ALIGN_MIDDLE, 3, 0);
  179. // 第二行
  180. PdfUtil.createPDFCell(tableFont, table, "检查人", Element.ALIGN_MIDDLE, 2, 0);
  181. PdfUtil.createPDFCell(tableFont, table, data.get("submiterNames").toString(), Element.ALIGN_MIDDLE, 8, 0);
  182. // 第三行
  183. PdfUtil.createPDFCell(tableFont, table, "检查时间", Element.ALIGN_MIDDLE, 2, 0);
  184. PdfUtil.createPDFCell(tableFont, table, "检查内容", Element.ALIGN_MIDDLE, 6, 0);
  185. PdfUtil.createPDFCell(tableFont, table, "检查情况", Element.ALIGN_MIDDLE, 2, 0);
  186. // PdfUtil.createPDFCell(tableFont, table, "检查时间", Element.ALIGN_CENTER, 1, 0);
  187. // PdfUtil.createPDFCell(tableFont, table, "检查内容", Element.ALIGN_CENTER, 5, 0);
  188. // PdfUtil.createPDFCell(tableFont, table, "检查情况", Element.ALIGN_CENTER, 0, 0);
  189. // PdfUtil.createPDFCell(tableFont, table, "登记人", Element.ALIGN_CENTER, 0, 0);
  190. /*List<String> names = new ArrayList<>();
  191. names.add(DictUtils.getDictLabel(DictConstants.RESUMPTION_PLAN_EXEC, 2));
  192. names.add(DictUtils.getDictLabel(DictConstants.RESUMPTION_PLAN_EXEC, 3));
  193. names.add(DictUtils.getDictLabel(DictConstants.RESUMPTION_PLAN_EXEC, 4));*/
  194. final List<SysDictData> dictCache = DictUtils.getDictCache(DictConstants.RESUMPTION_PLAN_EXEC);
  195. final List<String> names = dictCache.stream().map(SysDictData::getDictLabel).collect(Collectors.toList());
  196. for (String s : names) {
  197. List<LinkedHashMap<String, Object>> lists = (List<LinkedHashMap<String, Object>>) data.get(s);
  198. if (ObjectUtil.isEmpty(lists)) {
  199. continue;
  200. }
  201. //不同的执行时刻
  202. PdfUtil.createPDFCell(tableFont, table, getLineStr(s), PdfPCell.ALIGN_MIDDLE, 2, lists.size());
  203. int o = 1;
  204. for (LinkedHashMap<String, Object> listVo : lists) {
  205. // 检查内容
  206. String rowContent = o + "、" + listVo.get("pointName");
  207. PdfUtil.createPDFCell(tableFont, table, rowContent, Element.ALIGN_LEFT, 6, 0);
  208. // 检查情况
  209. PdfUtil.createPDFCell(tableFont, table, ObjectUtil.isEmpty(listVo.get("resValue")) ? "" : (((Integer) listVo.get("resValue")) == 0 ? "正常" : "异常"), Element.ALIGN_MIDDLE, 2, 0);
  210. // 检查人
  211. //PdfUtil.createPDFCell(tableFont, table, String.valueOf(listVo.get("submitName")), Element.ALIGN_CENTER, 0, 0);
  212. o++;
  213. }
  214. }
  215. PdfUtil.createPDFCell(tableFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 10, 1);
  216. PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  217. PdfUtil.createPDFCell(tableFont, table, "检查项目", Element.ALIGN_MIDDLE, 4, 1);
  218. PdfUtil.createPDFCell(tableFont, table, "存在问题", Element.ALIGN_MIDDLE, 4, 1);
  219. final List<LinkedHashMap> questionPdfVoList = (List<LinkedHashMap>) data.get("questions");
  220. if (ObjectUtil.isEmpty(questionPdfVoList)) {
  221. PdfUtil.createPDFCell(tableFont, table, "无", Element.ALIGN_MIDDLE, 2, 1);
  222. PdfUtil.createPDFCell(tableFont, table, StringUtil.EMPTY_STRING, Element.ALIGN_LEFT, 4, 1);
  223. PdfUtil.createPDFCell(tableFont, table, StringUtil.EMPTY_STRING, Element.ALIGN_MIDDLE, 4, 1);
  224. document.add(table);
  225. return;
  226. }
  227. int questionIndex = 1;
  228. for (int i = 0; i < questionPdfVoList.size(); i++) {
  229. PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionIndex++), Element.ALIGN_MIDDLE, 2, 1);
  230. PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionPdfVoList.get(i).get("checkContent")), Element.ALIGN_MIDDLE, 4, 1);
  231. PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionPdfVoList.get(i).get("questionDesc")), Element.ALIGN_MIDDLE, 4, 1);
  232. }
  233. // 备注数据
  234. // PdfUtil.createPDFCell(tableFont, table, getLineStr("备注"), PdfPCell.ALIGN_MIDDLE, 0, 0);
  235. // PdfUtil.createPDFCell(tableFont, table, data.get("remark").toString(), Element.ALIGN_LEFT, 7, 0);
  236. document.add(table);
  237. // Paragraph foot = new Paragraph(" 注:检查情况正常打“√”;发现问题打“×”,并在备注中具体说明。", tableFont);
  238. // Paragraph foot = new Paragraph(" 注:检查情况正常打“√”;发现问题打“×”,并在备注中具体说明。", new Font(fs, 8, Font.NORMAL));
  239. // foot.setAlignment(Paragraph.ALIGN_LEFT);
  240. //在后方加入16个空格
  241. // document.add(foot);
  242. }
  243. public static String getLineStr(String str) {
  244. StringBuilder result = new StringBuilder();
  245. for (int i = 0; i < str.length(); i++) {
  246. result.append(str.charAt(i)).append("\r");
  247. }
  248. return result.toString();
  249. }
  250. public static void dealOutInBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  251. table.setSplitLate(false);
  252. table.setSplitRows(true);
  253. //第一行
  254. createPDFCell(tableFont, table, "接待机构", Element.ALIGN_MIDDLE, 1, 1);
  255. createPDFCell(tableFont, table, data.get("inOrg").toString(), Element.ALIGN_MIDDLE, 2, 1);
  256. createPDFCell(tableFont, table, "接待日期", Element.ALIGN_MIDDLE, 1, 1);
  257. createPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_MIDDLE, 2, 1);
  258. //第二行
  259. createPDFCell(tableFont, table, "来访事由", Element.ALIGN_MIDDLE, 1, 1);
  260. createPDFCell(tableFont, table, data.get("reasons").toString(), Element.ALIGN_MIDDLE, 2, 1);
  261. createPDFCell(tableFont, table, "陪同人员", Element.ALIGN_MIDDLE, 1, 1);
  262. createPDFCell(tableFont, table, data.get("accompanyingPerson").toString(), Element.ALIGN_MIDDLE, 2, 1);
  263. //第三行
  264. createPDFCell(tableFont, table, "来访单位", Element.ALIGN_MIDDLE, 1, 1);
  265. createPDFCell(tableFont, table, data.get("outOrgName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  266. createPDFCell(tableFont, table, "来访人员", Element.ALIGN_MIDDLE, 1, 1);
  267. createPDFCell(tableFont, table, data.get("userName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  268. //第四行
  269. createPDFCell(tableFont, table, "证件类型", Element.ALIGN_MIDDLE, 1, 1);
  270. createPDFCell(tableFont, table, data.get("idType").toString(), Element.ALIGN_MIDDLE, 2, 1);
  271. createPDFCell(tableFont, table, "证件号码", Element.ALIGN_MIDDLE, 1, 1);
  272. createPDFCell(tableFont, table, data.get("idCard").toString(), Element.ALIGN_MIDDLE, 2, 1);
  273. //第五行
  274. createPDFCell(tableFont, table, "进入时间", Element.ALIGN_MIDDLE, 1, 1);
  275. createPDFCell(tableFont, table, data.get("inTime").toString(), Element.ALIGN_MIDDLE, 2, 1);
  276. createPDFCell(tableFont, table, "离开时间", Element.ALIGN_MIDDLE, 1, 1);
  277. createPDFCell(tableFont, table, data.get("outTime").toString(), Element.ALIGN_MIDDLE, 2, 1);
  278. //第六行
  279. createPDFCell(tableFont, table, "审批人", Element.ALIGN_MIDDLE, 1, 1);
  280. createPDFCell(tableFont, table, data.get("approveUser").toString(), Element.ALIGN_MIDDLE, 2, 1);
  281. createPDFCell(tableFont, table, "登记人", Element.ALIGN_MIDDLE, 1, 1);
  282. createPDFCell(tableFont, table, data.get("createBy").toString(), Element.ALIGN_MIDDLE, 2, 1);
  283. //第七行
  284. createPDFCell(tableFont, table, "审批人签字", Element.ALIGN_MIDDLE, 1, 1);
  285. dealImageCell((List<String>) data.get("approveSign"), table, 1, 50, 40,2,1);
  286. createPDFCell(tableFont, table, "登记人签字", Element.ALIGN_MIDDLE, 1, 1);
  287. dealImageCell((List<String>) data.get("submitSign"), table, 1, 50, 40,2,1);
  288. // /statics/2023/12/05/20231205183106A001.png
  289. //证件图片
  290. createPDFCell(tableFont, table, "证件图片", Element.ALIGN_MIDDLE, 1, 1);
  291. //证件图片 图片填充
  292. final PdfPTable imageTable1 = getImage((List<String>) data.get("imageFile"), 2, 190, 130, 2);
  293. final PdfPCell cell1 = new PdfPCell();
  294. cell1.setNoWrap(false);
  295. cell1.setPaddingLeft(4f);
  296. cell1.setPaddingRight(4f);
  297. cell1.setPaddingBottom(4f);
  298. cell1.setPaddingTop(4f);
  299. cell1.setColspan(5);
  300. cell1.setRowspan(1);
  301. //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  302. cell1.addElement(imageTable1);
  303. table.addCell(cell1);
  304. createPDFCell(tableFont, table, "身份核验材料", Element.ALIGN_MIDDLE, 1, 1);
  305. final PdfPTable imageTable3 = getImage((List<String>) data.get("checkImage"), 2, 190, 190, 2);
  306. final PdfPCell cell3 = new PdfPCell();
  307. cell3.setNoWrap(false);
  308. cell3.setPaddingLeft(4f);
  309. cell3.setPaddingRight(4f);
  310. cell3.setPaddingBottom(4f);
  311. cell3.setPaddingTop(4f);
  312. cell3.setColspan(5);
  313. cell3.setRowspan(1);
  314. //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  315. cell3.addElement(imageTable3);
  316. table.addCell(cell3);
  317. document.add(table);
  318. PdfPTable innerTable = new PdfPTable(6);
  319. createPDFCell(tableFont, innerTable, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
  320. // createPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
  321. final PdfPTable imageTable2 = getImage((List<String>) data.get("file"), 2, 380, 320, 1);
  322. final PdfPCell cell2 = new PdfPCell();
  323. cell2.setNoWrap(false);
  324. cell2.setPaddingLeft(4f);
  325. cell2.setPaddingRight(4f);
  326. cell2.setPaddingBottom(4f);
  327. cell2.setPaddingTop(4f);
  328. cell2.setColspan(5);
  329. cell2.setRowspan(1);
  330. cell2.addElement(imageTable2);
  331. innerTable.addCell(cell2);
  332. document.newPage();
  333. document.add(innerTable);
  334. }
  335. public static void dealDrillBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  336. //第一行
  337. createPDFCell(tableFont, table, "演练单位", Element.ALIGN_MIDDLE, 1, 1);
  338. createPDFCell(tableFont, table, data.get("orgName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  339. createPDFCell(tableFont, table, "地点", Element.ALIGN_MIDDLE, 1, 1);
  340. createPDFCell(tableFont, table, data.get("drillSite").toString(), Element.ALIGN_MIDDLE, 2, 1);
  341. //第二行
  342. createPDFCell(tableFont, table, "指挥人", Element.ALIGN_MIDDLE, 1, 1);
  343. createPDFCell(tableFont, table, data.get("hostName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  344. createPDFCell(tableFont, table, "记录人", Element.ALIGN_MIDDLE, 1, 1);
  345. createPDFCell(tableFont, table, data.get("recorderName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  346. //第三行
  347. createPDFCell(tableFont, table, "演练时间", Element.ALIGN_MIDDLE, 1, 1);
  348. createPDFCell(tableFont, table, data.get("drillTime").toString(), Element.ALIGN_LEFT, 5, 1);
  349. createPDFCell(tableFont, table, "演练项目", Element.ALIGN_MIDDLE, 1, 1);
  350. createPDFCell(tableFont, table, data.get("typeText").toString(), Element.ALIGN_LEFT, 5, 1);
  351. //预设案由
  352. createPDFCell(tableFont, table, "预设案由", Element.ALIGN_MIDDLE, 1, 1);
  353. createPDFCell(tableFont, table, data.get("presetCase").toString(), Element.ALIGN_LEFT, 5, 1);
  354. //演练情况
  355. createPDFCell(tableFont, table, "演练情况", Element.ALIGN_MIDDLE, 1, 1);
  356. createPDFCell(tableFont, table, data.get("drillSituation").toString(), Element.ALIGN_LEFT, 5, 1);
  357. //点评总结
  358. createPDFCell(tableFont, table, "点评总结", Element.ALIGN_MIDDLE, 1, 1);
  359. createPDFCell(tableFont, table, data.get("comment").toString(), Element.ALIGN_LEFT, 5, 1);
  360. //参会人员签字
  361. createPDFCell(tableFont, table, "参会人员签字", Element.ALIGN_MIDDLE, 1, 1);
  362. dealEduImageCell((List<String>) data.get("signImage"), table, 5, 40, 40);
  363. document.add(table);
  364. //第二页
  365. //按6份等分图片数组,一页只显示6张
  366. final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 6);
  367. for (List<String> stringList : listList) {
  368. PdfPTable innerTable = new PdfPTable(6);
  369. createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
  370. //一行展示一张图片
  371. dealEduImageCell(new ArrayList<>(stringList), innerTable, 2, 185, 200);
  372. document.newPage();
  373. document.add(innerTable);
  374. }
  375. }
  376. public static void dealEduImageCell(List<String> imageList, PdfPTable table, Integer imageNumsOfRow, Integer imageWith, Integer imageHigh) throws Exception {
  377. //签字区域
  378. PdfPCell outCell = new PdfPCell();
  379. outCell.setNoWrap(false);
  380. outCell.setColspan(6);
  381. // outCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  382. final int imageAddNums = imageList.size() % imageNumsOfRow;
  383. if (imageAddNums != 0) {
  384. for (int i = 0; i < imageNumsOfRow - imageAddNums; i++) {
  385. imageList.add("black.png");
  386. }
  387. }
  388. PdfPTable imageInnerTable = new PdfPTable(imageNumsOfRow);
  389. imageInnerTable.setSplitRows(true);
  390. imageInnerTable.setSplitLate(false);
  391. imageInnerTable.setWidthPercentage(100F);
  392. for (String image : imageList) {
  393. Image imageData = convertFileToByteArray(new File(image));
  394. if (imageData != null) {
  395. imageData.scaleAbsolute(imageWith, imageHigh);
  396. }
  397. PdfPCell innerCell = new PdfPCell(imageData);
  398. innerCell.setNoWrap(false);
  399. innerCell.setPaddingTop(8f);
  400. innerCell.setPaddingLeft(8f);
  401. innerCell.setPaddingRight(8f);
  402. innerCell.setPaddingBottom(8f);
  403. innerCell.setBorder(Rectangle.NO_BORDER);
  404. innerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  405. innerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  406. imageInnerTable.addCell(innerCell);
  407. }
  408. outCell.addElement(imageInnerTable);
  409. table.addCell(outCell);
  410. }
  411. public static void dealImageCell(List<String> imageList, PdfPTable table, Integer imageNumsOfRow, Integer imageWith, Integer imageHigh,Integer cosSpan,Integer rowSpan) throws Exception {
  412. //签字区域
  413. PdfPCell outCell = new PdfPCell();
  414. outCell.setNoWrap(false);
  415. outCell.setColspan(cosSpan);
  416. outCell.setRowspan(rowSpan);
  417. final int imageAddNums = imageList.size() % imageNumsOfRow;
  418. if (imageAddNums != 0) {
  419. for (int i = 0; i < imageNumsOfRow - imageAddNums; i++) {
  420. imageList.add("black.png");
  421. }
  422. }
  423. PdfPTable imageInnerTable = new PdfPTable(imageNumsOfRow);
  424. imageInnerTable.setSplitRows(true);
  425. imageInnerTable.setSplitLate(false);
  426. imageInnerTable.setWidthPercentage(100F);
  427. for (String image : imageList) {
  428. Image imageData = convertFileToByteArray(new File(image));
  429. if (imageData != null) {
  430. imageData.scaleAbsolute(imageWith, imageHigh);
  431. }
  432. PdfPCell innerCell = new PdfPCell(imageData);
  433. innerCell.setNoWrap(false);
  434. innerCell.setPaddingTop(8f);
  435. innerCell.setPaddingLeft(8f);
  436. innerCell.setPaddingRight(8f);
  437. innerCell.setPaddingBottom(8f);
  438. innerCell.setBorder(Rectangle.NO_BORDER);
  439. innerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  440. innerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  441. imageInnerTable.addCell(innerCell);
  442. }
  443. outCell.addElement(imageInnerTable);
  444. table.addCell(outCell);
  445. }
  446. private static PdfPTable getImage(List<String> images, int totalImages, float imageWidth, float imageHeight, Integer ImageNumsOfRow) throws Exception {
  447. if (images == null) {
  448. images = new ArrayList<>();
  449. }
  450. PdfPTable innerTable = new PdfPTable(ImageNumsOfRow);
  451. innerTable.setWidthPercentage(100f);
  452. innerTable.setSplitRows(true);
  453. innerTable.setSplitLate(false);
  454. //这里根据实际图片数量来判断是否需要补充白色图片,保证每行显示3张图片,用以填充空白
  455. final int reallySize = images.size();
  456. List<String> list = new ArrayList<>(images);
  457. for (int i = 0; i < totalImages - reallySize && totalImages > reallySize; i++) {
  458. list.add("black.png");
  459. }
  460. //分割,每行显示3张图片,获取分割的行数
  461. List<List<String>> rows = new ArrayList<>();
  462. for (int i = 0; i < list.size(); i += ImageNumsOfRow) {
  463. List<String> row = list.subList(i, Math.min(i + ImageNumsOfRow, list.size()));
  464. rows.add(row);
  465. }
  466. for (List<String> row : rows) {
  467. for (String image : row) {
  468. Image imageData = null;
  469. imageData = convertFileToByteArray(new File(image));
  470. if (imageData != null) {
  471. imageData.scaleAbsolute(imageWidth, imageHeight);
  472. }
  473. PdfPCell cell = new PdfPCell(imageData);
  474. cell.setBorder(Rectangle.NO_BORDER);
  475. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  476. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  477. innerTable.addCell(cell);
  478. }
  479. }
  480. PdfPTable outerTable = new PdfPTable(1);
  481. PdfPCell innerCell = new PdfPCell(innerTable);
  482. innerCell.setBorder(Rectangle.NO_BORDER);
  483. outerTable.setSplitRows(true);
  484. outerTable.setSplitLate(false);
  485. outerTable.addCell(innerCell);
  486. outerTable.setWidthPercentage(100f);
  487. return outerTable;
  488. }
  489. public static Image convertFileToByteArray(File file) throws Exception {
  490. try {
  491. FileInputStream fis = new FileInputStream(file);
  492. byte[] byteArray = new byte[(int) file.length()];
  493. fis.read(byteArray);
  494. fis.close();
  495. return Image.getInstance(byteArray);
  496. } catch (IOException e) {
  497. return getLocalImage();
  498. }
  499. }
  500. private static Image getLocalImage() {
  501. try {
  502. final ApplicationContext applicationContext = SpringUtil.getApplicationContext();
  503. final Resource[] resources = applicationContext.getResources("classpath:file/black.png");
  504. if (resources == null || resources.length == 0) {
  505. return null;
  506. }
  507. return Image.getInstance(resources[0].getURL());
  508. } catch (IOException | BadElementException e) {
  509. throw new RuntimeException(e);
  510. }
  511. }
  512. public static void dealAccessPBody(Document document, PdfPTable table, Font tableFont, Font tableTitleFont, List<AccessDataVo> data) throws DocumentException {
  513. PdfUtil.createPDFCell(tableTitleFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  514. PdfUtil.createPDFCell(tableTitleFont, table, "区域名称", Element.ALIGN_MIDDLE, 6, 1);
  515. PdfUtil.createPDFCell(tableTitleFont, table, "调阅项目", Element.ALIGN_MIDDLE, 5, 1);
  516. PdfUtil.createPDFCell(tableTitleFont, table, "调阅情况", Element.ALIGN_MIDDLE, 6, 1);
  517. List<AccessDataVo> normalDataList = data.stream().filter(d -> ObjectUtil.equal(0, d.getStatus())).collect(Collectors.toList());
  518. if (ObjectUtil.isNotEmpty(normalDataList)) {
  519. for (int i = 1; i <= normalDataList.size(); i++) {
  520. AccessDataVo accessDataVo = normalDataList.get(i - 1);
  521. PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 1);
  522. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getAreaName(), Element.ALIGN_MIDDLE, 6, 1);
  523. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getProject(), Element.ALIGN_MIDDLE, 5, 1);
  524. PdfUtil.createPDFCell(tableFont, table, "正常", Element.ALIGN_MIDDLE, 6, 1);
  525. }
  526. } else {
  527. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  528. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  529. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 5, 1);
  530. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  531. }
  532. PdfUtil.createPDFCell(tableTitleFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 19, 1);
  533. PdfUtil.createPDFCell(tableTitleFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  534. PdfUtil.createPDFCell(tableTitleFont, table, "区域名称", Element.ALIGN_MIDDLE, 6, 1);
  535. PdfUtil.createPDFCell(tableTitleFont, table, "调阅项目", Element.ALIGN_MIDDLE, 5, 1);
  536. PdfUtil.createPDFCell(tableTitleFont, table, "存在问题", Element.ALIGN_MIDDLE, 6, 1);
  537. List<AccessDataVo> exceptionDataList = data.stream().filter(d -> ObjectUtil.equal(1, d.getStatus())).collect(Collectors.toList());
  538. if (ObjectUtil.isNotEmpty(exceptionDataList)) {
  539. for (int i = 1; i <= exceptionDataList.size(); i++) {
  540. AccessDataVo accessDataVo = exceptionDataList.get(i - 1);
  541. PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 1);
  542. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getAreaName(), Element.ALIGN_MIDDLE, 6, 1);
  543. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getProject(), Element.ALIGN_MIDDLE, 5, 1);
  544. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getAbnormalIllustrate(), Element.ALIGN_MIDDLE, 6, 1);
  545. }
  546. } else {
  547. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  548. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  549. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 5, 1);
  550. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  551. }
  552. }
  553. public static void dealSafeCheckPBody(Document document, PdfPTable table, Font
  554. tableFont, SafeCheckTaskRegisterBookVo data) throws Exception {
  555. PdfUtil.createPDFCell(tableFont, table, "被查单位", Element.ALIGN_MIDDLE, 3, 1);
  556. PdfUtil.createPDFCell(tableFont, table, data.getOrgName(), Element.ALIGN_MIDDLE, 4, 1);
  557. PdfUtil.createPDFCell(tableFont, table, "检查日期", Element.ALIGN_MIDDLE, 3, 1);
  558. PdfUtil.createPDFCell(tableFont, table, data.getDateStr(), Element.ALIGN_MIDDLE, 4, 1);
  559. PdfUtil.createPDFCell(tableFont, table, "检查类型", Element.ALIGN_MIDDLE, 3, 1);
  560. PdfUtil.createPDFCell(tableFont, table, data.getCheckTypeText(), Element.ALIGN_MIDDLE, 4, 1);
  561. PdfUtil.createPDFCell(tableFont, table, "检查名称", Element.ALIGN_MIDDLE, 3, 1);
  562. PdfUtil.createPDFCell(tableFont, table, data.getTaskTitle(), Element.ALIGN_MIDDLE, 4, 1);
  563. PdfUtil.createPDFCell(tableFont, table, "检查单位", Element.ALIGN_MIDDLE, 3, 1);
  564. PdfUtil.createPDFCell(tableFont, table, data.getCheckOrgName(), Element.ALIGN_MIDDLE, 4, 1);
  565. PdfUtil.createPDFCell(tableFont, table, "检查组成员", Element.ALIGN_MIDDLE, 3, 1);
  566. PdfUtil.createPDFCell(tableFont, table, data.getCheckTeam(), Element.ALIGN_MIDDLE, 4, 1);
  567. PdfUtil.createPDFCell(tableFont, table, "主查人", Element.ALIGN_MIDDLE, 3, 1);
  568. PdfUtil.createPDFCell(tableFont, table, data.getCheckUserInfo(), Element.ALIGN_MIDDLE, 4, 1);
  569. PdfUtil.createPDFCell(tableFont, table, "主查人签字", Element.ALIGN_MIDDLE, 3, 1);
  570. List<String> list = new ArrayList<>();
  571. list.add(data.getSignImg());
  572. dealEduImageCell(list, table, 1, 70, 40);
  573. PdfUtil.createPDFCell(tableFont, table, "检查工作情况", Element.ALIGN_MIDDLE, 14, 1);
  574. List<CheckDataVo> normalDataList = data.getCheckDatas().stream().filter(d -> ObjectUtil.equal(Boolean.TRUE, d.getCheckStatus())).collect(Collectors.toList());
  575. if (normalDataList.size() > 0) {
  576. PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  577. PdfUtil.createPDFCell(tableFont, table, "检查内容", Element.ALIGN_MIDDLE, 10, 1);
  578. PdfUtil.createPDFCell(tableFont, table, "检查情况", Element.ALIGN_MIDDLE, 2, 1);
  579. for (CheckDataVo checkDataVo : normalDataList) {
  580. PdfUtil.createPDFCell(tableFont, table, String.valueOf(normalDataList.indexOf(checkDataVo) + 1), Element.ALIGN_MIDDLE, 2, 1);
  581. PdfUtil.createPDFCell(tableFont, table, checkDataVo.getPointName(), Element.ALIGN_MIDDLE, 10, 1);
  582. PdfUtil.createPDFCell(tableFont, table, "正常", Element.ALIGN_MIDDLE, 2, 1);
  583. }
  584. }
  585. PdfUtil.createPDFCell(tableFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 14, 1);
  586. PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  587. PdfUtil.createPDFCell(tableFont, table, "检查项目", Element.ALIGN_MIDDLE, 6, 1);
  588. PdfUtil.createPDFCell(tableFont, table, "存在问题", Element.ALIGN_MIDDLE, 6, 1);
  589. final Optional<CheckDataVo> optional = data.getCheckDatas().stream().filter(res -> ObjectUtil.isNotEmpty(res.getResRemark())).findAny();
  590. if (!optional.isPresent()) {
  591. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  592. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  593. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  594. document.add(table);
  595. return;
  596. }
  597. int questionIndex = 1;
  598. for (int i = 1; i <= data.getCheckDatas().size(); i++) {
  599. final CheckDataVo checkDataVo = data.getCheckDatas().get(i - 1);
  600. if (ObjectUtil.isEmpty(checkDataVo.getResRemark())) {
  601. continue;
  602. }
  603. PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionIndex++), Element.ALIGN_MIDDLE, 2, 1);
  604. PdfUtil.createPDFCell(tableFont, table, checkDataVo.getItemName(), Element.ALIGN_MIDDLE, 6, 1);
  605. PdfUtil.createPDFCell(tableFont, table, checkDataVo.getResRemark(), Element.ALIGN_MIDDLE, 6, 1);
  606. }
  607. document.add(table);
  608. }
  609. public static void addPageNum(String srcPdfPath, String targetPdfPath, BaseFont fs, Font tableFont) {
  610. try {
  611. // 输出文件 流
  612. FileOutputStream fos = new FileOutputStream(targetPdfPath);
  613. // 读取 源PDF文件,进行一页一页复制,才能触发 添加页码的 页面监听事件
  614. PdfReader reader = new PdfReader(srcPdfPath);
  615. // 获取 源文件总页数
  616. int num = reader.getNumberOfPages();
  617. // 新建文档,默认A4大小
  618. Document document = createDocument(0, 0, 50, 50);
  619. PdfWriter writer = PdfWriter.getInstance(document, fos);
  620. writer.setPageEvent(new PdfPageHelperEvent(num, writer, fs, tableFont));
  621. document.open();
  622. // PDF内容体
  623. PdfContentByte pdfContent = writer.getDirectContent();
  624. // 页面数是从1开始的
  625. for (int i = 1; i <= num; i++) {
  626. document.newPage();
  627. // 设置空页码进行展示
  628. writer.setPageEmpty(false);
  629. PdfImportedPage page = writer.getImportedPage(reader, i);
  630. // 复制好的页面,添加到内容去,触发事件监听
  631. pdfContent.addTemplate(page, 0, 42);
  632. }
  633. document.close();
  634. reader.close();
  635. final File file = new File(srcPdfPath);
  636. file.delete();
  637. } catch (Exception e) {
  638. e.printStackTrace();
  639. }
  640. }
  641. }