PdfUtil.java 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. package com.xunmei.file.utils;
  2. import cn.hutool.core.collection.ListUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.extra.spring.SpringUtil;
  6. import com.alibaba.fastjson2.JSON;
  7. import com.alibaba.fastjson2.JSONArray;
  8. import com.lowagie.text.*;
  9. import com.lowagie.text.pdf.*;
  10. import com.xunmei.common.core.constant.DictConstants;
  11. import com.xunmei.common.core.domain.iot.domain.IotAlarmSubsystem;
  12. import com.xunmei.common.core.domain.letter.domain.CoreIntroduceLetterOutInRequestUser;
  13. import com.xunmei.common.core.domain.letter.dto.CoreIntroduceLetterUserAddDto;
  14. import com.xunmei.common.core.exception.ServiceException;
  15. import com.xunmei.common.security.utils.DictUtils;
  16. import com.xunmei.file.enums.IdTypeEnums;
  17. import com.xunmei.file.vo.ItextPdfTableVo;
  18. import com.xunmei.system.api.domain.AccessDataVo;
  19. import com.xunmei.system.api.domain.CheckDataVo;
  20. import com.xunmei.system.api.domain.SafeCheckTaskRegisterBookVo;
  21. import com.xunmei.system.api.domain.SysDictData;
  22. import com.xunmei.system.api.enums.ProtectionStatus;
  23. import io.netty.util.internal.StringUtil;
  24. import lombok.extern.slf4j.Slf4j;
  25. import org.apache.commons.io.FileUtils;
  26. import org.springframework.context.ApplicationContext;
  27. import org.springframework.core.io.Resource;
  28. import java.io.File;
  29. import java.io.FileInputStream;
  30. import java.io.FileOutputStream;
  31. import java.io.IOException;
  32. import java.text.SimpleDateFormat;
  33. import java.util.List;
  34. import java.util.*;
  35. import java.util.stream.Collectors;
  36. @Slf4j
  37. public class PdfUtil {
  38. public static Document createDocument(float marginLeft, float marginRight, float marginTop, float marginBottom) {
  39. return new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom);
  40. }
  41. public static ItextPdfTableVo createTable(String filename, int numColumns, int fontSize) throws Exception {
  42. Document document = createDocument(0, 0, 50, 12);
  43. File file = FileUtils.getFile(filename);
  44. FileOutputStream fos = new FileOutputStream(file);
  45. final PdfWriter writer = PdfWriter.getInstance(document, fos);
  46. document.open();
  47. // 使用语言包字
  48. BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  49. // 外部字体
  50. BaseFont fs = BaseFont.createFont("/fonts/msyh.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  51. Font tableFont = new Font(fs, fontSize, Font.NORMAL);
  52. PdfPTable table = new PdfPTable(numColumns);
  53. // 设置各列列宽
  54. // table.setTotalWidth(new float[]{90, 100, 100, 120, 100, 100});
  55. // table.setSpacingBefore(16f);
  56. table.setSplitRows(true);
  57. table.setSplitLate(false);
  58. ItextPdfTableVo itextPdfTableVo = new ItextPdfTableVo();
  59. itextPdfTableVo.setDocument(document);
  60. itextPdfTableVo.setWriter(writer);
  61. itextPdfTableVo.setAbf(abf);
  62. itextPdfTableVo.setFs(fs);
  63. itextPdfTableVo.setTableFont(tableFont);
  64. itextPdfTableVo.setTable(table);
  65. return itextPdfTableVo;
  66. }
  67. public static PdfPCell createPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan) {
  68. final PdfPCell cell = cell(tableFont, content, align, colspan, rowspan);
  69. table.addCell(cell);
  70. return cell;
  71. }
  72. public static PdfPCell createPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan, Integer lineSpacing) {
  73. final PdfPCell cell = cell(tableFont, content, align, colspan, rowspan, lineSpacing);
  74. table.addCell(cell);
  75. return cell;
  76. }
  77. /**
  78. * 创建单元格 - 固定单元格大小
  79. * @param tableFont
  80. * @param table
  81. * @param content
  82. * @param align
  83. * @param colspan
  84. * @param rowspan
  85. * @param
  86. * @return
  87. */
  88. public static PdfPCell createOutInPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan) {
  89. final PdfPCell cell = outInCell(tableFont, content, align, colspan, rowspan);
  90. table.addCell(cell);
  91. return cell;
  92. }
  93. public static PdfPCell createOutInPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan, Float fixedHeight) {
  94. final PdfPCell cell = OutInCell(tableFont, content, align, colspan, rowspan, fixedHeight);
  95. table.addCell(cell);
  96. return cell;
  97. }
  98. private static PdfPCell OutInCell(Font tableFont, String content, int align, Integer colspan, Integer rowspan, Float fixedHeight) {
  99. PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
  100. if (colspan != null && colspan > 0) {
  101. cell.setColspan(colspan);
  102. }
  103. if (rowspan != null && rowspan > 0) {
  104. cell.setRowspan(rowspan);
  105. }
  106. cell.setPaddingTop(2f);
  107. cell.setPaddingBottom(7f);
  108. cell.setPaddingLeft(8f);
  109. cell.setPaddingRight(8f);
  110. cell.setFixedHeight(fixedHeight);
  111. cell.setLeading(0F, 1.2F);
  112. if (PdfPCell.ALIGN_MIDDLE != align) {
  113. cell.setHorizontalAlignment(align);
  114. } else {
  115. //设置单元格的垂直方向对齐方式
  116. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
  117. //设置单元格的水平方向对齐方式
  118. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  119. }
  120. return cell;
  121. }
  122. private static PdfPCell outInCell(Font tableFont, String content, int align, Integer colspan, Integer rowspan) {
  123. PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
  124. if (colspan != null && colspan > 0) {
  125. cell.setColspan(colspan);
  126. }
  127. if (rowspan != null && rowspan > 0) {
  128. cell.setRowspan(rowspan);
  129. }
  130. cell.setPaddingTop(2f);
  131. cell.setPaddingBottom(7f);
  132. cell.setPaddingLeft(8f);
  133. cell.setPaddingRight(8f);
  134. cell.setLeading(0f, 1.2F);
  135. if (PdfPCell.ALIGN_MIDDLE != align) {
  136. cell.setHorizontalAlignment(align);
  137. } else {
  138. //设置单元格的垂直方向对齐方式
  139. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
  140. //设置单元格的水平方向对齐方式
  141. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  142. }
  143. return cell;
  144. }
  145. private static PdfPCell cell(Font tableFont, String content, int align, Integer colspan, Integer rowspan) {
  146. PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
  147. if (colspan != null && colspan > 0) {
  148. cell.setColspan(colspan);
  149. }
  150. if (rowspan != null && rowspan > 0) {
  151. cell.setRowspan(rowspan);
  152. }
  153. // cell.setPaddingTop(8f);
  154. cell.setPaddingLeft(8f);
  155. cell.setPaddingRight(8f);
  156. cell.setPaddingBottom(8f);
  157. cell.setLeading(1F, 1.5F);
  158. if (PdfPCell.ALIGN_MIDDLE != align) {
  159. cell.setHorizontalAlignment(align);
  160. } else {
  161. //设置单元格的垂直方向对齐方式
  162. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
  163. //设置单元格的水平方向对齐方式
  164. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  165. }
  166. return cell;
  167. }
  168. private static PdfPCell cell(Font tableFont, String content, int align, Integer colspan, Integer rowspan, Integer paddingLeft, Integer paddingRight) {
  169. PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
  170. if (colspan != null && colspan > 0) {
  171. cell.setColspan(colspan);
  172. }
  173. if (rowspan != null && rowspan > 0) {
  174. cell.setRowspan(rowspan);
  175. }
  176. // cell.setPaddingTop(8f);
  177. cell.setPaddingLeft(paddingLeft);
  178. cell.setPaddingRight(paddingRight);
  179. // cell.setPaddingBottom(8f);
  180. cell.setLeading(1F, 1.5F);
  181. if (PdfPCell.ALIGN_MIDDLE != align) {
  182. cell.setHorizontalAlignment(align);
  183. } else {
  184. //设置单元格的垂直方向对齐方式
  185. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
  186. //设置单元格的水平方向对齐方式
  187. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  188. }
  189. return cell;
  190. }
  191. private static PdfPCell cell(Font tableFont, String content, int align, Integer colspan, Integer rowspan, Integer lineSpacing) {
  192. PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
  193. if (colspan != null && colspan > 0) {
  194. cell.setColspan(colspan);
  195. }
  196. if (rowspan != null && rowspan > 0) {
  197. cell.setRowspan(rowspan);
  198. }
  199. cell.setNoWrap(false);
  200. cell.setLeading(lineSpacing, 0);
  201. if (PdfPCell.ALIGN_MIDDLE != align) {
  202. cell.setHorizontalAlignment(align);
  203. cell.setPaddingLeft(8f);
  204. cell.setPaddingRight(8f);
  205. cell.setPaddingBottom(8f);
  206. } else {
  207. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//设置单元格的垂直对齐方式
  208. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  209. }
  210. return cell;
  211. }
  212. /**
  213. * 生成文字类型的水印
  214. */
  215. public static void createWatermark(Document document, PdfWriter writer, Font waterMarkFont, String content) throws DocumentException, IOException {
  216. PdfContentByte waterMarkPdfContent = writer.getDirectContentUnder();
  217. Phrase phrase = new Phrase(content, waterMarkFont);
  218. float pageWidth = document.right() + document.left();//获取pdf内容正文页面宽度
  219. float pageHeight = document.top() + document.bottom();//获取pdf内容正文页面高度
  220. //两行三列
  221. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  222. pageWidth * 0.25f, pageHeight * 0.2f, 45);
  223. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  224. pageWidth * 0.25f, pageHeight * 0.5f, 45);
  225. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  226. pageWidth * 0.25f, pageHeight * 0.8f, 45);
  227. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  228. pageWidth * 0.65f, pageHeight * 0.2f, 45);
  229. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  230. pageWidth * 0.65f, pageHeight * 0.5f, 45);
  231. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  232. pageWidth * 0.65f, pageHeight * 0.8f, 45);
  233. }
  234. public static void dealHeader(Document document, BaseFont fs, String title, int fontSize) throws DocumentException {
  235. //文件title
  236. Paragraph p = new Paragraph(title, new Font(fs, fontSize, Font.NORMAL));
  237. p.setAlignment(Paragraph.ALIGN_CENTER);
  238. // 文件title 段落下空白
  239. p.setSpacingAfter(10f);
  240. p.setAlignment(Element.ALIGN_CENTER);
  241. document.add(p);
  242. }
  243. public static void dealEduBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  244. //第一行
  245. createPDFCell(tableFont, table, "单位名称", Element.ALIGN_MIDDLE, 1, 1);
  246. createPDFCell(tableFont, table, data.get("address").toString(), Element.ALIGN_MIDDLE, 2, 1);
  247. createPDFCell(tableFont, table, "地点", Element.ALIGN_MIDDLE, 1, 1);
  248. createPDFCell(tableFont, table, data.get("site").toString(), Element.ALIGN_MIDDLE, 2, 1);
  249. //第二行
  250. createPDFCell(tableFont, table, "主持人", Element.ALIGN_MIDDLE, 1, 1);
  251. createPDFCell(tableFont, table, data.get("hostName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  252. createPDFCell(tableFont, table, "记录人", Element.ALIGN_MIDDLE, 1, 1);
  253. createPDFCell(tableFont, table, data.get("recorderName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  254. createPDFCell(tableFont, table, "时间", Element.ALIGN_MIDDLE, 1, 1);
  255. createPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_LEFT, 5, 1);
  256. //内容
  257. createPDFCell(tableFont, table, "内容", Element.ALIGN_MIDDLE, 1, 1);
  258. createPDFCell(tableFont, table, data.get("content").toString(), Element.ALIGN_LEFT, 5, 1);
  259. createPDFCell(tableFont, table, "总结", Element.ALIGN_MIDDLE, 1, 1);
  260. createPDFCell(tableFont, table, data.get("note").toString(), Element.ALIGN_LEFT, 5, 1);
  261. createPDFCell(tableFont, table, "参会人员签字", Element.ALIGN_MIDDLE, 1, 1);
  262. dealEduImageCell((List<String>) data.get("signImage"), table, 4, 80, 30);
  263. // document.add(table);
  264. //第二页
  265. //按6份等分图片数组,一页只显示6张
  266. // final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 2);
  267. // for (List<String> stringList : listList) {
  268. // PdfPTable innerTable = new PdfPTable(6);
  269. // createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
  270. // //一行展示一张图片
  271. // dealEduImageCell(new ArrayList<>(stringList), innerTable, 2, 185, 200);
  272. //// document.newPage();
  273. // document.add(innerTable);
  274. // }
  275. document.add(table);
  276. PdfPTable imageTable = new PdfPTable(6);
  277. final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 2);
  278. createPDFCell(tableFont, imageTable, "图片附件", Element.ALIGN_MIDDLE, 1, listList.size() == 0 ? 1 : listList.size());
  279. for (List<String> stringList : listList) {
  280. PdfPTable innerTable = new PdfPTable(5);
  281. innerTable.setWidthPercentage(100F);
  282. // createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
  283. //一行展示一张图片
  284. dealEduImageCell2(new ArrayList<>(stringList), innerTable, 2, 185, 190);
  285. // document.newPage();
  286. final PdfPCell cell1 = new PdfPCell();
  287. cell1.setNoWrap(false);
  288. cell1.setColspan(5);
  289. cell1.setRowspan(1);
  290. cell1.addElement(innerTable);
  291. imageTable.addCell(cell1);
  292. }
  293. document.add(imageTable);
  294. }
  295. public static void dealResumptionBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  296. // 第一行
  297. PdfUtil.createPDFCell(tableFont, table, "单位名称", Element.ALIGN_MIDDLE, 2, 0);
  298. PdfUtil.createPDFCell(tableFont, table, data.get("orgName").toString(), Element.ALIGN_MIDDLE, 3, 0);
  299. PdfUtil.createPDFCell(tableFont, table, "检查时间", Element.ALIGN_MIDDLE, 2, 0);
  300. PdfUtil.createPDFCell(tableFont, table, ObjectUtil.isNotEmpty(data.get("dateStr")) ? data.get("dateStr").toString() : "", Element.ALIGN_MIDDLE, 3, 0);
  301. // 第二行
  302. PdfUtil.createPDFCell(tableFont, table, "检查人", Element.ALIGN_MIDDLE, 2, 0);
  303. PdfUtil.createPDFCell(tableFont, table, data.get("submiterNames").toString(), Element.ALIGN_MIDDLE, 8, 0);
  304. // 第三行
  305. Object planTypes = data.get("planTypes");
  306. if ("1".equals(planTypes)) {
  307. PdfUtil.createPDFCell(tableFont, table, "履职工作情况", Element.ALIGN_MIDDLE, 10, 0);
  308. PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 0);
  309. } else {
  310. PdfUtil.createPDFCell(tableFont, table, "任务时段", Element.ALIGN_MIDDLE, 2, 0);
  311. }
  312. PdfUtil.createPDFCell(tableFont, table, "检查内容", Element.ALIGN_MIDDLE, 6, 0);
  313. PdfUtil.createPDFCell(tableFont, table, "检查情况", Element.ALIGN_MIDDLE, 2, 0);
  314. final List<SysDictData> dictCache = DictUtils.getDictCache(DictConstants.RESUMPTION_PLAN_EXEC);
  315. final List<String> names = dictCache.stream().map(SysDictData::getDictLabel).collect(Collectors.toList());
  316. if ("1".equals(planTypes)) {
  317. int o = 1;
  318. for (String s : names) {
  319. List<LinkedHashMap<String, Object>> lists = (List<LinkedHashMap<String, Object>>) data.get(s);
  320. if (ObjectUtil.isNotEmpty(lists)) {
  321. for (LinkedHashMap<String, Object> listVo : lists) {
  322. //序号
  323. PdfUtil.createPDFCell(tableFont, table, String.valueOf(o), PdfPCell.ALIGN_MIDDLE, 2, 0);
  324. // 检查内容
  325. String rowContent = listVo.get("pointName") + "";
  326. PdfUtil.createPDFCell(tableFont, table, rowContent, Element.ALIGN_LEFT, 6, 0);
  327. // 检查情况
  328. PdfUtil.createPDFCell(tableFont, table, ObjectUtil.isEmpty(listVo.get("resValue")) ? "" : (((Integer) listVo.get("resValue")) == 0 ? "正常" : "异常"), Element.ALIGN_MIDDLE, 2, 0);
  329. // 检查人
  330. //PdfUtil.createPDFCell(tableFont, table, String.valueOf(listVo.get("submitName")), Element.ALIGN_CENTER, 0, 0);
  331. o++;
  332. }
  333. }
  334. }
  335. } else {
  336. for (String s : names) {
  337. List<LinkedHashMap<String, Object>> lists = (List<LinkedHashMap<String, Object>>) data.get(s);
  338. if (ObjectUtil.isEmpty(lists)) {
  339. //不同的执行时刻
  340. continue;
  341. }
  342. //不同的执行时刻
  343. PdfUtil.createPDFCell(tableFont, table, getLineStr(s), PdfPCell.ALIGN_MIDDLE, 2, lists.size());
  344. int o = 1;
  345. for (LinkedHashMap<String, Object> listVo : lists) {
  346. // 检查内容
  347. String rowContent = o + "、" + listVo.get("pointName");
  348. PdfUtil.createPDFCell(tableFont, table, rowContent, Element.ALIGN_LEFT, 6, 0);
  349. // 检查情况
  350. PdfUtil.createPDFCell(tableFont, table, ObjectUtil.isEmpty(listVo.get("resValue")) ? "" : (((Integer) listVo.get("resValue")) == 0 ? "正常" : "异常"), Element.ALIGN_MIDDLE, 2, 0);
  351. // 检查人
  352. //PdfUtil.createPDFCell(tableFont, table, String.valueOf(listVo.get("submitName")), Element.ALIGN_CENTER, 0, 0);
  353. o++;
  354. }
  355. }
  356. }
  357. List<Map<String, List<Map<String, Object>>>> protectionList = (List<Map<String, List<Map<String, Object>>>>) data.get("protection");
  358. if (ObjectUtil.isNotEmpty(protectionList)) {
  359. //日常登记簿-布撤防数据
  360. PdfUtil.createPDFCell(tableFont, table, "布撤防情况", Element.ALIGN_MIDDLE, 10, 1);
  361. PdfUtil.createPDFCell(tableFont, table, "防区", Element.ALIGN_MIDDLE, 4, 1);
  362. PdfUtil.createPDFCell(tableFont, table, "状态", Element.ALIGN_MIDDLE, 3, 1);
  363. PdfUtil.createPDFCell(tableFont, table, "时间", Element.ALIGN_MIDDLE, 3, 1);
  364. for (Map<String, List<Map<String, Object>>> listMap : protectionList) {
  365. for (Map.Entry<String, List<Map<String, Object>>> listEntry : listMap.entrySet()) {
  366. IotAlarmSubsystem subsystem = JSON.parseObject(listEntry.getKey(), IotAlarmSubsystem.class);
  367. List<Map<String, Object>> value = listEntry.getValue();
  368. //布防状态,0:撤防,1:布防
  369. if (value.isEmpty()) {
  370. //子系统名称
  371. PdfUtil.createPDFCell(tableFont, table, subsystem.getName(), Element.ALIGN_MIDDLE, 4, 1);
  372. //状态
  373. PdfUtil.createPDFCell(tableFont, table, StringUtil.EMPTY_STRING, Element.ALIGN_MIDDLE, 3, 1);
  374. // 时间
  375. PdfUtil.createPDFCell(tableFont, table, StringUtil.EMPTY_STRING, Element.ALIGN_MIDDLE, 3, 1);
  376. } else {
  377. //子系统名称
  378. PdfUtil.createPDFCell(tableFont, table, subsystem.getName(), Element.ALIGN_MIDDLE, 4, value.size());
  379. for (Map<String, Object> map : value) {
  380. //状态
  381. PdfUtil.createPDFCell(tableFont, table, getStatusText(map), Element.ALIGN_MIDDLE, 3, 1);
  382. //时间
  383. PdfUtil.createPDFCell(tableFont, table, getStatusTime(map), Element.ALIGN_MIDDLE, 3, 1);
  384. }
  385. }
  386. }
  387. }
  388. }
  389. PdfUtil.createPDFCell(tableFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 10, 1);
  390. PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  391. PdfUtil.createPDFCell(tableFont, table, "检查项目", Element.ALIGN_MIDDLE, 4, 1);
  392. PdfUtil.createPDFCell(tableFont, table, "存在问题", Element.ALIGN_MIDDLE, 4, 1);
  393. final List<LinkedHashMap> questionPdfVoList = (List<LinkedHashMap>) data.get("questions");
  394. if (ObjectUtil.isEmpty(questionPdfVoList)) {
  395. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  396. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 4, 1);
  397. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 4, 1);
  398. document.add(table);
  399. return;
  400. }
  401. int questionIndex = 1;
  402. for (int i = 0; i < questionPdfVoList.size(); i++) {
  403. PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionIndex++), Element.ALIGN_MIDDLE, 2, 1);
  404. PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionPdfVoList.get(i).get("checkContent")), Element.ALIGN_MIDDLE, 4, 1);
  405. PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionPdfVoList.get(i).get("questionDesc")), Element.ALIGN_MIDDLE, 4, 1);
  406. }
  407. document.add(table);
  408. }
  409. private static String getStatusTime(Map<String, Object> value) {
  410. if (value == null) {
  411. return StringUtil.EMPTY_STRING;
  412. }
  413. Object status = value.get("date");
  414. return status == null ? StringUtil.EMPTY_STRING : status.toString();
  415. }
  416. private static String getStatusText(Map<String, Object> value) {
  417. if (value == null) {
  418. return StringUtil.EMPTY_STRING;
  419. }
  420. Object status = value.get("status");
  421. if (status == null) {
  422. return StringUtil.EMPTY_STRING;
  423. }
  424. for (ProtectionStatus protectionStatus : ProtectionStatus.values()) {
  425. if (ObjectUtil.equal(protectionStatus.ordinal(), Integer.parseInt(status.toString()))) {
  426. return protectionStatus.getText();
  427. }
  428. }
  429. return StringUtil.EMPTY_STRING;
  430. }
  431. public static String getLineStr(String str) {
  432. StringBuilder result = new StringBuilder();
  433. for (int i = 0; i < str.length(); i++) {
  434. result.append(str.charAt(i)).append("\r");
  435. }
  436. return result.toString();
  437. }
  438. // public static void dealOutInBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  439. // table.setSplitLate(false);
  440. // table.setSplitRows(true);
  441. // //第一行
  442. // createPDFCell(tableFont, table, "接待机构", Element.ALIGN_MIDDLE, 1, 1);
  443. // createPDFCell(tableFont, table, data.get("inOrg").toString(), Element.ALIGN_MIDDLE, 2, 1);
  444. // createPDFCell(tableFont, table, "接待日期", Element.ALIGN_MIDDLE, 1, 1);
  445. // createPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_MIDDLE, 2, 1);
  446. // //第二行
  447. // createPDFCell(tableFont, table, "出入事由", Element.ALIGN_MIDDLE, 1, 1);
  448. // createPDFCell(tableFont, table, data.get("reasons").toString(), Element.ALIGN_MIDDLE, 2, 1);
  449. // createPDFCell(tableFont, table, "陪同人员", Element.ALIGN_MIDDLE, 1, 1);
  450. // createPDFCell(tableFont, table, data.get("accompanyingPerson").toString(), Element.ALIGN_MIDDLE, 2, 1);
  451. //
  452. // //第三行
  453. // createPDFCell(tableFont, table, "申请单位", Element.ALIGN_MIDDLE, 1, 1);
  454. // createPDFCell(tableFont, table, data.get("outOrgName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  455. // createPDFCell(tableFont, table, "出入人员", Element.ALIGN_MIDDLE, 1, 1);
  456. // createPDFCell(tableFont, table, data.get("userName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  457. // //第四行
  458. // createPDFCell(tableFont, table, "证件类型", Element.ALIGN_MIDDLE, 1, 1);
  459. // createPDFCell(tableFont, table, data.get("idType").toString(), Element.ALIGN_MIDDLE, 2, 1);
  460. // createPDFCell(tableFont, table, "证件号码", Element.ALIGN_MIDDLE, 1, 1);
  461. // createPDFCell(tableFont, table, data.get("idCard").toString(), Element.ALIGN_MIDDLE, 2, 1);
  462. // //第五行
  463. // createPDFCell(tableFont, table, "进入时间", Element.ALIGN_MIDDLE, 1, 1);
  464. // createPDFCell(tableFont, table, data.get("inTime").toString(), Element.ALIGN_MIDDLE, 2, 1);
  465. // createPDFCell(tableFont, table, "离开时间", Element.ALIGN_MIDDLE, 1, 1);
  466. // createPDFCell(tableFont, table, data.get("outTime").toString(), Element.ALIGN_MIDDLE, 2, 1);
  467. //
  468. // //第六行
  469. // createPDFCell(tableFont, table, "审批人", Element.ALIGN_MIDDLE, 1, 1);
  470. // createPDFCell(tableFont, table, data.get("approveUser").toString(), Element.ALIGN_MIDDLE, 2, 1);
  471. // createPDFCell(tableFont, table, "登记人", Element.ALIGN_MIDDLE, 1, 1);
  472. // createPDFCell(tableFont, table, data.get("createBy").toString(), Element.ALIGN_MIDDLE, 2, 1);
  473. //
  474. // //第七行
  475. // createPDFCell(tableFont, table, "审批人签字", Element.ALIGN_MIDDLE, 1, 1);
  476. // dealImageCell((List<String>) data.get("approveSign"), table, 1, 80, 30, 2, 1);
  477. // createPDFCell(tableFont, table, "登记人签字", Element.ALIGN_MIDDLE, 1, 1);
  478. // dealImageCell((List<String>) data.get("submitSign"), table, 1, 80, 30, 2, 1);
  479. //// //第8行
  480. //// createPDFCell(tableFont, table, "备注", Element.ALIGN_MIDDLE, 1, 1);
  481. //// createPDFCell(tableFont, table, data.get("remark").toString(), Element.ALIGN_MIDDLE, 5, 1);
  482. //
  483. //
  484. // //证件图片
  485. // createPDFCell(tableFont, table, "证件图片", Element.ALIGN_MIDDLE, 1, 1);
  486. // //证件图片 图片填充
  487. // final PdfPTable imageTable1 = getImage((List<String>) data.get("imageFile"), 2, 190, 130, 2,0f);
  488. // final PdfPCell cell1 = new PdfPCell();
  489. // cell1.setNoWrap(false);
  490. // cell1.setPaddingLeft(4f);
  491. // cell1.setPaddingRight(4f);
  492. // cell1.setPaddingBottom(4f);
  493. // cell1.setPaddingTop(4f);
  494. // cell1.setColspan(5);
  495. // cell1.setRowspan(1);
  496. // //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  497. // cell1.addElement(imageTable1);
  498. // table.addCell(cell1);
  499. //
  500. //
  501. //
  502. // List<String> files= (List<String>) data.get("file");
  503. // List<String> files1=new ArrayList<>();
  504. // List<String> files2= new ArrayList<>();
  505. // // 没有备注图片时 需要把介绍信图片上移
  506. // if(ObjectUtil.isNotEmpty(files))
  507. // {
  508. // files1= files.subList(0,1);
  509. // if(files.size()>1)
  510. // {
  511. // files2= files.subList(1,files.size());
  512. // }
  513. // }
  514. // else{
  515. // files2=files;
  516. // }
  517. //
  518. // if(ObjectUtil.isNotEmpty(files1))
  519. // {
  520. // createPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
  521. // final PdfPTable imageTable3 = getImage(files1, 1, 380, 320, 1,0f);
  522. // final PdfPCell cell3 = new PdfPCell();
  523. // cell3.setNoWrap(false);
  524. // cell3.setPaddingLeft(4f);
  525. // cell3.setPaddingRight(4f);
  526. // cell3.setPaddingBottom(4f);
  527. // cell3.setPaddingTop(4f);
  528. // cell3.setColspan(5);
  529. // cell3.setRowspan(1);
  530. // //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  531. // cell3.addElement(imageTable3);
  532. // table.addCell(cell3);
  533. // document.add(table);
  534. // }
  535. // else { // 紧急类型的出入 可能没有介绍信图片
  536. // document.add(table);
  537. // }
  538. //
  539. // if(files2.size()>0)
  540. // {
  541. // PdfPTable innerTable = new PdfPTable(6);
  542. // createPDFCell(tableFont, innerTable, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
  543. //
  544. // // createPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
  545. // final PdfPTable imageTable2 = getImage(files2, 2, 380, 340, 1,8f);
  546. // final PdfPCell cell2 = new PdfPCell();
  547. // cell2.setNoWrap(false);
  548. // cell2.setPaddingLeft(4f);
  549. // cell2.setPaddingRight(4f);
  550. // cell2.setPaddingBottom(4f);
  551. // cell2.setPaddingTop(4f);
  552. // cell2.setColspan(5);
  553. // cell2.setRowspan(1);
  554. // cell2.addElement(imageTable2);
  555. // innerTable.addCell(cell2);
  556. // document.newPage();
  557. // document.add(innerTable);
  558. // }
  559. //
  560. // List<String> checkImages= (List<String>) data.get("checkImage");
  561. //
  562. // PdfPTable innerTable2 = new PdfPTable(6);
  563. // createPDFCell(tableFont, innerTable2, "备注", Element.ALIGN_MIDDLE, 1, checkImages.isEmpty()?1:2);
  564. // createPDFCell(tableFont, innerTable2, data.get("remark").toString(), Element.ALIGN_LEFT, 5, 1);
  565. // // 核查结果图片为非必填,没有图片时不显示
  566. // if(!checkImages.isEmpty())
  567. // {
  568. // final PdfPTable imageTable3 = getImage((List<String>) data.get("checkImage"), 2, 190, 190, 2,0f);
  569. // final PdfPCell cell3 = new PdfPCell();
  570. // cell3.setNoWrap(false);
  571. // cell3.setPaddingLeft(4f);
  572. // cell3.setPaddingRight(4f);
  573. // cell3.setPaddingBottom(4f);
  574. // cell3.setPaddingTop(4f);
  575. // cell3.setColspan(5);
  576. // cell3.setRowspan(1);
  577. // //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  578. // cell3.addElement(imageTable3);
  579. // innerTable2.addCell(cell3);
  580. // }
  581. // document.add(innerTable2);
  582. // }
  583. public static void dealOutInBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  584. table.setSplitLate(false);
  585. table.setSplitRows(true);
  586. //第1行
  587. createOutInPDFCell(tableFont, table, "接待机构", Element.ALIGN_MIDDLE, 1, 1);
  588. createOutInPDFCell(tableFont, table, data.get("inOrg").toString(), Element.ALIGN_MIDDLE, 2, 1);
  589. createOutInPDFCell(tableFont, table, "接待日期", Element.ALIGN_MIDDLE, 1, 1);
  590. createOutInPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_MIDDLE, 2, 1);
  591. //第2行
  592. createOutInPDFCell(tableFont, table, "出入事由", Element.ALIGN_MIDDLE, 1, 1,33F);
  593. createOutInPDFCell(tableFont, table, data.get("reasons").toString(), Element.ALIGN_MIDDLE, 2, 1);
  594. createOutInPDFCell(tableFont, table, "陪同人员", Element.ALIGN_MIDDLE, 1, 1);
  595. createOutInPDFCell(tableFont, table, data.get("accompanyingPerson").toString(), Element.ALIGN_MIDDLE, 2, 1);
  596. //第3行
  597. createOutInPDFCell(tableFont, table, "审批人", Element.ALIGN_MIDDLE, 1, 1);
  598. createOutInPDFCell(tableFont, table, data.get("approveUser").toString(), Element.ALIGN_MIDDLE, 2, 1);
  599. createOutInPDFCell(tableFont, table, "登记人", Element.ALIGN_MIDDLE, 1, 1);
  600. createOutInPDFCell(tableFont, table, data.get("createBy").toString(), Element.ALIGN_MIDDLE, 2, 1);
  601. //第4行
  602. createOutInPDFCell(tableFont, table, "审批人签字", Element.ALIGN_MIDDLE, 1, 1);
  603. dealImageCell((List<String>) data.get("approveSign"), table, 1, 100, 50, 2, 1);
  604. createOutInPDFCell(tableFont, table, "登记人签字", Element.ALIGN_MIDDLE, 1, 1);
  605. dealImageCell((List<String>) data.get("checkSign"), table, 1, 100, 50, 2, 1);
  606. List<String> letterFiles = (List<String>) data.get("letterFile");
  607. List<String> files1 = new ArrayList<>();
  608. List<String> files2 = new ArrayList<>();
  609. // 没有备注图片时 需要把介绍信图片上移
  610. if (ObjectUtil.isNotEmpty(letterFiles)) {
  611. if (letterFiles.size() == 1) {
  612. letterFilesModelOne(tableFont, table, letterFiles);
  613. }
  614. if (letterFiles.size() == 2) {
  615. letterFilesModelTwo(tableFont, table, letterFiles);
  616. }
  617. if (letterFiles.size() > 2) {
  618. letterFilesModelThree(tableFont, table, letterFiles);
  619. }
  620. }
  621. // 个人信息部分
  622. String userInfos = JSON.toJSONString(data.get("userInfos"));
  623. List<CoreIntroduceLetterOutInRequestUser> personInfoList = JSON.parseArray(userInfos, CoreIntroduceLetterOutInRequestUser.class);
  624. if (ObjectUtil.isNotEmpty(personInfoList)) {
  625. personInfoList.forEach(item -> {
  626. createOutInPDFCell(tableFont, table, "工作单位", Element.ALIGN_MIDDLE, 1, 1,33F);
  627. createOutInPDFCell(tableFont, table, item.getCompanyName(), Element.ALIGN_MIDDLE, 2, 1);
  628. //
  629. createOutInPDFCell(tableFont, table, "出入人员", Element.ALIGN_MIDDLE, 1, 1);
  630. createOutInPDFCell(tableFont, table, item.getUserName(), Element.ALIGN_MIDDLE, 2, 1);
  631. //第二行 30
  632. createOutInPDFCell(tableFont, table, "证件类型", Element.ALIGN_MIDDLE, 1, 1);
  633. createOutInPDFCell(tableFont, table, IdTypeEnums.description(item.getIdType()), Element.ALIGN_MIDDLE, 2, 1);
  634. createOutInPDFCell(tableFont, table, "证件号码", Element.ALIGN_MIDDLE, 1, 1);
  635. createOutInPDFCell(tableFont, table, item.getIdCard(), Element.ALIGN_MIDDLE, 2, 1);
  636. SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月dd日 HH时mm分");
  637. String arrivalTime = sdf.format(item.getArrivalTime());
  638. String departureTime = sdf.format(item.getDepartureTime());
  639. createOutInPDFCell(tableFont, table, "进入时间", Element.ALIGN_MIDDLE, 1, 1);
  640. createOutInPDFCell(tableFont, table, arrivalTime, Element.ALIGN_MIDDLE, 2, 1);
  641. createOutInPDFCell(tableFont, table, "离开时间", Element.ALIGN_MIDDLE, 1, 1);
  642. createOutInPDFCell(tableFont, table, departureTime, Element.ALIGN_MIDDLE, 2, 1);
  643. createOutInPDFCell(tableFont, table, "证件图片", Element.ALIGN_MIDDLE, 1, 1);
  644. List<String> images = new ArrayList<>(Arrays.asList(item.getImgFile().split(",")));
  645. try {
  646. PdfPTable image = getImage(images, 2, 168, 104, 2, 0f);
  647. PdfPCell cell = new PdfPCell();
  648. cell.setNoWrap(false);
  649. cell.setPaddingLeft(4f);
  650. cell.setPaddingRight(4f);
  651. cell.setPaddingBottom(8f);
  652. cell.setPaddingTop(8f);
  653. cell.setColspan(5);
  654. cell.setRowspan(1);
  655. cell.addElement(image);
  656. table.addCell(cell);
  657. } catch (Exception e) {
  658. throw new ServiceException("出入管理登记簿,证件图片处理失败");
  659. }
  660. });
  661. }
  662. document.add(table);
  663. String remark = (String) data.get("remark");
  664. List<String> remarkImage = (List<String>) data.get("remarkImage");
  665. if (!StringUtil.isNullOrEmpty(remark) || ObjectUtil.isNotEmpty(remarkImage)) {
  666. PdfPTable remarkTable = new PdfPTable(6);
  667. createOutInPDFCell(tableFont, remarkTable, "备注", Element.ALIGN_MIDDLE, 1, remarkImage.isEmpty() ? 1 : 2);
  668. if (StrUtil.isNotEmpty(remark)) {
  669. createOutInPDFCell(tableFont, remarkTable, data.get("remark").toString(), Element.ALIGN_LEFT, 5, 1);
  670. }
  671. if (ObjectUtil.isNotEmpty(remarkImage)) {
  672. PdfPTable remarkImageTable = getImage(remarkImage, 2, 168, 104, 2, 0f);
  673. PdfPCell remarkImageCell = new PdfPCell();
  674. remarkImageCell.setNoWrap(false);
  675. remarkImageCell.setPaddingLeft(4f);
  676. remarkImageCell.setPaddingRight(4f);
  677. remarkImageCell.setPaddingBottom(8f);
  678. remarkImageCell.setPaddingTop(8f);
  679. remarkImageCell.setColspan(5);
  680. remarkImageCell.setRowspan(1);
  681. remarkImageCell.addElement(remarkImageTable);
  682. remarkTable.addCell(remarkImageCell);
  683. }
  684. document.add(remarkTable);
  685. }
  686. }
  687. public static void dealDrillBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  688. //第一行
  689. createPDFCell(tableFont, table, "演练单位", Element.ALIGN_MIDDLE, 1, 1);
  690. createPDFCell(tableFont, table, data.get("orgName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  691. createPDFCell(tableFont, table, "地点", Element.ALIGN_MIDDLE, 1, 1);
  692. createPDFCell(tableFont, table, data.get("drillSite").toString(), Element.ALIGN_MIDDLE, 2, 1);
  693. //第二行
  694. createPDFCell(tableFont, table, "指挥人", Element.ALIGN_MIDDLE, 1, 1);
  695. createPDFCell(tableFont, table, data.get("hostName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  696. createPDFCell(tableFont, table, "记录人", Element.ALIGN_MIDDLE, 1, 1);
  697. createPDFCell(tableFont, table, data.get("recorderName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  698. //第三行
  699. createPDFCell(tableFont, table, "演练时间", Element.ALIGN_MIDDLE, 1, 1);
  700. createPDFCell(tableFont, table, data.get("drillTime").toString(), Element.ALIGN_LEFT, 5, 1);
  701. createPDFCell(tableFont, table, "演练类型", Element.ALIGN_MIDDLE, 1, 1);
  702. createPDFCell(tableFont, table, data.get("typeText").toString(), Element.ALIGN_LEFT, 5, 1);
  703. //预设案由
  704. createPDFCell(tableFont, table, "预设案由", Element.ALIGN_MIDDLE, 1, 1);
  705. createPDFCell(tableFont, table, data.get("presetCase").toString(), Element.ALIGN_LEFT, 5, 1);
  706. //演练情况
  707. createPDFCell(tableFont, table, "演练情况", Element.ALIGN_MIDDLE, 1, 1);
  708. createPDFCell(tableFont, table, data.get("drillSituation").toString(), Element.ALIGN_LEFT, 5, 1);
  709. //点评总结
  710. createPDFCell(tableFont, table, "点评总结", Element.ALIGN_MIDDLE, 1, 1);
  711. createPDFCell(tableFont, table, data.get("comment").toString(), Element.ALIGN_LEFT, 5, 1);
  712. //参会人员签字
  713. createPDFCell(tableFont, table, "参演人员", Element.ALIGN_MIDDLE, 1, 1);
  714. dealEduImageCell((List<String>) data.get("signImage"), table, 4, 80, 30);
  715. // document.add(table);
  716. //第二页
  717. //按6份等分图片数组,一页只显示6张
  718. // final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 6);
  719. // for (List<String> stringList : listList) {
  720. // PdfPTable innerTable = new PdfPTable(6);
  721. // createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
  722. // //一行展示一张图片
  723. // dealEduImageCell(new ArrayList<>(stringList), innerTable, 2, 185, 200);
  724. // document.newPage();
  725. // document.add(innerTable);
  726. // }
  727. document.add(table);
  728. PdfPTable imageTable = new PdfPTable(6);
  729. final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 2);
  730. createPDFCell(tableFont, imageTable, "图片附件", Element.ALIGN_MIDDLE, 1, listList.size() == 0 ? 1 : listList.size());
  731. for (List<String> stringList : listList) {
  732. PdfPTable innerTable = new PdfPTable(5);
  733. innerTable.setWidthPercentage(100F);
  734. // createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
  735. //一行展示一张图片
  736. dealEduImageCell2(new ArrayList<>(stringList), innerTable, 2, 185, 190);
  737. // document.newPage();
  738. final PdfPCell cell1 = new PdfPCell();
  739. cell1.setNoWrap(false);
  740. cell1.setColspan(5);
  741. cell1.setRowspan(1);
  742. cell1.addElement(innerTable);
  743. imageTable.addCell(cell1);
  744. }
  745. document.add(imageTable);
  746. }
  747. public static void dealEduImageCell(List<String> imageList, PdfPTable table, Integer imageNumsOfRow, Integer imageWith, Integer imageHigh) throws Exception {
  748. //签字区域
  749. PdfPCell outCell = new PdfPCell();
  750. outCell.setNoWrap(false);
  751. outCell.setColspan(6);
  752. // outCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  753. final int imageAddNums = imageList.size() % imageNumsOfRow;
  754. if (imageAddNums != 0) {
  755. for (int i = 0; i < imageNumsOfRow - imageAddNums; i++) {
  756. imageList.add("black.png");
  757. }
  758. }
  759. PdfPTable imageInnerTable = new PdfPTable(imageNumsOfRow);
  760. imageInnerTable.setSplitRows(true);
  761. imageInnerTable.setSplitLate(false);
  762. imageInnerTable.setWidthPercentage(100F);
  763. for (String image : imageList) {
  764. Image imageData = convertFileToByteArray(new File(image));
  765. if (imageData != null) {
  766. imageData.scaleAbsolute(imageWith, imageHigh);
  767. }
  768. PdfPCell innerCell = new PdfPCell(imageData);
  769. innerCell.setNoWrap(false);
  770. innerCell.setPaddingTop(8f);
  771. innerCell.setPaddingLeft(8f);
  772. innerCell.setPaddingRight(8f);
  773. innerCell.setPaddingBottom(8f);
  774. innerCell.setBorder(Rectangle.NO_BORDER);
  775. innerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  776. innerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  777. imageInnerTable.addCell(innerCell);
  778. }
  779. outCell.addElement(imageInnerTable);
  780. table.addCell(outCell);
  781. }
  782. private static void letterFilesModelOne(Font tableFont, PdfPTable table, List<String> letterFiles) throws Exception {
  783. createPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
  784. PdfPTable letterFileTable = getImage(letterFiles, 1, 380, 386, 1, 0f);
  785. PdfPCell cell = new PdfPCell();
  786. cell.setNoWrap(false);
  787. cell.setPaddingLeft(4f);
  788. cell.setPaddingRight(4f);
  789. cell.setPaddingBottom(4f);
  790. cell.setPaddingTop(4f);
  791. cell.setColspan(5);
  792. cell.setRowspan(1);
  793. cell.addElement(letterFileTable);
  794. table.addCell(cell);
  795. }
  796. private static void letterFilesModelTwo(Font tableFont, PdfPTable table, List<String> letterFiles) throws Exception {
  797. createPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
  798. PdfPTable letterFileTable = getImage(letterFiles, 2, 380, 286, 1, 4f);
  799. PdfPCell cell = new PdfPCell();
  800. cell.setNoWrap(false);
  801. cell.setPaddingLeft(4f);
  802. cell.setPaddingRight(4f);
  803. cell.setPaddingBottom(4f);
  804. cell.setPaddingTop(4f);
  805. cell.setColspan(5);
  806. cell.setRowspan(1);
  807. cell.addElement(letterFileTable);
  808. table.addCell(cell);
  809. }
  810. private static void letterFilesModelThree(Font tableFont, PdfPTable table, List<String> letterFiles) throws Exception {
  811. List<String> headFiles = letterFiles.subList(0, 2);
  812. List<String> otherFiles = letterFiles.subList(2, letterFiles.size());
  813. createOutInPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
  814. PdfPTable headLetterFileTable = getImage(headFiles, 2, 380, 286, 1, 4f);
  815. PdfPCell headCell = new PdfPCell();
  816. headCell.setNoWrap(false);
  817. headCell.setPaddingLeft(4f);
  818. headCell.setPaddingRight(4f);
  819. headCell.setPaddingBottom(4f);
  820. headCell.setPaddingTop(4f);
  821. headCell.setColspan(5);
  822. headCell.setRowspan(1);
  823. headCell.addElement(headLetterFileTable);
  824. table.addCell(headCell);
  825. int otherFilesSize = otherFiles.size();
  826. createOutInPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
  827. PdfPTable otherLetterFileTable = getImage(otherFiles, otherFilesSize, 380, 378, 1, 4f);
  828. PdfPCell ohterCell = new PdfPCell();
  829. ohterCell.setNoWrap(false);
  830. ohterCell.setPaddingLeft(4f);
  831. ohterCell.setPaddingRight(4f);
  832. ohterCell.setPaddingBottom(4f);
  833. ohterCell.setPaddingTop(4f);
  834. ohterCell.setColspan(5);
  835. ohterCell.setRowspan(1);
  836. ohterCell.addElement(otherLetterFileTable);
  837. table.addCell(ohterCell);
  838. }
  839. public static void dealEduImageCell2(List<String> imageList, PdfPTable table, Integer imageNumsOfRow, Integer imageWith, Integer imageHigh) throws Exception {
  840. //签字区域
  841. PdfPCell outCell = new PdfPCell();
  842. outCell.setNoWrap(false);
  843. outCell.setColspan(5);
  844. // outCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  845. final int imageAddNums = imageList.size() % imageNumsOfRow;
  846. if (imageAddNums != 0) {
  847. for (int i = 0; i < imageNumsOfRow - imageAddNums; i++) {
  848. imageList.add("black.png");
  849. }
  850. }
  851. PdfPTable imageInnerTable = new PdfPTable(imageNumsOfRow);
  852. imageInnerTable.setSplitRows(true);
  853. imageInnerTable.setSplitLate(false);
  854. imageInnerTable.setWidthPercentage(100F);
  855. for (String image : imageList) {
  856. Image imageData = convertFileToByteArray(new File(image));
  857. if (imageData != null) {
  858. imageData.scaleAbsolute(imageWith, imageHigh);
  859. }
  860. PdfPCell innerCell = new PdfPCell(imageData);
  861. innerCell.setNoWrap(false);
  862. innerCell.setPaddingTop(4f);
  863. innerCell.setPaddingLeft(4f);
  864. innerCell.setPaddingRight(4f);
  865. innerCell.setPaddingBottom(4f);
  866. innerCell.setBorder(Rectangle.NO_BORDER);
  867. innerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  868. innerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  869. imageInnerTable.addCell(innerCell);
  870. }
  871. outCell.addElement(imageInnerTable);
  872. outCell.setBorder(Rectangle.NO_BORDER);
  873. table.addCell(outCell);
  874. }
  875. public static void dealImageCell(List<String> imageList, PdfPTable table, Integer imageNumsOfRow, Integer imageWith, Integer imageHigh, Integer cosSpan, Integer rowSpan) throws Exception {
  876. //签字区域
  877. PdfPCell outCell = new PdfPCell();
  878. outCell.setNoWrap(false);
  879. outCell.setColspan(cosSpan);
  880. outCell.setRowspan(rowSpan);
  881. final int imageAddNums = imageList.size() % imageNumsOfRow;
  882. if (imageAddNums != 0) {
  883. for (int i = 0; i < imageNumsOfRow - imageAddNums; i++) {
  884. imageList.add("black.png");
  885. }
  886. }
  887. PdfPTable imageInnerTable = new PdfPTable(imageNumsOfRow);
  888. imageInnerTable.setSplitRows(true);
  889. imageInnerTable.setSplitLate(false);
  890. imageInnerTable.setWidthPercentage(100F);
  891. for (String image : imageList) {
  892. Image imageData = convertFileToByteArray(new File(image));
  893. if (imageData != null) {
  894. imageData.scaleAbsolute(imageWith, imageHigh);
  895. }
  896. PdfPCell innerCell = new PdfPCell(imageData);
  897. innerCell.setNoWrap(false);
  898. innerCell.setPaddingTop(8f);
  899. innerCell.setPaddingLeft(8f);
  900. innerCell.setPaddingRight(8f);
  901. innerCell.setPaddingBottom(8f);
  902. innerCell.setBorder(Rectangle.NO_BORDER);
  903. innerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  904. innerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  905. imageInnerTable.addCell(innerCell);
  906. }
  907. outCell.addElement(imageInnerTable);
  908. table.addCell(outCell);
  909. }
  910. private static PdfPTable getImage(List<String> images, int totalImages, float imageWidth, float imageHeight, Integer ImageNumsOfRow, Float paddingBottom) throws Exception {
  911. if (images == null) {
  912. images = new ArrayList<>();
  913. }
  914. PdfPTable innerTable = new PdfPTable(ImageNumsOfRow);
  915. innerTable.setWidthPercentage(100f);
  916. innerTable.setSplitRows(true);
  917. innerTable.setSplitLate(false);
  918. //这里根据实际图片数量来判断是否需要补充白色图片,保证每行显示3张图片,用以填充空白
  919. final int reallySize = images.size();
  920. List<String> list = new ArrayList<>(images);
  921. for (int i = 0; i < totalImages - reallySize && totalImages > reallySize; i++) {
  922. list.add("black.png");
  923. }
  924. //分割,每行显示3张图片,获取分割的行数
  925. List<List<String>> rows = new ArrayList<>();
  926. for (int i = 0; i < list.size(); i += ImageNumsOfRow) {
  927. List<String> row = list.subList(i, Math.min(i + ImageNumsOfRow, list.size()));
  928. rows.add(row);
  929. }
  930. for (List<String> row : rows) {
  931. for (String image : row) {
  932. Image imageData = null;
  933. imageData = convertFileToByteArray(new File(image));
  934. if (imageData != null) {
  935. imageData.scaleAbsolute(imageWidth, imageHeight);
  936. }
  937. PdfPCell cell = new PdfPCell(imageData);
  938. cell.setBorder(Rectangle.NO_BORDER);
  939. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  940. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  941. cell.setPaddingBottom(paddingBottom);
  942. innerTable.addCell(cell);
  943. }
  944. }
  945. PdfPTable outerTable = new PdfPTable(1);
  946. PdfPCell innerCell = new PdfPCell(innerTable);
  947. innerCell.setBorder(Rectangle.NO_BORDER);
  948. outerTable.setSplitRows(true);
  949. outerTable.setSplitLate(false);
  950. outerTable.addCell(innerCell);
  951. outerTable.setWidthPercentage(100f);
  952. return outerTable;
  953. }
  954. public static Image convertFileToByteArray(File file) throws Exception {
  955. try {
  956. FileInputStream fis = new FileInputStream(file);
  957. byte[] byteArray = new byte[(int) file.length()];
  958. fis.read(byteArray);
  959. fis.close();
  960. return Image.getInstance(byteArray);
  961. } catch (IOException e) {
  962. return getLocalImage();
  963. }
  964. }
  965. private static Image getLocalImage() {
  966. try {
  967. final ApplicationContext applicationContext = SpringUtil.getApplicationContext();
  968. final Resource[] resources = applicationContext.getResources("classpath:file/black.png");
  969. if (resources == null || resources.length == 0) {
  970. return null;
  971. }
  972. return Image.getInstance(resources[0].getURL());
  973. } catch (IOException | BadElementException e) {
  974. throw new RuntimeException(e);
  975. }
  976. }
  977. public static void dealAccessTimes(Document document, PdfPTable table, Font tableFont, Font tableTitleFont, List<String> data) throws DocumentException {
  978. if (ObjectUtil.isNotEmpty(data)) {
  979. for (int i = 1; i <= data.size(); i++) {
  980. String time = data.get(i - 1);
  981. PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 1);
  982. PdfUtil.createPDFCell(tableFont, table, time, Element.ALIGN_MIDDLE, 17, 1);
  983. }
  984. } else {
  985. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  986. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 17, 1);
  987. }
  988. }
  989. public static void dealAccessPBody(Document document, PdfPTable table, Font tableFont, Font tableTitleFont, List<AccessDataVo> data) throws DocumentException {
  990. PdfUtil.createPDFCell(tableTitleFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  991. PdfUtil.createPDFCell(tableTitleFont, table, "区域名称", Element.ALIGN_MIDDLE, 6, 1);
  992. PdfUtil.createPDFCell(tableTitleFont, table, "调阅项目", Element.ALIGN_MIDDLE, 5, 1);
  993. PdfUtil.createPDFCell(tableTitleFont, table, "调阅情况", Element.ALIGN_MIDDLE, 6, 1);
  994. List<AccessDataVo> normalDataList = data.stream().filter(d -> ObjectUtil.equal(0, d.getStatus())).collect(Collectors.toList());
  995. if (ObjectUtil.isNotEmpty(normalDataList)) {
  996. for (int i = 1; i <= normalDataList.size(); i++) {
  997. AccessDataVo accessDataVo = normalDataList.get(i - 1);
  998. PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 1);
  999. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getAreaName(), Element.ALIGN_MIDDLE, 6, 1);
  1000. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getProject(), Element.ALIGN_MIDDLE, 5, 1);
  1001. PdfUtil.createPDFCell(tableFont, table, "正常", Element.ALIGN_MIDDLE, 6, 1);
  1002. }
  1003. } else {
  1004. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  1005. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  1006. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 5, 1);
  1007. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  1008. }
  1009. PdfUtil.createPDFCell(tableTitleFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 19, 1);
  1010. PdfUtil.createPDFCell(tableTitleFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  1011. PdfUtil.createPDFCell(tableTitleFont, table, "区域名称", Element.ALIGN_MIDDLE, 6, 1);
  1012. PdfUtil.createPDFCell(tableTitleFont, table, "调阅项目", Element.ALIGN_MIDDLE, 5, 1);
  1013. PdfUtil.createPDFCell(tableTitleFont, table, "存在问题", Element.ALIGN_MIDDLE, 6, 1);
  1014. List<AccessDataVo> exceptionDataList = data.stream().filter(d -> ObjectUtil.equal(1, d.getStatus())).collect(Collectors.toList());
  1015. if (ObjectUtil.isNotEmpty(exceptionDataList)) {
  1016. for (int i = 1; i <= exceptionDataList.size(); i++) {
  1017. AccessDataVo accessDataVo = exceptionDataList.get(i - 1);
  1018. PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 1);
  1019. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getAreaName(), Element.ALIGN_MIDDLE, 6, 1);
  1020. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getProject(), Element.ALIGN_MIDDLE, 5, 1);
  1021. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getAbnormalIllustrate(), Element.ALIGN_MIDDLE, 6, 1);
  1022. }
  1023. } else {
  1024. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  1025. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  1026. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 5, 1);
  1027. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  1028. }
  1029. }
  1030. public static void dealSafeCheckPBody(Document document, PdfPTable table, Font
  1031. tableFont, SafeCheckTaskRegisterBookVo data) throws Exception {
  1032. PdfUtil.createPDFCell(tableFont, table, "被查单位", Element.ALIGN_MIDDLE, 3, 1);
  1033. PdfUtil.createPDFCell(tableFont, table, data.getOrgName(), Element.ALIGN_MIDDLE, 4, 1);
  1034. PdfUtil.createPDFCell(tableFont, table, "检查名称", Element.ALIGN_MIDDLE, 3, 1);
  1035. PdfUtil.createPDFCell(tableFont, table, data.getTaskTitle(), Element.ALIGN_MIDDLE, 4, 1);
  1036. PdfUtil.createPDFCell(tableFont, table, "检查类型", Element.ALIGN_MIDDLE, 3, 1);
  1037. PdfUtil.createPDFCell(tableFont, table, data.getCheckTypeText(), Element.ALIGN_MIDDLE, 11, 1);
  1038. PdfUtil.createPDFCell(tableFont, table, "检查开始时间", Element.ALIGN_MIDDLE, 3, 1);
  1039. //PdfUtil.createPDFCell(tableFont, table, data.getStartTimeStr(), Element.ALIGN_MIDDLE, 4, 1);
  1040. table.addCell(cell(tableFont, data.getStartTimeStr(), Element.ALIGN_MIDDLE, 4, 1, 2, 2));
  1041. PdfUtil.createPDFCell(tableFont, table, "检查结束时间", Element.ALIGN_MIDDLE, 3, 1);
  1042. //PdfUtil.createPDFCell(tableFont, table, data.getEndTimeStr(), Element.ALIGN_MIDDLE, 4, 1);
  1043. table.addCell(cell(tableFont, data.getEndTimeStr(), Element.ALIGN_MIDDLE, 4, 1, 2, 2));
  1044. PdfUtil.createPDFCell(tableFont, table, "检查单位", Element.ALIGN_MIDDLE, 3, 1);
  1045. PdfUtil.createPDFCell(tableFont, table, data.getCheckOrgName(), Element.ALIGN_MIDDLE, 4, 1);
  1046. PdfUtil.createPDFCell(tableFont, table, "检查组成员", Element.ALIGN_MIDDLE, 3, 1);
  1047. PdfUtil.createPDFCell(tableFont, table, data.getCheckTeam(), Element.ALIGN_MIDDLE, 4, 1);
  1048. PdfUtil.createPDFCell(tableFont, table, "主查人", Element.ALIGN_MIDDLE, 3, 1);
  1049. PdfUtil.createPDFCell(tableFont, table, data.getCheckUserInfo(), Element.ALIGN_MIDDLE, 4, 1);
  1050. PdfUtil.createPDFCell(tableFont, table, "主查人签字", Element.ALIGN_MIDDLE, 3, 1);
  1051. List<String> list = new ArrayList<>();
  1052. list.add(data.getSignImg());
  1053. dealEduImageCell(list, table, 1, 80, 30);
  1054. PdfUtil.createPDFCell(tableFont, table, "检查工作情况", Element.ALIGN_MIDDLE, 14, 1);
  1055. // List<CheckDataVo> normalDataList = data.getCheckDatas().stream().filter(d -> ObjectUtil.equal(Boolean.TRUE, d.getCheckStatus())).collect(Collectors.toList());
  1056. PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  1057. PdfUtil.createPDFCell(tableFont, table, "检查内容", Element.ALIGN_MIDDLE, 10, 1);
  1058. PdfUtil.createPDFCell(tableFont, table, "检查情况", Element.ALIGN_MIDDLE, 2, 1);
  1059. if (ObjectUtil.isNotEmpty(data.getCheckDatas()) && data.getCheckDatas().size() > 0) {
  1060. for (CheckDataVo checkDataVo : data.getCheckDatas()) {
  1061. PdfUtil.createPDFCell(tableFont, table, String.valueOf(data.getCheckDatas().indexOf(checkDataVo) + 1), Element.ALIGN_MIDDLE, 2, 1);
  1062. PdfUtil.createPDFCell(tableFont, table, checkDataVo.getPointName(), Element.ALIGN_MIDDLE, 10, 1);
  1063. PdfUtil.createPDFCell(tableFont, table, ObjectUtil.equal(Boolean.TRUE, checkDataVo.getCheckStatus()) ? "正常" : "异常", Element.ALIGN_MIDDLE, 2, 1);
  1064. }
  1065. } else {
  1066. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  1067. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 10, 1);
  1068. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  1069. }
  1070. PdfUtil.createPDFCell(tableFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 14, 1);
  1071. PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  1072. PdfUtil.createPDFCell(tableFont, table, "检查项目", Element.ALIGN_MIDDLE, 6, 1);
  1073. PdfUtil.createPDFCell(tableFont, table, "存在问题", Element.ALIGN_MIDDLE, 6, 1);
  1074. final Optional<CheckDataVo> optional = data.getCheckDatas().stream().filter(res -> ObjectUtil.isNotEmpty(res.getResRemark())).findAny();
  1075. if (!optional.isPresent()) {
  1076. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  1077. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  1078. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  1079. document.add(table);
  1080. return;
  1081. }
  1082. int questionIndex = 1;
  1083. for (int i = 1; i <= data.getCheckDatas().size(); i++) {
  1084. final CheckDataVo checkDataVo = data.getCheckDatas().get(i - 1);
  1085. if (ObjectUtil.isEmpty(checkDataVo.getResRemark())) {
  1086. continue;
  1087. }
  1088. PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionIndex++), Element.ALIGN_MIDDLE, 2, 1);
  1089. PdfUtil.createPDFCell(tableFont, table, checkDataVo.getItemName(), Element.ALIGN_MIDDLE, 6, 1);
  1090. PdfUtil.createPDFCell(tableFont, table, checkDataVo.getResRemark(), Element.ALIGN_MIDDLE, 6, 1);
  1091. }
  1092. document.add(table);
  1093. }
  1094. public static void addPageNum(String srcPdfPath, String targetPdfPath, BaseFont fs, Font tableFont) {
  1095. try {
  1096. // 输出文件 流
  1097. FileOutputStream fos = new FileOutputStream(targetPdfPath);
  1098. // 读取 源PDF文件,进行一页一页复制,才能触发 添加页码的 页面监听事件
  1099. PdfReader reader = new PdfReader(srcPdfPath);
  1100. // 获取 源文件总页数
  1101. int num = reader.getNumberOfPages();
  1102. // 新建文档,默认A4大小
  1103. Document document = createDocument(0, 0, 50, 50);
  1104. PdfWriter writer = PdfWriter.getInstance(document, fos);
  1105. writer.setPageEvent(new PdfPageHelperEvent(num, writer, fs, tableFont));
  1106. document.open();
  1107. // PDF内容体
  1108. PdfContentByte pdfContent = writer.getDirectContent();
  1109. // 页面数是从1开始的
  1110. for (int i = 1; i <= num; i++) {
  1111. document.newPage();
  1112. // 设置空页码进行展示
  1113. writer.setPageEmpty(false);
  1114. PdfImportedPage page = writer.getImportedPage(reader, i);
  1115. // 复制好的页面,添加到内容去,触发事件监听
  1116. pdfContent.addTemplate(page, 0, 42);
  1117. }
  1118. document.close();
  1119. reader.close();
  1120. final File file = new File(srcPdfPath);
  1121. file.delete();
  1122. } catch (Exception e) {
  1123. e.printStackTrace();
  1124. }
  1125. }
  1126. }