PdfUtil.java 30 KB

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