| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- package com.xunmei.file.utils;
- import cn.hutool.core.collection.ListUtil;
- import cn.hutool.core.util.ObjectUtil;
- import cn.hutool.extra.spring.SpringUtil;
- import com.lowagie.text.*;
- import com.lowagie.text.pdf.*;
- import com.xunmei.file.vo.ItextPdfTableVo;
- import com.xunmei.system.api.domain.AccessDataVo;
- import com.xunmei.system.api.domain.CheckDataVo;
- 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.context.ApplicationContext;
- import org.springframework.core.io.Resource;
- 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, 100);//SUPPRESS
- File file = FileUtils.getFile(filename);
- FileOutputStream fos = new FileOutputStream(file);
- String afterStr_1 = StringEscapeUtils.escapeEcmaScript(filename);
- log.info("filename,{}", afterStr_1);
- final PdfWriter writer = PdfWriter.getInstance(document, fos);
- document.open();
- // 使用语言包字
- BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- // 外部字体
- BaseFont fs = BaseFont.createFont("/fonts/msyh.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- Font tableFont = new Font(fs, fontSize, Font.NORMAL);
- PdfPTable table = new PdfPTable(numColumns);
- // 设置各列列宽
- // table.setTotalWidth(new float[]{90, 100, 100, 120, 100, 100});
- table.setSpacingBefore(16f);
- table.setSplitRows(true);
- table.setSplitLate(false);
- ItextPdfTableVo itextPdfTableVo = new ItextPdfTableVo();
- itextPdfTableVo.setDocument(document);
- itextPdfTableVo.setWriter(writer);
- itextPdfTableVo.setAbf(abf);
- itextPdfTableVo.setFs(fs);
- itextPdfTableVo.setTableFont(tableFont);
- itextPdfTableVo.setTable(table);
- return itextPdfTableVo;
- }
- public static PdfPCell createPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan) {
- final PdfPCell cell = cell(tableFont, content, align, colspan, rowspan);
- table.addCell(cell);
- return cell;
- }
- public static PdfPCell createPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan, Integer lineSpacing) {
- final PdfPCell cell = cell(tableFont, content, align, colspan, rowspan, lineSpacing);
- table.addCell(cell);
- return cell;
- }
- private static PdfPCell cell(Font tableFont, String content, int align, Integer colspan, Integer rowspan) {
- PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
- if (colspan != null && colspan > 0) {
- cell.setColspan(colspan);
- }
- if (rowspan != null && rowspan > 0) {
- cell.setRowspan(rowspan);
- }
- cell.setPaddingTop(8f);
- cell.setPaddingLeft(8f);
- cell.setPaddingRight(8f);
- cell.setPaddingBottom(8f);
- cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
- if (PdfPCell.ALIGN_MIDDLE != align) {
- cell.setHorizontalAlignment(align);
- } else {
- cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//设置单元格的垂直对齐方式
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
- }
- return cell;
- }
- private static PdfPCell cell(Font tableFont, String content, int align, Integer colspan, Integer rowspan, Integer lineSpacing) {
- PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
- if (colspan != null && colspan > 0) {
- cell.setColspan(colspan);
- }
- if (rowspan != null && rowspan > 0) {
- cell.setRowspan(rowspan);
- }
- cell.setLeading(lineSpacing, 0);
- if (PdfPCell.ALIGN_MIDDLE != align) {
- cell.setHorizontalAlignment(align);
- cell.setPaddingLeft(8f);
- cell.setPaddingRight(8f);
- cell.setPaddingBottom(8f);
- } else {
- cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//设置单元格的垂直对齐方式
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
- }
- return cell;
- }
- /**
- * 生成文字类型的水印
- */
- public static void createWatermark(Document document, PdfWriter writer, Font waterMarkFont, String content) throws DocumentException, IOException {
- PdfContentByte waterMarkPdfContent = writer.getDirectContentUnder();
- Phrase phrase = new Phrase(content, waterMarkFont);
- float pageWidth = document.right() + document.left();//获取pdf内容正文页面宽度
- float pageHeight = document.top() + document.bottom();//获取pdf内容正文页面高度
- //两行三列
- ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
- pageWidth * 0.25f, pageHeight * 0.2f, 45);
- ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
- pageWidth * 0.25f, pageHeight * 0.5f, 45);
- ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
- pageWidth * 0.25f, pageHeight * 0.8f, 45);
- ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
- pageWidth * 0.65f, pageHeight * 0.2f, 45);
- ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
- pageWidth * 0.65f, pageHeight * 0.5f, 45);
- ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
- pageWidth * 0.65f, pageHeight * 0.8f, 45);
- }
- public static void dealHeader(Document document, BaseFont fs, String title, int fontSize) throws DocumentException {
- //文件title
- Paragraph p = new Paragraph(title, new Font(fs, fontSize, Font.NORMAL));
- p.setAlignment(Paragraph.ALIGN_CENTER);
- // 文件title 段落下空白
- p.setSpacingAfter(10f);
- p.setAlignment(Element.ALIGN_CENTER);
- document.add(p);
- }
- public static void dealEduHeader(PdfPTable table, BaseFont fs, String title, int fontSize) {
- //文件title
- PdfPCell pdfPCell = new PdfPCell();
- final Paragraph p = new Paragraph(new Chunk(title, new Font(fs, fontSize)));
- p.setAlignment(Paragraph.ALIGN_CENTER);
- // 文件title 段落下空白
- p.setSpacingAfter(10f);
- pdfPCell.setColspan(6);
- pdfPCell.addElement(p);
- pdfPCell.setBorder(Rectangle.BOTTOM);
- table.addCell(pdfPCell);
- }
- public static void dealEduBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
- //第一行
- createPDFCell(tableFont, table, "时间", Element.ALIGN_MIDDLE, 1, 1);
- createPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_MIDDLE, 2, 1);
- createPDFCell(tableFont, table, "地点", Element.ALIGN_MIDDLE, 1, 1);
- createPDFCell(tableFont, table, data.get("address").toString(), Element.ALIGN_MIDDLE, 2, 1);
- //第二行
- createPDFCell(tableFont, table, "主持人", Element.ALIGN_MIDDLE, 1, 1);
- createPDFCell(tableFont, table, data.get("hostName").toString(), Element.ALIGN_MIDDLE, 2, 1);
- createPDFCell(tableFont, table, "记录人", Element.ALIGN_MIDDLE, 1, 1);
- createPDFCell(tableFont, table, data.get("recorderName").toString(), Element.ALIGN_MIDDLE, 2, 1);
- //内容
- createPDFCell(tableFont, table, "内容", Element.ALIGN_MIDDLE, 1, 140);
- createPDFCell(tableFont, table, data.get("content").toString(), Element.ALIGN_LEFT, 5, 140);
- createPDFCell(tableFont, table, "总结", Element.ALIGN_MIDDLE, 1, 140);
- createPDFCell(tableFont, table, data.get("note").toString(), Element.ALIGN_LEFT, 5, 140);
- createPDFCell(tableFont, table, "参会人员签字", Element.ALIGN_MIDDLE, 1, 140);
- 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 dealResumptionBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
- PdfUtil.createPDFCell(tableFont, table, "检查内容", Element.ALIGN_CENTER, 6, 0);
- PdfUtil.createPDFCell(tableFont, table, "检查情况", Element.ALIGN_CENTER, 0, 0);
- PdfUtil.createPDFCell(tableFont, table, "登记人", Element.ALIGN_CENTER, 0, 0);
- List<String> names = new ArrayList<>();
- names.add("营业前");
- names.add("营业期间");
- names.add("营业终了");
- for (String s : names) {
- List<LinkedHashMap<String, Object>> lists = (List<LinkedHashMap<String, Object>>) data.get(s);
- if (ObjectUtil.isEmpty(lists)) {
- continue;
- }
- //不同的执行时刻
- PdfUtil.createPDFCell(tableFont, table, getLineStr(s), PdfPCell.ALIGN_MIDDLE, 0, lists.size());
- int o = 1;
- for (LinkedHashMap<String, Object> listVo : lists) {
- // 检查内容
- String rowContent = o + "、" + listVo.get("pointName");
- PdfUtil.createPDFCell(tableFont, table, rowContent, Element.ALIGN_LEFT, 5, 0);
- // 检查情况
- PdfUtil.createPDFCell(tableFont, table, ((Integer) listVo.get("resValue")) == 0 ? "√" : "×", Element.ALIGN_CENTER, 0, 0);
- // 检查人
- PdfUtil.createPDFCell(tableFont, table, String.valueOf(listVo.get("submitName")), Element.ALIGN_CENTER, 0, 0);
- o++;
- }
- }
- // 备注数据
- PdfUtil.createPDFCell(tableFont, table, getLineStr("备注"), PdfPCell.ALIGN_MIDDLE, 0, 0);
- PdfUtil.createPDFCell(tableFont, table, data.get("remark").toString(), Element.ALIGN_LEFT, 7, 0);
- document.add(table);
- Paragraph foot = new Paragraph(" 注:检查情况正常打“√”;发现问题打“×”,并在备注中具体说明。", tableFont);
- // Paragraph foot = new Paragraph(" 注:检查情况正常打“√”;发现问题打“×”,并在备注中具体说明。", new Font(fs, 8, Font.NORMAL));
- foot.setAlignment(Paragraph.ALIGN_LEFT);
- //在后方加入16个空格
- document.add(foot);
- }
- public static String getLineStr(String str) {
- StringBuilder result = new StringBuilder();
- for (int i = 0; i < str.length(); i++) {
- result.append(str.charAt(i)).append("\r");
- }
- return result.toString();
- }
- public static void dealOutInBody(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("inOrg").toString(), Element.ALIGN_CENTER, 2, 1);
- createPDFCell(tableFont, table, "接待日期", Element.ALIGN_CENTER, 1, 1);
- createPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_CENTER, 2, 1);
- //第二行
- createPDFCell(tableFont, table, "来访事由", Element.ALIGN_CENTER, 1, 1);
- createPDFCell(tableFont, table, data.get("reasons").toString(), Element.ALIGN_CENTER, 2, 1);
- createPDFCell(tableFont, table, "审批人", Element.ALIGN_CENTER, 1, 1);
- createPDFCell(tableFont, table, data.get("approveUser").toString(), Element.ALIGN_CENTER, 2, 1);
- //第三行
- createPDFCell(tableFont, table, "来访单位", Element.ALIGN_CENTER, 1, 1);
- createPDFCell(tableFont, table, data.get("outOrgName").toString(), Element.ALIGN_CENTER, 2, 1);
- createPDFCell(tableFont, table, "来访人员", Element.ALIGN_CENTER, 1, 1);
- createPDFCell(tableFont, table, data.get("userName").toString(), Element.ALIGN_CENTER, 2, 1);
- //第四行
- createPDFCell(tableFont, table, "证件类型", Element.ALIGN_CENTER, 1, 1);
- createPDFCell(tableFont, table, data.get("idType").toString(), Element.ALIGN_CENTER, 2, 1);
- createPDFCell(tableFont, table, "证件号码", Element.ALIGN_CENTER, 1, 1);
- createPDFCell(tableFont, table, data.get("idCard").toString(), Element.ALIGN_CENTER, 2, 1);
- //第五行
- createPDFCell(tableFont, table, "进入时间", Element.ALIGN_CENTER, 1, 1);
- createPDFCell(tableFont, table, data.get("inTime").toString(), Element.ALIGN_CENTER, 2, 1);
- createPDFCell(tableFont, table, "离开时间", Element.ALIGN_CENTER, 1, 1);
- createPDFCell(tableFont, table, data.get("outTime").toString(), Element.ALIGN_CENTER, 2, 1);
- //第六行
- createPDFCell(tableFont, table, "陪同人员", Element.ALIGN_CENTER, 1, 1);
- createPDFCell(tableFont, table, data.get("accompanyingPerson").toString(), Element.ALIGN_CENTER, 2, 1);
- createPDFCell(tableFont, table, "登记人员", Element.ALIGN_CENTER, 1, 1);
- createPDFCell(tableFont, table, data.get("createBy").toString(), Element.ALIGN_CENTER, 2, 1);
- // /statics/2023/12/05/20231205183106A001.png
- //证件图片
- createPDFCell(tableFont, table, "证件图片", Element.ALIGN_CENTER, 1, 60);
- //证件图片 图片填充
- final PdfPTable imageTable1 = getImage((List<String>) data.get("imageFile"), 2, 150, 130, 2);
- final PdfPCell cell1 = new PdfPCell();
- cell1.setNoWrap(false);
- cell1.setPaddingLeft(8f);
- cell1.setPaddingRight(8f);
- cell1.setPaddingBottom(8f);
- cell1.setPaddingTop(8f);
- cell1.setColspan(5);
- cell1.setRowspan(60);
- //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
- cell1.addElement(imageTable1);
- table.addCell(cell1);
- createPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_CENTER, 1, 60);
- final PdfPTable imageTable2 = getImage((List<String>) data.get("file"), 2, 150, 130, 2);
- final PdfPCell cell2 = new PdfPCell();
- cell2.setNoWrap(false);
- cell2.setPaddingLeft(8f);
- cell2.setPaddingRight(8f);
- cell2.setPaddingBottom(8f);
- cell2.setPaddingTop(8f);
- cell2.setColspan(5);
- cell2.setRowspan(60);
- //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
- cell2.addElement(imageTable2);
- table.addCell(cell2);
- createPDFCell(tableFont, table, "身份核验材料", Element.ALIGN_CENTER, 1, 60);
- final PdfPTable imageTable3 = getImage((List<String>) data.get("checkImage"), 2, 150, 130, 2);
- final PdfPCell cell3 = new PdfPCell();
- cell3.setNoWrap(false);
- cell3.setPaddingLeft(8f);
- cell3.setPaddingRight(8f);
- cell3.setPaddingBottom(8f);
- cell3.setPaddingTop(8f);
- cell3.setColspan(5);
- cell3.setRowspan(60);
- //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
- cell3.addElement(imageTable3);
- table.addCell(cell3);
- document.add(table);
- }
- public static void dealDrillBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
- //第一行
- 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("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);
- //预设案由
- 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);
- //点评总结
- 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( List<String> imageList, PdfPTable table) throws Exception {
- //签字区域
- PdfPCell outCell = new PdfPCell();
- outCell.setColspan(5);
- outCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
- final int imageAddNums = imageList.size() % 3;
- if (imageAddNums != 0) {
- for (int i = 0; i < 3 - imageAddNums; i++) {
- imageList.add("black.png");
- }
- }
- PdfPTable imageInnerTable = new PdfPTable(3);
- for (String image : imageList) {
- Image imageData = convertFileToByteArray(new File(image));
- if (imageData != null) {
- imageData.scaleAbsolute(120, 120);
- }
- PdfPCell innerCell = new PdfPCell(imageData);
- innerCell.setBorder(Rectangle.NO_BORDER);
- innerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
- innerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- imageInnerTable.addCell(innerCell);
- }
- outCell.addElement(imageInnerTable);
- table.addCell(outCell);
- }
- public static void dealEduImageCell(List<String> imageList, PdfPTable table, Integer imageNumsOfRow, Integer imageWith, Integer imageHigh) throws Exception {
- //签字区域
- PdfPCell outCell = new PdfPCell();
- outCell.setNoWrap(false);
- outCell.setColspan(6);
- // outCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
- final int imageAddNums = imageList.size() % imageNumsOfRow;
- if (imageAddNums != 0) {
- for (int i = 0; i < imageNumsOfRow - imageAddNums; i++) {
- imageList.add("black.png");
- }
- }
- PdfPTable imageInnerTable = new PdfPTable(imageNumsOfRow);
- imageInnerTable.setSplitRows(true);
- imageInnerTable.setSplitLate(false);
- for (String image : imageList) {
- Image imageData = convertFileToByteArray(new File(image));
- if (imageData != null) {
- imageData.scaleAbsolute(imageWith, imageHigh);
- }
- PdfPCell innerCell = new PdfPCell(imageData);
- innerCell.setNoWrap(false);
- innerCell.setPaddingTop(8f);
- innerCell.setPaddingLeft(8f);
- innerCell.setPaddingRight(8f);
- innerCell.setPaddingBottom(8f);
- innerCell.setBorder(Rectangle.NO_BORDER);
- innerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
- innerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- imageInnerTable.addCell(innerCell);
- }
- outCell.addElement(imageInnerTable);
- table.addCell(outCell);
- }
- private static PdfPTable getImage(List<String> images, int totalImages, float imageWidth, float imageHeight, Integer ImageNumsOfRow) throws Exception {
- if (images == null) {
- images = new ArrayList<>();
- }
- PdfPTable innerTable = new PdfPTable(ImageNumsOfRow);
- innerTable.setSplitRows(true);
- innerTable.setSplitLate(false);
- //这里根据实际图片数量来判断是否需要补充白色图片,保证每行显示3张图片,用以填充空白
- final int reallySize = images.size();
- List<String> list = new ArrayList<>(images);
- for (int i = 0; i < totalImages - reallySize && totalImages > reallySize; i++) {
- list.add("black.png");
- }
- //分割,每行显示3张图片,获取分割的行数
- List<List<String>> rows = new ArrayList<>();
- for (int i = 0; i < list.size(); i += ImageNumsOfRow) {
- List<String> row = list.subList(i, Math.min(i + ImageNumsOfRow, list.size()));
- rows.add(row);
- }
- for (List<String> row : rows) {
- for (String image : row) {
- Image imageData = null;
- imageData = convertFileToByteArray(new File(image));
- if (imageData != null) {
- imageData.scaleAbsolute(imageWidth, imageHeight);
- }
- PdfPCell cell = new PdfPCell(imageData);
- cell.setBorder(Rectangle.NO_BORDER);
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- innerTable.addCell(cell);
- }
- }
- PdfPTable outerTable = new PdfPTable(1);
- PdfPCell innerCell = new PdfPCell(innerTable);
- innerCell.setBorder(Rectangle.NO_BORDER);
- outerTable.setSplitRows(true);
- outerTable.setSplitLate(false);
- outerTable.addCell(innerCell);
- return outerTable;
- }
- public static Image convertFileToByteArray(File file) throws Exception {
- try {
- FileInputStream fis = new FileInputStream(file);
- byte[] byteArray = new byte[(int) file.length()];
- fis.read(byteArray);
- fis.close();
- return Image.getInstance(byteArray);
- } catch (IOException e) {
- return getLocalImage();
- }
- }
- private static Image getLocalImage() {
- try {
- final ApplicationContext applicationContext = SpringUtil.getApplicationContext();
- final Resource[] resources = applicationContext.getResources("classpath:file/black.png");
- if (resources == null || resources.length == 0) {
- return null;
- }
- return Image.getInstance(resources[0].getURL());
- } catch (IOException | BadElementException e) {
- throw new RuntimeException(e);
- }
- }
- public static void dealAccessPBody(Document document, PdfPTable table, Font tableFont, Font tableTitleFont, List<AccessDataVo> data)throws DocumentException {
- 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, 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);
- }
- public static void dealSafeCheckPBody(Document document, PdfPTable table, Font tableFont, Font tableTitleFont, List<CheckDataVo> 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, 17, 0);
- 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);
- 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);
- PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getPointName(), Element.ALIGN_MIDDLE, 17, 0, 10);
- PdfUtil.createPDFCell(tableFont, table, StringUtils.isEmpty(data.get(i - 1).getResRemark()) ? "√" : "×", Element.ALIGN_MIDDLE, 5, 0, 10);
- PdfUtil.createPDFCell(tableFont, table, StringUtils.isEmpty(data.get(i - 1).getResRemark()) ? "" : data.get(i - 1).getResRemark(), Element.ALIGN_MIDDLE, 15, 0, 10);
- PdfUtil.createPDFCell(tableFont, table, data.get(i - 1).getRectificationDeadline(), Element.ALIGN_MIDDLE, 2, 0, 10);
- }
- 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();
- }
- }
- }
|