PdfUtil.java 42 KB

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