PdfUtil.java 30 KB

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