| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238 |
- package com.xunmei.file.utils;
- import cn.hutool.core.collection.ListUtil;
- import cn.hutool.core.util.ObjectUtil;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.extra.spring.SpringUtil;
- import com.alibaba.fastjson2.JSON;
- import com.alibaba.fastjson2.JSONArray;
- import com.lowagie.text.*;
- import com.lowagie.text.pdf.*;
- import com.xunmei.common.core.constant.DictConstants;
- import com.xunmei.common.core.domain.iot.domain.IotAlarmSubsystem;
- import com.xunmei.common.core.domain.letter.domain.CoreIntroduceLetterOutInRequestUser;
- import com.xunmei.common.core.domain.letter.dto.CoreIntroduceLetterUserAddDto;
- import com.xunmei.common.core.exception.ServiceException;
- import com.xunmei.common.security.utils.DictUtils;
- import com.xunmei.file.enums.IdTypeEnums;
- import com.xunmei.file.vo.ItextPdfTableVo;
- import com.xunmei.system.api.domain.AccessDataVo;
- import com.xunmei.system.api.domain.CheckDataVo;
- import com.xunmei.system.api.domain.SafeCheckTaskRegisterBookVo;
- import com.xunmei.system.api.domain.SysDictData;
- import com.xunmei.system.api.enums.ProtectionStatus;
- import io.netty.util.internal.StringUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.io.FileUtils;
- 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.text.SimpleDateFormat;
- import java.util.List;
- import java.util.*;
- import java.util.stream.Collectors;
- @Slf4j
- public class PdfUtil {
- public static Document createDocument(float marginLeft, float marginRight, float marginTop, float marginBottom) {
- return new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom);
- }
- public static ItextPdfTableVo createTable(String filename, int numColumns, int fontSize) throws Exception {
- Document document = createDocument(0, 0, 50, 12);
- File file = FileUtils.getFile(filename);
- FileOutputStream fos = new FileOutputStream(file);
- 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;
- }
- /**
- * 创建单元格 - 固定单元格大小
- * @param tableFont
- * @param table
- * @param content
- * @param align
- * @param colspan
- * @param rowspan
- * @param
- * @return
- */
- public static PdfPCell createOutInPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan) {
- final PdfPCell cell = outInCell(tableFont, content, align, colspan, rowspan);
- table.addCell(cell);
- return cell;
- }
- public static PdfPCell createOutInPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan, Float fixedHeight) {
- final PdfPCell cell = OutInCell(tableFont, content, align, colspan, rowspan, fixedHeight);
- table.addCell(cell);
- return cell;
- }
- private static PdfPCell OutInCell(Font tableFont, String content, int align, Integer colspan, Integer rowspan, Float fixedHeight) {
- 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(2f);
- cell.setPaddingBottom(7f);
- cell.setPaddingLeft(8f);
- cell.setPaddingRight(8f);
- cell.setFixedHeight(fixedHeight);
- cell.setLeading(0F, 1.2F);
- if (PdfPCell.ALIGN_MIDDLE != align) {
- cell.setHorizontalAlignment(align);
- } else {
- //设置单元格的垂直方向对齐方式
- cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
- //设置单元格的水平方向对齐方式
- cell.setHorizontalAlignment(Element.ALIGN_CENTER);
- }
- return cell;
- }
- private static PdfPCell outInCell(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(2f);
- cell.setPaddingBottom(7f);
- cell.setPaddingLeft(8f);
- cell.setPaddingRight(8f);
- cell.setLeading(0f, 1.2F);
- 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) {
- 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.setLeading(1F, 1.5F);
- 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 paddingLeft, Integer paddingRight) {
- 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(paddingLeft);
- cell.setPaddingRight(paddingRight);
- // cell.setPaddingBottom(8f);
- cell.setLeading(1F, 1.5F);
- 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.setNoWrap(false);
- 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 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("address").toString(), Element.ALIGN_MIDDLE, 2, 1);
- createPDFCell(tableFont, table, "地点", Element.ALIGN_MIDDLE, 1, 1);
- createPDFCell(tableFont, table, data.get("site").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, 1);
- createPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_LEFT, 5, 1);
- //内容
- createPDFCell(tableFont, table, "内容", Element.ALIGN_MIDDLE, 1, 1);
- createPDFCell(tableFont, table, data.get("content").toString(), Element.ALIGN_LEFT, 5, 1);
- createPDFCell(tableFont, table, "总结", Element.ALIGN_MIDDLE, 1, 1);
- createPDFCell(tableFont, table, data.get("note").toString(), Element.ALIGN_LEFT, 5, 1);
- createPDFCell(tableFont, table, "参会人员签字", Element.ALIGN_MIDDLE, 1, 1);
- dealEduImageCell((List<String>) data.get("signImage"), table, 4, 80, 30);
- // document.add(table);
- //第二页
- //按6份等分图片数组,一页只显示6张
- // final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 2);
- // for (List<String> stringList : listList) {
- // PdfPTable innerTable = new PdfPTable(6);
- // createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
- // //一行展示一张图片
- // dealEduImageCell(new ArrayList<>(stringList), innerTable, 2, 185, 200);
- //// document.newPage();
- // document.add(innerTable);
- // }
- document.add(table);
- PdfPTable imageTable = new PdfPTable(6);
- final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 2);
- createPDFCell(tableFont, imageTable, "图片附件", Element.ALIGN_MIDDLE, 1, listList.size() == 0 ? 1 : listList.size());
- for (List<String> stringList : listList) {
- PdfPTable innerTable = new PdfPTable(5);
- innerTable.setWidthPercentage(100F);
- // createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
- //一行展示一张图片
- dealEduImageCell2(new ArrayList<>(stringList), innerTable, 2, 185, 190);
- // document.newPage();
- final PdfPCell cell1 = new PdfPCell();
- cell1.setNoWrap(false);
- cell1.setColspan(5);
- cell1.setRowspan(1);
- cell1.addElement(innerTable);
- imageTable.addCell(cell1);
- }
- document.add(imageTable);
- }
- public static void dealResumptionBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
- // 第一行
- PdfUtil.createPDFCell(tableFont, table, "单位名称", Element.ALIGN_MIDDLE, 2, 0);
- PdfUtil.createPDFCell(tableFont, table, data.get("orgName").toString(), Element.ALIGN_MIDDLE, 3, 0);
- PdfUtil.createPDFCell(tableFont, table, "检查时间", Element.ALIGN_MIDDLE, 2, 0);
- PdfUtil.createPDFCell(tableFont, table, ObjectUtil.isNotEmpty(data.get("dateStr")) ? data.get("dateStr").toString() : "", Element.ALIGN_MIDDLE, 3, 0);
- // 第二行
- PdfUtil.createPDFCell(tableFont, table, "检查人", Element.ALIGN_MIDDLE, 2, 0);
- PdfUtil.createPDFCell(tableFont, table, data.get("submiterNames").toString(), Element.ALIGN_MIDDLE, 8, 0);
- // 第三行
- Object planTypes = data.get("planTypes");
- if ("1".equals(planTypes)) {
- PdfUtil.createPDFCell(tableFont, table, "履职工作情况", Element.ALIGN_MIDDLE, 10, 0);
- PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 0);
- } else {
- PdfUtil.createPDFCell(tableFont, table, "任务时段", Element.ALIGN_MIDDLE, 2, 0);
- }
- PdfUtil.createPDFCell(tableFont, table, "检查内容", Element.ALIGN_MIDDLE, 6, 0);
- PdfUtil.createPDFCell(tableFont, table, "检查情况", Element.ALIGN_MIDDLE, 2, 0);
- final List<SysDictData> dictCache = DictUtils.getDictCache(DictConstants.RESUMPTION_PLAN_EXEC);
- final List<String> names = dictCache.stream().map(SysDictData::getDictLabel).collect(Collectors.toList());
- if ("1".equals(planTypes)) {
- int o = 1;
- for (String s : names) {
- List<LinkedHashMap<String, Object>> lists = (List<LinkedHashMap<String, Object>>) data.get(s);
- if (ObjectUtil.isNotEmpty(lists)) {
- for (LinkedHashMap<String, Object> listVo : lists) {
- //序号
- PdfUtil.createPDFCell(tableFont, table, String.valueOf(o), PdfPCell.ALIGN_MIDDLE, 2, 0);
- // 检查内容
- String rowContent = listVo.get("pointName") + "";
- PdfUtil.createPDFCell(tableFont, table, rowContent, Element.ALIGN_LEFT, 6, 0);
- // 检查情况
- PdfUtil.createPDFCell(tableFont, table, ObjectUtil.isEmpty(listVo.get("resValue")) ? "" : (((Integer) listVo.get("resValue")) == 0 ? "正常" : "异常"), Element.ALIGN_MIDDLE, 2, 0);
- // 检查人
- //PdfUtil.createPDFCell(tableFont, table, String.valueOf(listVo.get("submitName")), Element.ALIGN_CENTER, 0, 0);
- o++;
- }
- }
- }
- } else {
- 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, 2, 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, 6, 0);
- // 检查情况
- PdfUtil.createPDFCell(tableFont, table, ObjectUtil.isEmpty(listVo.get("resValue")) ? "" : (((Integer) listVo.get("resValue")) == 0 ? "正常" : "异常"), Element.ALIGN_MIDDLE, 2, 0);
- // 检查人
- //PdfUtil.createPDFCell(tableFont, table, String.valueOf(listVo.get("submitName")), Element.ALIGN_CENTER, 0, 0);
- o++;
- }
- }
- }
- List<Map<String, List<Map<String, Object>>>> protectionList = (List<Map<String, List<Map<String, Object>>>>) data.get("protection");
- if (ObjectUtil.isNotEmpty(protectionList)) {
- //日常登记簿-布撤防数据
- PdfUtil.createPDFCell(tableFont, table, "布撤防情况", Element.ALIGN_MIDDLE, 10, 1);
- PdfUtil.createPDFCell(tableFont, table, "防区", Element.ALIGN_MIDDLE, 4, 1);
- PdfUtil.createPDFCell(tableFont, table, "状态", Element.ALIGN_MIDDLE, 3, 1);
- PdfUtil.createPDFCell(tableFont, table, "时间", Element.ALIGN_MIDDLE, 3, 1);
- for (Map<String, List<Map<String, Object>>> listMap : protectionList) {
- for (Map.Entry<String, List<Map<String, Object>>> listEntry : listMap.entrySet()) {
- IotAlarmSubsystem subsystem = JSON.parseObject(listEntry.getKey(), IotAlarmSubsystem.class);
- List<Map<String, Object>> value = listEntry.getValue();
- //布防状态,0:撤防,1:布防
- if (value.isEmpty()) {
- //子系统名称
- PdfUtil.createPDFCell(tableFont, table, subsystem.getName(), Element.ALIGN_MIDDLE, 4, 1);
- //状态
- PdfUtil.createPDFCell(tableFont, table, StringUtil.EMPTY_STRING, Element.ALIGN_MIDDLE, 3, 1);
- // 时间
- PdfUtil.createPDFCell(tableFont, table, StringUtil.EMPTY_STRING, Element.ALIGN_MIDDLE, 3, 1);
- } else {
- //子系统名称
- PdfUtil.createPDFCell(tableFont, table, subsystem.getName(), Element.ALIGN_MIDDLE, 4, value.size());
- for (Map<String, Object> map : value) {
- //状态
- PdfUtil.createPDFCell(tableFont, table, getStatusText(map), Element.ALIGN_MIDDLE, 3, 1);
- //时间
- PdfUtil.createPDFCell(tableFont, table, getStatusTime(map), Element.ALIGN_MIDDLE, 3, 1);
- }
- }
- }
- }
- }
- PdfUtil.createPDFCell(tableFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 10, 1);
- PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, "检查项目", Element.ALIGN_MIDDLE, 4, 1);
- PdfUtil.createPDFCell(tableFont, table, "存在问题", Element.ALIGN_MIDDLE, 4, 1);
- final List<LinkedHashMap> questionPdfVoList = (List<LinkedHashMap>) data.get("questions");
- if (ObjectUtil.isEmpty(questionPdfVoList)) {
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 4, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 4, 1);
- document.add(table);
- return;
- }
- int questionIndex = 1;
- for (int i = 0; i < questionPdfVoList.size(); i++) {
- PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionIndex++), Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionPdfVoList.get(i).get("checkContent")), Element.ALIGN_MIDDLE, 4, 1);
- PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionPdfVoList.get(i).get("questionDesc")), Element.ALIGN_MIDDLE, 4, 1);
- }
- document.add(table);
- }
- private static String getStatusTime(Map<String, Object> value) {
- if (value == null) {
- return StringUtil.EMPTY_STRING;
- }
- Object status = value.get("date");
- return status == null ? StringUtil.EMPTY_STRING : status.toString();
- }
- private static String getStatusText(Map<String, Object> value) {
- if (value == null) {
- return StringUtil.EMPTY_STRING;
- }
- Object status = value.get("status");
- if (status == null) {
- return StringUtil.EMPTY_STRING;
- }
- for (ProtectionStatus protectionStatus : ProtectionStatus.values()) {
- if (ObjectUtil.equal(protectionStatus.ordinal(), Integer.parseInt(status.toString()))) {
- return protectionStatus.getText();
- }
- }
- return StringUtil.EMPTY_STRING;
- }
- 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_MIDDLE, 1, 1);
- // createPDFCell(tableFont, table, data.get("inOrg").toString(), Element.ALIGN_MIDDLE, 2, 1);
- // 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("reasons").toString(), Element.ALIGN_MIDDLE, 2, 1);
- // createPDFCell(tableFont, table, "陪同人员", Element.ALIGN_MIDDLE, 1, 1);
- // createPDFCell(tableFont, table, data.get("accompanyingPerson").toString(), Element.ALIGN_MIDDLE, 2, 1);
- //
- // //第三行
- // createPDFCell(tableFont, table, "申请单位", Element.ALIGN_MIDDLE, 1, 1);
- // createPDFCell(tableFont, table, data.get("outOrgName").toString(), Element.ALIGN_MIDDLE, 2, 1);
- // createPDFCell(tableFont, table, "出入人员", Element.ALIGN_MIDDLE, 1, 1);
- // createPDFCell(tableFont, table, data.get("userName").toString(), Element.ALIGN_MIDDLE, 2, 1);
- // //第四行
- // createPDFCell(tableFont, table, "证件类型", Element.ALIGN_MIDDLE, 1, 1);
- // createPDFCell(tableFont, table, data.get("idType").toString(), Element.ALIGN_MIDDLE, 2, 1);
- // createPDFCell(tableFont, table, "证件号码", Element.ALIGN_MIDDLE, 1, 1);
- // createPDFCell(tableFont, table, data.get("idCard").toString(), Element.ALIGN_MIDDLE, 2, 1);
- // //第五行
- // createPDFCell(tableFont, table, "进入时间", Element.ALIGN_MIDDLE, 1, 1);
- // createPDFCell(tableFont, table, data.get("inTime").toString(), Element.ALIGN_MIDDLE, 2, 1);
- // createPDFCell(tableFont, table, "离开时间", Element.ALIGN_MIDDLE, 1, 1);
- // createPDFCell(tableFont, table, data.get("outTime").toString(), Element.ALIGN_MIDDLE, 2, 1);
- //
- // //第六行
- // createPDFCell(tableFont, table, "审批人", Element.ALIGN_MIDDLE, 1, 1);
- // createPDFCell(tableFont, table, data.get("approveUser").toString(), Element.ALIGN_MIDDLE, 2, 1);
- // createPDFCell(tableFont, table, "登记人", Element.ALIGN_MIDDLE, 1, 1);
- // createPDFCell(tableFont, table, data.get("createBy").toString(), Element.ALIGN_MIDDLE, 2, 1);
- //
- // //第七行
- // createPDFCell(tableFont, table, "审批人签字", Element.ALIGN_MIDDLE, 1, 1);
- // dealImageCell((List<String>) data.get("approveSign"), table, 1, 80, 30, 2, 1);
- // createPDFCell(tableFont, table, "登记人签字", Element.ALIGN_MIDDLE, 1, 1);
- // dealImageCell((List<String>) data.get("submitSign"), table, 1, 80, 30, 2, 1);
- //// //第8行
- //// createPDFCell(tableFont, table, "备注", Element.ALIGN_MIDDLE, 1, 1);
- //// createPDFCell(tableFont, table, data.get("remark").toString(), Element.ALIGN_MIDDLE, 5, 1);
- //
- //
- // //证件图片
- // createPDFCell(tableFont, table, "证件图片", Element.ALIGN_MIDDLE, 1, 1);
- // //证件图片 图片填充
- // final PdfPTable imageTable1 = getImage((List<String>) data.get("imageFile"), 2, 190, 130, 2,0f);
- // final PdfPCell cell1 = new PdfPCell();
- // cell1.setNoWrap(false);
- // cell1.setPaddingLeft(4f);
- // cell1.setPaddingRight(4f);
- // cell1.setPaddingBottom(4f);
- // cell1.setPaddingTop(4f);
- // cell1.setColspan(5);
- // cell1.setRowspan(1);
- // //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
- // cell1.addElement(imageTable1);
- // table.addCell(cell1);
- //
- //
- //
- // List<String> files= (List<String>) data.get("file");
- // List<String> files1=new ArrayList<>();
- // List<String> files2= new ArrayList<>();
- // // 没有备注图片时 需要把介绍信图片上移
- // if(ObjectUtil.isNotEmpty(files))
- // {
- // files1= files.subList(0,1);
- // if(files.size()>1)
- // {
- // files2= files.subList(1,files.size());
- // }
- // }
- // else{
- // files2=files;
- // }
- //
- // if(ObjectUtil.isNotEmpty(files1))
- // {
- // createPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
- // final PdfPTable imageTable3 = getImage(files1, 1, 380, 320, 1,0f);
- // final PdfPCell cell3 = new PdfPCell();
- // cell3.setNoWrap(false);
- // cell3.setPaddingLeft(4f);
- // cell3.setPaddingRight(4f);
- // cell3.setPaddingBottom(4f);
- // cell3.setPaddingTop(4f);
- // cell3.setColspan(5);
- // cell3.setRowspan(1);
- // //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
- // cell3.addElement(imageTable3);
- // table.addCell(cell3);
- // document.add(table);
- // }
- // else { // 紧急类型的出入 可能没有介绍信图片
- // document.add(table);
- // }
- //
- // if(files2.size()>0)
- // {
- // PdfPTable innerTable = new PdfPTable(6);
- // createPDFCell(tableFont, innerTable, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
- //
- // // createPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
- // final PdfPTable imageTable2 = getImage(files2, 2, 380, 340, 1,8f);
- // final PdfPCell cell2 = new PdfPCell();
- // cell2.setNoWrap(false);
- // cell2.setPaddingLeft(4f);
- // cell2.setPaddingRight(4f);
- // cell2.setPaddingBottom(4f);
- // cell2.setPaddingTop(4f);
- // cell2.setColspan(5);
- // cell2.setRowspan(1);
- // cell2.addElement(imageTable2);
- // innerTable.addCell(cell2);
- // document.newPage();
- // document.add(innerTable);
- // }
- //
- // List<String> checkImages= (List<String>) data.get("checkImage");
- //
- // PdfPTable innerTable2 = new PdfPTable(6);
- // createPDFCell(tableFont, innerTable2, "备注", Element.ALIGN_MIDDLE, 1, checkImages.isEmpty()?1:2);
- // createPDFCell(tableFont, innerTable2, data.get("remark").toString(), Element.ALIGN_LEFT, 5, 1);
- // // 核查结果图片为非必填,没有图片时不显示
- // if(!checkImages.isEmpty())
- // {
- // final PdfPTable imageTable3 = getImage((List<String>) data.get("checkImage"), 2, 190, 190, 2,0f);
- // final PdfPCell cell3 = new PdfPCell();
- // cell3.setNoWrap(false);
- // cell3.setPaddingLeft(4f);
- // cell3.setPaddingRight(4f);
- // cell3.setPaddingBottom(4f);
- // cell3.setPaddingTop(4f);
- // cell3.setColspan(5);
- // cell3.setRowspan(1);
- // //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
- // cell3.addElement(imageTable3);
- // innerTable2.addCell(cell3);
- // }
- // document.add(innerTable2);
- // }
- public static void dealOutInBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
- table.setSplitLate(false);
- table.setSplitRows(true);
- //第1行
- createOutInPDFCell(tableFont, table, "接待机构", Element.ALIGN_MIDDLE, 1, 1);
- createOutInPDFCell(tableFont, table, data.get("inOrg").toString(), Element.ALIGN_MIDDLE, 2, 1);
- createOutInPDFCell(tableFont, table, "接待日期", Element.ALIGN_MIDDLE, 1, 1);
- createOutInPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_MIDDLE, 2, 1);
- //第2行
- createOutInPDFCell(tableFont, table, "出入事由", Element.ALIGN_MIDDLE, 1, 1,33F);
- createOutInPDFCell(tableFont, table, data.get("reasons").toString(), Element.ALIGN_MIDDLE, 2, 1);
- createOutInPDFCell(tableFont, table, "陪同人员", Element.ALIGN_MIDDLE, 1, 1);
- createOutInPDFCell(tableFont, table, data.get("accompanyingPerson").toString(), Element.ALIGN_MIDDLE, 2, 1);
- //第3行
- createOutInPDFCell(tableFont, table, "审批人", Element.ALIGN_MIDDLE, 1, 1);
- createOutInPDFCell(tableFont, table, data.get("approveUser").toString(), Element.ALIGN_MIDDLE, 2, 1);
- createOutInPDFCell(tableFont, table, "登记人", Element.ALIGN_MIDDLE, 1, 1);
- createOutInPDFCell(tableFont, table, data.get("createBy").toString(), Element.ALIGN_MIDDLE, 2, 1);
- //第4行
- createOutInPDFCell(tableFont, table, "审批人签字", Element.ALIGN_MIDDLE, 1, 1);
- dealImageCell((List<String>) data.get("approveSign"), table, 1, 100, 50, 2, 1);
- createOutInPDFCell(tableFont, table, "登记人签字", Element.ALIGN_MIDDLE, 1, 1);
- dealImageCell((List<String>) data.get("checkSign"), table, 1, 100, 50, 2, 1);
- List<String> letterFiles = (List<String>) data.get("letterFile");
- List<String> files1 = new ArrayList<>();
- List<String> files2 = new ArrayList<>();
- // 没有备注图片时 需要把介绍信图片上移
- if (ObjectUtil.isNotEmpty(letterFiles)) {
- if (letterFiles.size() == 1) {
- letterFilesModelOne(tableFont, table, letterFiles);
- }
- if (letterFiles.size() == 2) {
- letterFilesModelTwo(tableFont, table, letterFiles);
- }
- if (letterFiles.size() > 2) {
- letterFilesModelThree(tableFont, table, letterFiles);
- }
- }
- // 个人信息部分
- String userInfos = JSON.toJSONString(data.get("userInfos"));
- List<CoreIntroduceLetterOutInRequestUser> personInfoList = JSON.parseArray(userInfos, CoreIntroduceLetterOutInRequestUser.class);
- if (ObjectUtil.isNotEmpty(personInfoList)) {
- personInfoList.forEach(item -> {
- createOutInPDFCell(tableFont, table, "工作单位", Element.ALIGN_MIDDLE, 1, 1,33F);
- createOutInPDFCell(tableFont, table, item.getCompanyName(), Element.ALIGN_MIDDLE, 2, 1);
- //
- createOutInPDFCell(tableFont, table, "出入人员", Element.ALIGN_MIDDLE, 1, 1);
- createOutInPDFCell(tableFont, table, item.getUserName(), Element.ALIGN_MIDDLE, 2, 1);
- //第二行 30
- createOutInPDFCell(tableFont, table, "证件类型", Element.ALIGN_MIDDLE, 1, 1);
- createOutInPDFCell(tableFont, table, IdTypeEnums.description(item.getIdType()), Element.ALIGN_MIDDLE, 2, 1);
- createOutInPDFCell(tableFont, table, "证件号码", Element.ALIGN_MIDDLE, 1, 1);
- createOutInPDFCell(tableFont, table, item.getIdCard(), Element.ALIGN_MIDDLE, 2, 1);
- SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月dd日 HH时mm分");
- String arrivalTime = sdf.format(item.getArrivalTime());
- String departureTime = sdf.format(item.getDepartureTime());
- createOutInPDFCell(tableFont, table, "进入时间", Element.ALIGN_MIDDLE, 1, 1);
- createOutInPDFCell(tableFont, table, arrivalTime, Element.ALIGN_MIDDLE, 2, 1);
- createOutInPDFCell(tableFont, table, "离开时间", Element.ALIGN_MIDDLE, 1, 1);
- createOutInPDFCell(tableFont, table, departureTime, Element.ALIGN_MIDDLE, 2, 1);
- createOutInPDFCell(tableFont, table, "证件图片", Element.ALIGN_MIDDLE, 1, 1);
- List<String> images = new ArrayList<>(Arrays.asList(item.getImgFile().split(",")));
- try {
- PdfPTable image = getImage(images, 2, 168, 104, 2, 0f);
- PdfPCell cell = new PdfPCell();
- cell.setNoWrap(false);
- cell.setPaddingLeft(4f);
- cell.setPaddingRight(4f);
- cell.setPaddingBottom(8f);
- cell.setPaddingTop(8f);
- cell.setColspan(5);
- cell.setRowspan(1);
- cell.addElement(image);
- table.addCell(cell);
- } catch (Exception e) {
- throw new ServiceException("出入管理登记簿,证件图片处理失败");
- }
- });
- }
- document.add(table);
- String remark = (String) data.get("remark");
- List<String> remarkImage = (List<String>) data.get("remarkImage");
- if (!StringUtil.isNullOrEmpty(remark) || ObjectUtil.isNotEmpty(remarkImage)) {
- PdfPTable remarkTable = new PdfPTable(6);
- createOutInPDFCell(tableFont, remarkTable, "备注", Element.ALIGN_MIDDLE, 1, remarkImage.isEmpty() ? 1 : 2);
- if (StrUtil.isNotEmpty(remark)) {
- createOutInPDFCell(tableFont, remarkTable, data.get("remark").toString(), Element.ALIGN_LEFT, 5, 1);
- }
- if (ObjectUtil.isNotEmpty(remarkImage)) {
- PdfPTable remarkImageTable = getImage(remarkImage, 2, 168, 104, 2, 0f);
- PdfPCell remarkImageCell = new PdfPCell();
- remarkImageCell.setNoWrap(false);
- remarkImageCell.setPaddingLeft(4f);
- remarkImageCell.setPaddingRight(4f);
- remarkImageCell.setPaddingBottom(8f);
- remarkImageCell.setPaddingTop(8f);
- remarkImageCell.setColspan(5);
- remarkImageCell.setRowspan(1);
- remarkImageCell.addElement(remarkImageTable);
- remarkTable.addCell(remarkImageCell);
- }
- document.add(remarkTable);
- }
- }
- public static void dealDrillBody(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("orgName").toString(), Element.ALIGN_MIDDLE, 2, 1);
- createPDFCell(tableFont, table, "地点", Element.ALIGN_MIDDLE, 1, 1);
- createPDFCell(tableFont, table, data.get("drillSite").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, 1);
- createPDFCell(tableFont, table, data.get("drillTime").toString(), Element.ALIGN_LEFT, 5, 1);
- createPDFCell(tableFont, table, "演练类型", Element.ALIGN_MIDDLE, 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, 4, 80, 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);
- // //一行展示一张图片
- // dealEduImageCell(new ArrayList<>(stringList), innerTable, 2, 185, 200);
- // document.newPage();
- // document.add(innerTable);
- // }
- document.add(table);
- PdfPTable imageTable = new PdfPTable(6);
- final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 2);
- createPDFCell(tableFont, imageTable, "图片附件", Element.ALIGN_MIDDLE, 1, listList.size() == 0 ? 1 : listList.size());
- for (List<String> stringList : listList) {
- PdfPTable innerTable = new PdfPTable(5);
- innerTable.setWidthPercentage(100F);
- // createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
- //一行展示一张图片
- dealEduImageCell2(new ArrayList<>(stringList), innerTable, 2, 185, 190);
- // document.newPage();
- final PdfPCell cell1 = new PdfPCell();
- cell1.setNoWrap(false);
- cell1.setColspan(5);
- cell1.setRowspan(1);
- cell1.addElement(innerTable);
- imageTable.addCell(cell1);
- }
- document.add(imageTable);
- }
- 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);
- imageInnerTable.setWidthPercentage(100F);
- 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 void letterFilesModelOne(Font tableFont, PdfPTable table, List<String> letterFiles) throws Exception {
- createPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
- PdfPTable letterFileTable = getImage(letterFiles, 1, 380, 386, 1, 0f);
- PdfPCell cell = new PdfPCell();
- cell.setNoWrap(false);
- cell.setPaddingLeft(4f);
- cell.setPaddingRight(4f);
- cell.setPaddingBottom(4f);
- cell.setPaddingTop(4f);
- cell.setColspan(5);
- cell.setRowspan(1);
- cell.addElement(letterFileTable);
- table.addCell(cell);
- }
- private static void letterFilesModelTwo(Font tableFont, PdfPTable table, List<String> letterFiles) throws Exception {
- createPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
- PdfPTable letterFileTable = getImage(letterFiles, 2, 380, 286, 1, 4f);
- PdfPCell cell = new PdfPCell();
- cell.setNoWrap(false);
- cell.setPaddingLeft(4f);
- cell.setPaddingRight(4f);
- cell.setPaddingBottom(4f);
- cell.setPaddingTop(4f);
- cell.setColspan(5);
- cell.setRowspan(1);
- cell.addElement(letterFileTable);
- table.addCell(cell);
- }
- private static void letterFilesModelThree(Font tableFont, PdfPTable table, List<String> letterFiles) throws Exception {
- List<String> headFiles = letterFiles.subList(0, 2);
- List<String> otherFiles = letterFiles.subList(2, letterFiles.size());
- createOutInPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
- PdfPTable headLetterFileTable = getImage(headFiles, 2, 380, 286, 1, 4f);
- PdfPCell headCell = new PdfPCell();
- headCell.setNoWrap(false);
- headCell.setPaddingLeft(4f);
- headCell.setPaddingRight(4f);
- headCell.setPaddingBottom(4f);
- headCell.setPaddingTop(4f);
- headCell.setColspan(5);
- headCell.setRowspan(1);
- headCell.addElement(headLetterFileTable);
- table.addCell(headCell);
- int otherFilesSize = otherFiles.size();
- createOutInPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
- PdfPTable otherLetterFileTable = getImage(otherFiles, otherFilesSize, 380, 378, 1, 4f);
- PdfPCell ohterCell = new PdfPCell();
- ohterCell.setNoWrap(false);
- ohterCell.setPaddingLeft(4f);
- ohterCell.setPaddingRight(4f);
- ohterCell.setPaddingBottom(4f);
- ohterCell.setPaddingTop(4f);
- ohterCell.setColspan(5);
- ohterCell.setRowspan(1);
- ohterCell.addElement(otherLetterFileTable);
- table.addCell(ohterCell);
- }
- public static void dealEduImageCell2(List<String> imageList, PdfPTable table, Integer imageNumsOfRow, Integer imageWith, Integer imageHigh) throws Exception {
- //签字区域
- PdfPCell outCell = new PdfPCell();
- outCell.setNoWrap(false);
- outCell.setColspan(5);
- // 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);
- imageInnerTable.setWidthPercentage(100F);
- 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(4f);
- innerCell.setPaddingLeft(4f);
- innerCell.setPaddingRight(4f);
- innerCell.setPaddingBottom(4f);
- innerCell.setBorder(Rectangle.NO_BORDER);
- innerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
- innerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- imageInnerTable.addCell(innerCell);
- }
- outCell.addElement(imageInnerTable);
- outCell.setBorder(Rectangle.NO_BORDER);
- table.addCell(outCell);
- }
- public static void dealImageCell(List<String> imageList, PdfPTable table, Integer imageNumsOfRow, Integer imageWith, Integer imageHigh, Integer cosSpan, Integer rowSpan) throws Exception {
- //签字区域
- PdfPCell outCell = new PdfPCell();
- outCell.setNoWrap(false);
- outCell.setColspan(cosSpan);
- outCell.setRowspan(rowSpan);
- 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);
- imageInnerTable.setWidthPercentage(100F);
- 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, Float paddingBottom) throws Exception {
- if (images == null) {
- images = new ArrayList<>();
- }
- PdfPTable innerTable = new PdfPTable(ImageNumsOfRow);
- innerTable.setWidthPercentage(100f);
- 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);
- cell.setPaddingBottom(paddingBottom);
- 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);
- outerTable.setWidthPercentage(100f);
- 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 dealAccessTimes(Document document, PdfPTable table, Font tableFont, Font tableTitleFont, List<String> data) throws DocumentException {
- if (ObjectUtil.isNotEmpty(data)) {
- for (int i = 1; i <= data.size(); i++) {
- String time = data.get(i - 1);
- PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, time, Element.ALIGN_MIDDLE, 17, 1);
- }
- } else {
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 17, 1);
- }
- }
- 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, 1);
- PdfUtil.createPDFCell(tableTitleFont, table, "区域名称", Element.ALIGN_MIDDLE, 6, 1);
- PdfUtil.createPDFCell(tableTitleFont, table, "调阅项目", Element.ALIGN_MIDDLE, 5, 1);
- PdfUtil.createPDFCell(tableTitleFont, table, "调阅情况", Element.ALIGN_MIDDLE, 6, 1);
- List<AccessDataVo> normalDataList = data.stream().filter(d -> ObjectUtil.equal(0, d.getStatus())).collect(Collectors.toList());
- if (ObjectUtil.isNotEmpty(normalDataList)) {
- for (int i = 1; i <= normalDataList.size(); i++) {
- AccessDataVo accessDataVo = normalDataList.get(i - 1);
- PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, accessDataVo.getAreaName(), Element.ALIGN_MIDDLE, 6, 1);
- PdfUtil.createPDFCell(tableFont, table, accessDataVo.getProject(), Element.ALIGN_MIDDLE, 5, 1);
- PdfUtil.createPDFCell(tableFont, table, "正常", Element.ALIGN_MIDDLE, 6, 1);
- }
- } else {
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 5, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
- }
- PdfUtil.createPDFCell(tableTitleFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 19, 1);
- PdfUtil.createPDFCell(tableTitleFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableTitleFont, table, "区域名称", Element.ALIGN_MIDDLE, 6, 1);
- PdfUtil.createPDFCell(tableTitleFont, table, "调阅项目", Element.ALIGN_MIDDLE, 5, 1);
- PdfUtil.createPDFCell(tableTitleFont, table, "存在问题", Element.ALIGN_MIDDLE, 6, 1);
- List<AccessDataVo> exceptionDataList = data.stream().filter(d -> ObjectUtil.equal(1, d.getStatus())).collect(Collectors.toList());
- if (ObjectUtil.isNotEmpty(exceptionDataList)) {
- for (int i = 1; i <= exceptionDataList.size(); i++) {
- AccessDataVo accessDataVo = exceptionDataList.get(i - 1);
- PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, accessDataVo.getAreaName(), Element.ALIGN_MIDDLE, 6, 1);
- PdfUtil.createPDFCell(tableFont, table, accessDataVo.getProject(), Element.ALIGN_MIDDLE, 5, 1);
- PdfUtil.createPDFCell(tableFont, table, accessDataVo.getAbnormalIllustrate(), Element.ALIGN_MIDDLE, 6, 1);
- }
- } else {
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 5, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
- }
- }
- public static void dealSafeCheckPBody(Document document, PdfPTable table, Font
- tableFont, SafeCheckTaskRegisterBookVo data) throws Exception {
- PdfUtil.createPDFCell(tableFont, table, "被查单位", Element.ALIGN_MIDDLE, 3, 1);
- PdfUtil.createPDFCell(tableFont, table, data.getOrgName(), Element.ALIGN_MIDDLE, 4, 1);
- PdfUtil.createPDFCell(tableFont, table, "检查名称", Element.ALIGN_MIDDLE, 3, 1);
- PdfUtil.createPDFCell(tableFont, table, data.getTaskTitle(), Element.ALIGN_MIDDLE, 4, 1);
- PdfUtil.createPDFCell(tableFont, table, "检查类型", Element.ALIGN_MIDDLE, 3, 1);
- PdfUtil.createPDFCell(tableFont, table, data.getCheckTypeText(), Element.ALIGN_MIDDLE, 11, 1);
- PdfUtil.createPDFCell(tableFont, table, "检查开始时间", Element.ALIGN_MIDDLE, 3, 1);
- //PdfUtil.createPDFCell(tableFont, table, data.getStartTimeStr(), Element.ALIGN_MIDDLE, 4, 1);
- table.addCell(cell(tableFont, data.getStartTimeStr(), Element.ALIGN_MIDDLE, 4, 1, 2, 2));
- PdfUtil.createPDFCell(tableFont, table, "检查结束时间", Element.ALIGN_MIDDLE, 3, 1);
- //PdfUtil.createPDFCell(tableFont, table, data.getEndTimeStr(), Element.ALIGN_MIDDLE, 4, 1);
- table.addCell(cell(tableFont, data.getEndTimeStr(), Element.ALIGN_MIDDLE, 4, 1, 2, 2));
- PdfUtil.createPDFCell(tableFont, table, "检查单位", Element.ALIGN_MIDDLE, 3, 1);
- PdfUtil.createPDFCell(tableFont, table, data.getCheckOrgName(), Element.ALIGN_MIDDLE, 4, 1);
- PdfUtil.createPDFCell(tableFont, table, "检查组成员", Element.ALIGN_MIDDLE, 3, 1);
- PdfUtil.createPDFCell(tableFont, table, data.getCheckTeam(), Element.ALIGN_MIDDLE, 4, 1);
- PdfUtil.createPDFCell(tableFont, table, "主查人", Element.ALIGN_MIDDLE, 3, 1);
- PdfUtil.createPDFCell(tableFont, table, data.getCheckUserInfo(), Element.ALIGN_MIDDLE, 4, 1);
- PdfUtil.createPDFCell(tableFont, table, "主查人签字", Element.ALIGN_MIDDLE, 3, 1);
- List<String> list = new ArrayList<>();
- list.add(data.getSignImg());
- dealEduImageCell(list, table, 1, 80, 30);
- PdfUtil.createPDFCell(tableFont, table, "检查工作情况", Element.ALIGN_MIDDLE, 14, 1);
- // List<CheckDataVo> normalDataList = data.getCheckDatas().stream().filter(d -> ObjectUtil.equal(Boolean.TRUE, d.getCheckStatus())).collect(Collectors.toList());
- PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, "检查内容", Element.ALIGN_MIDDLE, 10, 1);
- PdfUtil.createPDFCell(tableFont, table, "检查情况", Element.ALIGN_MIDDLE, 2, 1);
- if (ObjectUtil.isNotEmpty(data.getCheckDatas()) && data.getCheckDatas().size() > 0) {
- for (CheckDataVo checkDataVo : data.getCheckDatas()) {
- PdfUtil.createPDFCell(tableFont, table, String.valueOf(data.getCheckDatas().indexOf(checkDataVo) + 1), Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, checkDataVo.getPointName(), Element.ALIGN_MIDDLE, 10, 1);
- PdfUtil.createPDFCell(tableFont, table, ObjectUtil.equal(Boolean.TRUE, checkDataVo.getCheckStatus()) ? "正常" : "异常", Element.ALIGN_MIDDLE, 2, 1);
- }
- } else {
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 10, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
- }
- PdfUtil.createPDFCell(tableFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 14, 1);
- PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, "检查项目", Element.ALIGN_MIDDLE, 6, 1);
- PdfUtil.createPDFCell(tableFont, table, "存在问题", Element.ALIGN_MIDDLE, 6, 1);
- final Optional<CheckDataVo> optional = data.getCheckDatas().stream().filter(res -> ObjectUtil.isNotEmpty(res.getResRemark())).findAny();
- if (!optional.isPresent()) {
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
- PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
- document.add(table);
- return;
- }
- int questionIndex = 1;
- for (int i = 1; i <= data.getCheckDatas().size(); i++) {
- final CheckDataVo checkDataVo = data.getCheckDatas().get(i - 1);
- if (ObjectUtil.isEmpty(checkDataVo.getResRemark())) {
- continue;
- }
- PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionIndex++), Element.ALIGN_MIDDLE, 2, 1);
- PdfUtil.createPDFCell(tableFont, table, checkDataVo.getItemName(), Element.ALIGN_MIDDLE, 6, 1);
- PdfUtil.createPDFCell(tableFont, table, checkDataVo.getResRemark(), Element.ALIGN_MIDDLE, 6, 1);
- }
- document.add(table);
- }
- public static void addPageNum(String srcPdfPath, String targetPdfPath, BaseFont fs, Font tableFont) {
- try {
- // 输出文件 流
- FileOutputStream fos = new FileOutputStream(targetPdfPath);
- // 读取 源PDF文件,进行一页一页复制,才能触发 添加页码的 页面监听事件
- PdfReader reader = new PdfReader(srcPdfPath);
- // 获取 源文件总页数
- int num = reader.getNumberOfPages();
- // 新建文档,默认A4大小
- Document document = createDocument(0, 0, 50, 50);
- PdfWriter writer = PdfWriter.getInstance(document, fos);
- writer.setPageEvent(new PdfPageHelperEvent(num, writer, fs, tableFont));
- document.open();
- // PDF内容体
- PdfContentByte pdfContent = writer.getDirectContent();
- // 页面数是从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();
- final File file = new File(srcPdfPath);
- file.delete();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
|