|
|
@@ -7,29 +7,30 @@ import cn.hutool.extra.spring.SpringUtil;
|
|
|
import com.lowagie.text.*;
|
|
|
import com.lowagie.text.pdf.*;
|
|
|
import com.xunmei.file.vo.ItextPdfTableVo;
|
|
|
-import com.xunmei.file.vo.PdfFilePathVo;
|
|
|
import com.xunmei.system.api.domain.AccessDataVo;
|
|
|
import com.xunmei.system.api.domain.CheckDataVo;
|
|
|
-import com.xunmei.system.api.domain.ResumptionPdf;
|
|
|
-import com.xunmei.system.api.domain.SafeCheckTaskRegisterBookVo;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.commons.text.StringEscapeUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.core.io.Resource;
|
|
|
|
|
|
-import java.io.*;
|
|
|
-import java.util.*;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@Slf4j
|
|
|
public class PdfUtil {
|
|
|
|
|
|
|
|
|
public static ItextPdfTableVo createTable(String filename, int numColumns, int fontSize) throws Exception {
|
|
|
- Document document = new Document(PageSize.A4, 0, 0, 50, 0);//SUPPRESS
|
|
|
+ Document document = new Document(PageSize.A4, 0, 0, 50, 100);//SUPPRESS
|
|
|
File file = FileUtils.getFile(filename);
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
String afterStr_1 = StringEscapeUtils.escapeEcmaScript(filename);
|
|
|
@@ -45,8 +46,8 @@ public class PdfUtil {
|
|
|
// 设置各列列宽
|
|
|
// table.setTotalWidth(new float[]{90, 100, 100, 120, 100, 100});
|
|
|
table.setSpacingBefore(16f);
|
|
|
- table.setSplitRows(false);
|
|
|
- table.setSplitLate(true);
|
|
|
+ table.setSplitRows(true);
|
|
|
+ table.setSplitLate(false);
|
|
|
ItextPdfTableVo itextPdfTableVo = new ItextPdfTableVo();
|
|
|
itextPdfTableVo.setDocument(document);
|
|
|
itextPdfTableVo.setWriter(writer);
|
|
|
@@ -331,104 +332,53 @@ public class PdfUtil {
|
|
|
}
|
|
|
|
|
|
public static void dealDrillBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
|
|
|
- table.setSplitLate(false);
|
|
|
- table.setSplitRows(true);
|
|
|
//第一行
|
|
|
createPDFCell(tableFont, table, "演练单位", Element.ALIGN_CENTER, 1, 1);
|
|
|
createPDFCell(tableFont, table, data.get("orgName").toString(), Element.ALIGN_CENTER, 2, 1);
|
|
|
createPDFCell(tableFont, table, "地点", Element.ALIGN_CENTER, 1, 1);
|
|
|
createPDFCell(tableFont, table, data.get("drillSite").toString(), Element.ALIGN_CENTER, 2, 1);
|
|
|
//第二行
|
|
|
- createPDFCell(tableFont, table, "演练时间", Element.ALIGN_CENTER, 1, 1);
|
|
|
- createPDFCell(tableFont, table, data.get("drillTime").toString(), Element.ALIGN_CENTER, 2, 1);
|
|
|
- createPDFCell(tableFont, table, "演练项目", Element.ALIGN_CENTER, 1, 1);
|
|
|
- createPDFCell(tableFont, table, data.get("typeText").toString(), Element.ALIGN_CENTER, 2, 1);
|
|
|
- //第三行
|
|
|
createPDFCell(tableFont, table, "指挥人", Element.ALIGN_CENTER, 1, 1);
|
|
|
createPDFCell(tableFont, table, data.get("hostName").toString(), Element.ALIGN_CENTER, 2, 1);
|
|
|
createPDFCell(tableFont, table, "记录人", Element.ALIGN_CENTER, 1, 1);
|
|
|
createPDFCell(tableFont, table, data.get("recorderName").toString(), Element.ALIGN_CENTER, 2, 1);
|
|
|
-
|
|
|
+ //第三行
|
|
|
+ createPDFCell(tableFont, table, "演练时间", Element.ALIGN_CENTER, 1, 1);
|
|
|
+ createPDFCell(tableFont, table, data.get("drillTime").toString(), Element.ALIGN_LEFT, 5, 1);
|
|
|
+ createPDFCell(tableFont, table, "演练项目", Element.ALIGN_CENTER, 1, 1);
|
|
|
+ createPDFCell(tableFont, table, data.get("typeText").toString(), Element.ALIGN_LEFT, 5, 1);
|
|
|
//预设案由
|
|
|
- PdfPCell contentCell = new PdfPCell();
|
|
|
- contentCell.setColspan(6);
|
|
|
- final Paragraph presetCase = new Paragraph(new Chunk(data.get("presetCase").toString(), tableFont));
|
|
|
- presetCase.setSpacingAfter(8F);
|
|
|
- contentCell.addElement(presetCase);
|
|
|
- contentCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
|
|
|
- table.addCell(contentCell);
|
|
|
- dealDrillImageCell(data.get("drillSituation").toString(), tableFont, (List<String>) data.get("imageData"), table);
|
|
|
- dealDrillImageCell("参会演练人员(签字):", tableFont, (List<String>) data.get("image"), table);
|
|
|
- /* //演练情况
|
|
|
- PdfPCell noteCell = new PdfPCell();
|
|
|
- noteCell.setColspan(6);
|
|
|
- Paragraph noteParagraph = new Paragraph(new Chunk(data.get("drillSituation").toString(), tableFont));
|
|
|
- noteParagraph.setSpacingAfter(8F);
|
|
|
- noteCell.addElement(noteParagraph);
|
|
|
- noteCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
|
|
|
- final List<String> imageDataList = (List<String>) data.get("imageData");
|
|
|
- final int imageDataAddNums = imageDataList.size() % 3;
|
|
|
- for (int i = 0; i < 3 - imageDataAddNums; i++) {
|
|
|
- imageDataList.add("black.png");
|
|
|
- }
|
|
|
- PdfPTable imageDataInnerTable = new PdfPTable(3);
|
|
|
- for (String str : imageDataList) {
|
|
|
- Image imageData = convertFileToByteArray(new File(str));
|
|
|
- if (imageData != null) {
|
|
|
- imageData.scaleAbsolute(130, 130);
|
|
|
- }
|
|
|
- PdfPCell cell = new PdfPCell(imageData);
|
|
|
- cell.setBorder(Rectangle.NO_BORDER);
|
|
|
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
|
|
- imageDataInnerTable.addCell(cell);
|
|
|
- }
|
|
|
- noteCell.addElement(imageDataInnerTable);
|
|
|
- table.addCell(noteCell);
|
|
|
-
|
|
|
- //签字区域
|
|
|
- PdfPCell signCell = new PdfPCell();
|
|
|
- signCell.setColspan(6);
|
|
|
- final Paragraph paragraph1 = new Paragraph(new Chunk("参会演练人员(签字):", tableFont));
|
|
|
- paragraph1.setSpacingAfter(8F);
|
|
|
- signCell.addElement(paragraph1);
|
|
|
- signCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
|
|
|
- final List<String> imageList = (List<String>) data.get("image");
|
|
|
- final int imageAddNums = imageList.size() % 3;
|
|
|
- for (int i = 0; i < 3 - imageAddNums; i++) {
|
|
|
- imageList.add("black.png");
|
|
|
- }
|
|
|
- PdfPTable imageInnerTable = new PdfPTable(3);
|
|
|
- for (String str : imageList) {
|
|
|
- Image imageData = convertFileToByteArray(new File(str));
|
|
|
- if (imageData != null) {
|
|
|
- imageData.scaleAbsolute(130, 130);
|
|
|
- }
|
|
|
- PdfPCell cell = new PdfPCell(imageData);
|
|
|
- cell.setBorder(Rectangle.NO_BORDER);
|
|
|
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
|
|
- imageInnerTable.addCell(cell);
|
|
|
- }
|
|
|
- signCell.addElement(imageInnerTable);
|
|
|
- table.addCell(signCell);*/
|
|
|
-
|
|
|
-
|
|
|
+ createPDFCell(tableFont, table, "预设案由", Element.ALIGN_MIDDLE, 1, 1);
|
|
|
+ createPDFCell(tableFont, table, data.get("presetCase").toString(), Element.ALIGN_LEFT, 5, 1);
|
|
|
+ //演练情况
|
|
|
+ createPDFCell(tableFont, table, "演练情况", Element.ALIGN_MIDDLE, 1, 1);
|
|
|
+ createPDFCell(tableFont, table, data.get("drillSituation").toString(), Element.ALIGN_LEFT, 5, 1);
|
|
|
//点评总结
|
|
|
- PdfPCell commentCell = new PdfPCell();
|
|
|
- commentCell.setColspan(6);
|
|
|
- commentCell.addElement(new Paragraph(new Chunk(data.get("comment").toString(), tableFont)));
|
|
|
- table.addCell(commentCell);
|
|
|
+ createPDFCell(tableFont, table, "点评总结", Element.ALIGN_MIDDLE, 1, 1);
|
|
|
+ createPDFCell(tableFont, table, data.get("comment").toString(), Element.ALIGN_LEFT, 5, 1);
|
|
|
+ //参会人员签字
|
|
|
+ createPDFCell(tableFont, table, "参会人员签字", Element.ALIGN_MIDDLE, 1, 1);
|
|
|
+ dealEduImageCell((List<String>) data.get("signImage"), table, 5, 30, 30);
|
|
|
document.add(table);
|
|
|
+
|
|
|
+ //第二页
|
|
|
+ //按6份等分图片数组,一页只显示6张
|
|
|
+ final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 6);
|
|
|
+ for (List<String> stringList : listList) {
|
|
|
+ PdfPTable innerTable = new PdfPTable(6);
|
|
|
+ createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
|
|
|
+ innerTable.setSpacingBefore(10f);
|
|
|
+ //一行展示一张图片
|
|
|
+ dealEduImageCell(new ArrayList<>(stringList), innerTable, 2, 150, 200);
|
|
|
+ document.newPage();
|
|
|
+ document.add(innerTable);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public static void dealDrillImageCell(String chunk, Font tableFont, List<String> imageList, PdfPTable table) throws Exception {
|
|
|
+ public static void dealDrillImageCell( List<String> imageList, PdfPTable table) throws Exception {
|
|
|
//签字区域
|
|
|
PdfPCell outCell = new PdfPCell();
|
|
|
- outCell.setColspan(6);
|
|
|
- final Paragraph paragraph1 = new Paragraph(new Chunk(chunk, tableFont));
|
|
|
- paragraph1.setSpacingAfter(8F);
|
|
|
- outCell.addElement(paragraph1);
|
|
|
+ outCell.setColspan(5);
|
|
|
outCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
|
|
|
final int imageAddNums = imageList.size() % 3;
|
|
|
if (imageAddNums != 0) {
|
|
|
@@ -465,8 +415,8 @@ public class PdfUtil {
|
|
|
}
|
|
|
}
|
|
|
PdfPTable imageInnerTable = new PdfPTable(imageNumsOfRow);
|
|
|
- imageInnerTable.setSplitRows(false);
|
|
|
- imageInnerTable.setSplitLate(true);
|
|
|
+ imageInnerTable.setSplitRows(true);
|
|
|
+ imageInnerTable.setSplitLate(false);
|
|
|
for (String image : imageList) {
|
|
|
Image imageData = convertFileToByteArray(new File(image));
|
|
|
if (imageData != null) {
|
|
|
@@ -484,8 +434,6 @@ public class PdfUtil {
|
|
|
imageInnerTable.addCell(innerCell);
|
|
|
}
|
|
|
outCell.addElement(imageInnerTable);
|
|
|
- table.setSplitRows(false);
|
|
|
- table.setSplitLate(true);
|
|
|
table.addCell(outCell);
|
|
|
}
|
|
|
|
|
|
@@ -495,8 +443,8 @@ public class PdfUtil {
|
|
|
images = new ArrayList<>();
|
|
|
}
|
|
|
PdfPTable innerTable = new PdfPTable(ImageNumsOfRow);
|
|
|
- innerTable.setSplitRows(false);
|
|
|
- innerTable.setSplitLate(true);
|
|
|
+ innerTable.setSplitRows(true);
|
|
|
+ innerTable.setSplitLate(false);
|
|
|
//这里根据实际图片数量来判断是否需要补充白色图片,保证每行显示3张图片,用以填充空白
|
|
|
final int reallySize = images.size();
|
|
|
List<String> list = new ArrayList<>(images);
|
|
|
@@ -528,8 +476,8 @@ public class PdfUtil {
|
|
|
PdfPTable outerTable = new PdfPTable(1);
|
|
|
PdfPCell innerCell = new PdfPCell(innerTable);
|
|
|
innerCell.setBorder(Rectangle.NO_BORDER);
|
|
|
- outerTable.setSplitRows(false);
|
|
|
- outerTable.setSplitLate(true);
|
|
|
+ outerTable.setSplitRows(true);
|
|
|
+ outerTable.setSplitLate(false);
|
|
|
outerTable.addCell(innerCell);
|
|
|
return outerTable;
|
|
|
}
|
|
|
@@ -561,19 +509,19 @@ public class PdfUtil {
|
|
|
}
|
|
|
}
|
|
|
public static void dealAccessPBody(Document document, PdfPTable table, Font tableFont, Font tableTitleFont, List<AccessDataVo> data)throws DocumentException {
|
|
|
- PdfUtil.createPDFCell(tableTitleFont, table, "序号", Element.ALIGN_MIDDLE, 2, 0);
|
|
|
- PdfUtil.createPDFCell(tableTitleFont, table, "视频主机", Element.ALIGN_MIDDLE, 5, 0);
|
|
|
- PdfUtil.createPDFCell(tableTitleFont, table, "视频通道", Element.ALIGN_MIDDLE, 5, 0);
|
|
|
- PdfUtil.createPDFCell(tableTitleFont, table, "项目", Element.ALIGN_MIDDLE, 5, 0);
|
|
|
- PdfUtil.createPDFCell(tableTitleFont, table, "检查情况", Element.ALIGN_MIDDLE, 2, 0);
|
|
|
- PdfUtil.createPDFCell(tableTitleFont, table, "说明", Element.ALIGN_MIDDLE, 17, 0);
|
|
|
+
|
|
|
+ PdfUtil.createPDFCell(tableTitleFont, table, "序号", Element.ALIGN_MIDDLE, 3, 0);
|
|
|
+ PdfUtil.createPDFCell(tableTitleFont, table, "硬盘录像机", Element.ALIGN_MIDDLE, 16, 0);
|
|
|
+ PdfUtil.createPDFCell(tableTitleFont, table, "通道名称", Element.ALIGN_MIDDLE, 9, 0);
|
|
|
+ PdfUtil.createPDFCell(tableTitleFont, table, "检查项目", Element.ALIGN_MIDDLE, 9, 0);
|
|
|
+ PdfUtil.createPDFCell(tableTitleFont, table, "存在问题", Element.ALIGN_MIDDLE, 9, 0);
|
|
|
for (int i = 1; i <= data.size(); i++) {
|
|
|
- PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 0, 10);
|
|
|
- PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getHostName(), Element.ALIGN_MIDDLE, 5, 0, 10);
|
|
|
- PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getVideoChannelName(), Element.ALIGN_MIDDLE, 5, 0, 10);
|
|
|
- PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getProject(), Element.ALIGN_MIDDLE, 5, 0, 10);
|
|
|
- PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getSituation().equals("0") ? "正常" : "异常", Element.ALIGN_MIDDLE, 2, 0, 10);
|
|
|
- PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getAbnormalIllustrate(), Element.ALIGN_MIDDLE, 17, 0, 10);
|
|
|
+ PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 3, 0, 10);
|
|
|
+ PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getHostName(), Element.ALIGN_MIDDLE, 16, 0, 10);
|
|
|
+ PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getVideoChannelName(), Element.ALIGN_MIDDLE, 9, 0, 10);
|
|
|
+ PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getProject(), Element.ALIGN_MIDDLE, 9, 0, 10);
|
|
|
+ // PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getSituation().equals("0") ? "正常" : "异常", Element.ALIGN_MIDDLE, 2, 0, 10);
|
|
|
+ PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getAbnormalIllustrate(), Element.ALIGN_MIDDLE, 9, 0, 10);
|
|
|
}
|
|
|
document.add(table);
|
|
|
}
|
|
|
@@ -584,7 +532,6 @@ public class PdfUtil {
|
|
|
PdfUtil.createPDFCell(tableTitleFont, table, "检查情况", Element.ALIGN_MIDDLE, 5, 0);
|
|
|
PdfUtil.createPDFCell(tableTitleFont, table, "存在的问题及整改意见", Element.ALIGN_MIDDLE, 15, 0);
|
|
|
PdfUtil.createPDFCell(tableTitleFont, table, "整改期限", Element.ALIGN_MIDDLE, 2, 0);
|
|
|
-// List<Object> dataList = (List<Object>) data.get("content");
|
|
|
for (int i = 1; i <= data.size(); i++) {
|
|
|
PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 0, 10);
|
|
|
PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getItemName(), Element.ALIGN_MIDDLE, 5, 0, 10);
|
|
|
@@ -595,4 +542,39 @@ public class PdfUtil {
|
|
|
}
|
|
|
document.add(table);
|
|
|
}
|
|
|
+
|
|
|
+ public static void addPageNum(String srcPdfPath, String tagetPdfPath) {
|
|
|
+ try {
|
|
|
+ // 输出文件 流
|
|
|
+ FileOutputStream fos = new FileOutputStream(tagetPdfPath);
|
|
|
+
|
|
|
+ // 读取 源PDF文件,进行一页一页复制,才能触发 添加页码的 页面监听事件
|
|
|
+ PdfReader reader = new PdfReader(srcPdfPath);
|
|
|
+ // 获取 源文件总页数
|
|
|
+ int num = reader.getNumberOfPages();
|
|
|
+
|
|
|
+ // 新建文档,默认A4大小
|
|
|
+ Document document = new Document(PageSize.A4);
|
|
|
+ PdfWriter writer = PdfWriter.getInstance(document, fos);
|
|
|
+ // 设置页面监听事件,必须在open方法前
|
|
|
+ // writer.setPageEvent(new PdfPageHelperEvent(num,writer,document,));
|
|
|
+ document.open();
|
|
|
+ // PDF内容体
|
|
|
+ PdfContentByte pdfContent = writer.getDirectContent();
|
|
|
+ //System.out.println("总页数:" + num);
|
|
|
+ // 页面数是从1开始的
|
|
|
+ for (int i = 1; i <= num; i++) {
|
|
|
+ document.newPage();
|
|
|
+ // 设置空页码进行展示
|
|
|
+ writer.setPageEmpty(false);
|
|
|
+ PdfImportedPage page = writer.getImportedPage(reader, i);
|
|
|
+ // 复制好的页面,添加到内容去,触发事件监听
|
|
|
+ pdfContent.addTemplate(page, 0, 42);
|
|
|
+ }
|
|
|
+ document.close();
|
|
|
+ reader.close();
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|