Java 填充模板
最近接了個任務,需要對模板進行填充,使用word模板進行填充,嘗試后,使用.docx的文檔。
以下是dome:
方法入口
InputStream inputStream = ConTemplateCtr.getInstance().print(new ByteArrayInputStream(legalDownLoad), conBase, info);
注意這行代碼,在這里面進行填充
這里主要是參數配置
//模板渲染
return DynWordUtils.process(paramMap, in, outPath);
后面的就是方式類
ConTemplateCtr類
DynWordUtils類
package com.midea.dmr.cont.ctr;import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.xwpf.usermodel.*; import org.apache.xmlbeans.XmlCursor; import org.slf4j.Logger; import org.slf4j.LoggerFactory;import java.io.*; import java.util.*;/*** Create by IntelliJ Idea 2018.2** @author: qyp* Date: 2019-10-25 14:48*/ public class DynWordUtils {private static final Logger logger = LoggerFactory.getLogger(DynWordUtils.class);/*** 被list替換的段落 被替換的都是oldParagraph*/private XWPFParagraph oldParagraph;/*** 參數*/private Map<String, Object> paramMap;/*** 當前元素的位置*/int n = 0;/*** 判斷當前是否是遍歷的表格*/boolean isTable = false;/*** 模板對象*/XWPFDocument templateDoc;/*** 默認字體的大小*/public static final int DEFAULT_FONT_SIZE = 10;/*** 重復模式的占位符所在的行索引*/private int currentRowIndex;/*** 入口** @param paramMap 模板中使用的參數* @param templatePaht 模板全路徑* @param outPath 生成的文件存放的本地全路徑*/public static void process(Map<String, Object> paramMap, String templatePaht, String outPath) {DynWordUtils dynWordUtils = new DynWordUtils();dynWordUtils.setParamMap(paramMap);dynWordUtils.createWord(templatePaht, outPath);}/*** 入口** @param paramMap 模板中使用的參數* @param outPath 生成的文件存放的本地全路徑*/public static InputStream process(Map<String, Object> paramMap, InputStream in, String outPath) {DynWordUtils dynWordUtils = new DynWordUtils();dynWordUtils.setParamMap(paramMap);return dynWordUtils.createWord(in, outPath);}/*** 生成動態的word** @param outPath*/public InputStream createWord(InputStream in, String outPath) {ByteArrayOutputStream outStream = null;InputStream inputStream = null;try {Date date = new Date();logger.info("生成動態的createWord_outPath:" + outPath);outStream = new ByteArrayOutputStream();templateDoc = new XWPFDocument(in);parseTemplateWord();logger.info("生成動態的createWord成功。" + date.getTime());templateDoc.write(outStream);inputStream = new ByteArrayInputStream(outStream.toByteArray());} catch (Exception e) {StackTraceElement[] stackTrace = e.getStackTrace();String className = stackTrace[0].getClassName();String methodName = stackTrace[0].getMethodName();int lineNumber = stackTrace[0].getLineNumber();logger.info("錯誤:第:" + lineNumber + "行, 類名:" + className + ", 方法名:" + methodName);logger.info("生成動態的word失敗_" + e.getMessage());} finally {if (outStream != null) {try {outStream.close();} catch (IOException e) {logger.info("關閉流失敗:" + e.getMessage());}}}return inputStream;}/*** 生成動態的word** @param templatePath* @param outPath*/public void createWord(String templatePath, String outPath) {FileOutputStream outStream = null;try {logger.info("生成動態的createWord_templatePath:" + templatePath + "_outPath:" + outPath);outStream = new FileOutputStream(outPath);templateDoc = new XWPFDocument(OPCPackage.open(templatePath));parseTemplateWord();templateDoc.write(outStream);} catch (Exception e) {StackTraceElement[] stackTrace = e.getStackTrace();String className = stackTrace[0].getClassName();String methodName = stackTrace[0].getMethodName();int lineNumber = stackTrace[0].getLineNumber();logger.info("錯誤:第:" + lineNumber + "行, 類名:" + className + ", 方法名:" + methodName);logger.info("createWord_生成動態的word失敗:{}", e.getMessage());} finally {if (outStream != null) {try {outStream.close();} catch (IOException e) {logger.info("createWord_關閉流失敗:" + e.getMessage());}}}}/*** 解析word模板*/public void parseTemplateWord() throws Exception {List<IBodyElement> elements = templateDoc.getBodyElements();for (; n < elements.size(); n++) {IBodyElement element = elements.get(n);// 普通段落if (element instanceof XWPFParagraph) {XWPFParagraph paragraph = (XWPFParagraph) element;oldParagraph = paragraph;if (paragraph.getParagraphText().isEmpty()) {continue;}delParagraph(paragraph);} else if (element instanceof XWPFTable) {// 表格isTable = true;XWPFTable table = (XWPFTable) element;delTable(table, paramMap);isTable = false;}}}/*** 處理段落*/private void delParagraph(XWPFParagraph paragraph) throws Exception {List<XWPFRun> runs = oldParagraph.getRuns();StringBuilder sb = new StringBuilder();for (XWPFRun run : runs) {String text = run.getText(0);if (text == null) {continue;}sb.append(text);run.setText("", 0);}placeholder(paragraph, runs, sb);}/*** 匹配傳入信息集合與模板** @param placeholder 模板需要替換的區域()* @param paramMap 傳入信息集合* @return 模板需要替換區域信息集合對應值*/public void changeValue(XWPFRun currRun, String placeholder, Map<String, Object> paramMap) throws Exception {String placeholderValue = placeholder;if (paramMap == null || paramMap.isEmpty()) {return;}Set<Map.Entry<String, Object>> textSets = paramMap.entrySet();for (Map.Entry<String, Object> textSet : textSets) {//匹配模板與替換值 格式${key}String mapKey = textSet.getKey();String docKey = PoiWordUtils.getDocKey(mapKey);if (placeholderValue.indexOf(docKey) != -1) {Object obj = textSet.getValue();// 需要添加一個listif (obj instanceof List) {placeholderValue = delDynList(placeholder, (List) obj);} else {placeholderValue = placeholderValue.replaceAll(PoiWordUtils.getPlaceholderReg(mapKey), String.valueOf(obj));}}}currRun.setText(placeholderValue, 0);}/*** 處理的動態的段落(參數為list)** @param placeholder 段落占位符* @param obj* @return*/private String delDynList(String placeholder, List obj) {String placeholderValue = placeholder;List dataList = obj;Collections.reverse(dataList);for (int i = 0, size = dataList.size(); i < size; i++) {Object text = dataList.get(i);// 占位符的那行, 不用重新創建新的行if (i == 0) {placeholderValue = String.valueOf(text);} else {XWPFParagraph paragraph = createParagraph(String.valueOf(text));if (paragraph != null) {oldParagraph = paragraph;}// 增加段落后doc文檔的element的size會隨著增加(在當前行的上面添加// 這里減操作是回退并解析新增的行(因為可能新增的帶有占位符,這里為了支持圖片和表格)if (!isTable) {n--;}}}return placeholderValue;}/*** 創建段落 <p></p>** @param texts*/public XWPFParagraph createParagraph(String... texts) {// 使用游標創建一個新行XmlCursor cursor = oldParagraph.getCTP().newCursor();XWPFParagraph newPar = templateDoc.insertNewParagraph(cursor);// 設置段落樣式newPar.getCTP().setPPr(oldParagraph.getCTP().getPPr());copyParagraph(oldParagraph, newPar, texts);return newPar;}/*** 處理表格(遍歷)** @param table 表格* @param paramMap 需要替換的信息集合*/public void delTable(XWPFTable table, Map<String, Object> paramMap) throws Exception {List<XWPFTableRow> rows = table.getRows();for (int i = 0, size = rows.size(); i < size; i++) {XWPFTableRow row = rows.get(i);currentRowIndex = i;// 如果是動態添加行 直接處理后返回if (delAndJudgeRow(table, paramMap, row)) {return;}}}/*** 判斷并且是否是動態行,并且處理表格占位符** @param table 表格對象* @param paramMap 參數map* @param row 當前行* @return* @throws Exception*/private boolean delAndJudgeRow(XWPFTable table, Map<String, Object> paramMap, XWPFTableRow row) throws Exception {// 當前行是動態行標志if (PoiWordUtils.isAddRow(row)) {List<XWPFTableRow> xwpfTableRows = addAndGetRows(table, row, paramMap);// 回溯添加的行,這里是試圖處理動態添加的圖片for (XWPFTableRow tbRow : xwpfTableRows) {delAndJudgeRow(table, paramMap, tbRow);}return true;}// 如果是重復添加的行if (PoiWordUtils.isAddRowRepeat(row)) {List<XWPFTableRow> xwpfTableRows = addAndGetRepeatRows(table, row, paramMap);// 回溯添加的行,這里是試圖處理動態添加的圖片for (XWPFTableRow tbRow : xwpfTableRows) {delAndJudgeRow(table, paramMap, tbRow);}return true;}// 當前行非動態行標簽List<XWPFTableCell> cells = row.getTableCells();for (XWPFTableCell cell : cells) {//判斷單元格是否需要替換if (PoiWordUtils.checkText(cell.getText())) {List<XWPFParagraph> paragraphs = cell.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {List<XWPFRun> runs = paragraph.getRuns();StringBuilder sb = new StringBuilder();for (XWPFRun run : runs) {sb.append(run.toString());run.setText("", 0);}placeholder(paragraph, runs, sb);}}}return false;}/*** 處理占位符** @param runs 當前段的runs* @param sb 當前段的內容* @throws Exception*/private void placeholder(XWPFParagraph currentPar, List<XWPFRun> runs, StringBuilder sb) throws Exception {if (runs.size() > 0) {String text = sb.toString();XWPFRun currRun = runs.get(0);if (PoiWordUtils.isPicture(text)) {// 該段落是圖片占位符ImageEntity imageEntity = (ImageEntity) PoiWordUtils.getValueByPlaceholder(paramMap, text);int indentationFirstLine = currentPar.getIndentationFirstLine();// 清除段落的格式,否則圖片的縮進有問題currentPar.getCTP().setPPr(null);//設置縮進currentPar.setIndentationFirstLine(indentationFirstLine); // addPicture(currRun, imageEntity);} else {changeValue(currRun, text, paramMap);}}}/*** 添加圖片* @param currRun 當前run* @param imageEntity 圖片對象* @throws InvalidFormatException* @throws FileNotFoundException*//*private void addPicture(XWPFRun currRun, ImageEntity imageEntity) throws InvalidFormatException, FileNotFoundException {Integer typeId = imageEntity.getTypeId().getTypeId();String picId = currRun.getDocument().addPictureData(new FileInputStream(imageEntity.getUrl()), typeId);ImageUtils.createPicture(currRun, picId, templateDoc.getNextPicNameNumber(typeId),imageEntity.getWidth(), imageEntity.getHeight());}*//*** 添加行 標簽行不是新創建的** @param table* @param flagRow flagRow 表有標簽的行* @param paramMap 參數*/private List<XWPFTableRow> addAndGetRows(XWPFTable table, XWPFTableRow flagRow, Map<String, Object> paramMap) throws Exception {List<XWPFTableCell> flagRowCells = flagRow.getTableCells();XWPFTableCell flagCell = flagRowCells.get(0);String text = flagCell.getText();List<List<String>> dataList = (List<List<String>>) PoiWordUtils.getValueByPlaceholder(paramMap, text);if (dataList == null || dataList.size() <= 0) {return new ArrayList();}// 新添加的行List<XWPFTableRow> newRows = new ArrayList(dataList.size());XWPFTableRow currentRow = flagRow;int cellSize = flagRow.getTableCells().size();for (int i = 0, size = dataList.size(); i < size; i++) {if (i != 0) {currentRow = table.createRow();// 復制樣式if (flagRow.getCtRow() != null) {currentRow.getCtRow().setTrPr(flagRow.getCtRow().getTrPr());}}addRow(flagCell, currentRow, cellSize, dataList.get(i));newRows.add(currentRow);}return newRows;}/*** 添加重復多行 動態行 每一行都是新創建的** @param table* @param flagRow* @param paramMap* @return* @throws Exception*/private List<XWPFTableRow> addAndGetRepeatRows(XWPFTable table, XWPFTableRow flagRow, Map<String, Object> paramMap) throws Exception {List<XWPFTableCell> flagRowCells = flagRow.getTableCells();XWPFTableCell flagCell = flagRowCells.get(0);String text = flagCell.getText();List<List<String>> dataList = (List<List<String>>) PoiWordUtils.getValueByPlaceholder(paramMap, text);String tbRepeatMatrix = PoiWordUtils.getTbRepeatMatrix(text);//Assert.assertNotNull("模板矩陣不能為空", tbRepeatMatrix);// 新添加的行if (dataList == null || dataList.size() <= 0) {return new ArrayList();}List<XWPFTableRow> newRows = new ArrayList(dataList.size());String[] split = tbRepeatMatrix.split(PoiWordUtils.tbRepeatMatrixSeparator);int startRow = Integer.parseInt(split[0]);int endRow = Integer.parseInt(split[1]);int startCell = Integer.parseInt(split[2]);int endCell = Integer.parseInt(split[3]);XWPFTableRow currentRow;for (int i = 0, size = dataList.size(); i < size; i++) {int flagRowIndex = i % (endRow - startRow + 1);XWPFTableRow repeatFlagRow = table.getRow(flagRowIndex);// 清除占位符那行if (i == 0) {table.removeRow(currentRowIndex);}currentRow = table.createRow();// 復制樣式if (repeatFlagRow.getCtRow() != null) {currentRow.getCtRow().setTrPr(repeatFlagRow.getCtRow().getTrPr());}addRowRepeat(startCell, endCell, currentRow, repeatFlagRow, dataList.get(i));newRows.add(currentRow);}return newRows;}/*** 根據模板cell添加新行** @param flagCell 模板列(標記占位符的那個cell)* @param row 新增的行* @param cellSize 每行的列數量(用來補列補足的情況)* @param rowDataList 每行的數據*/private void addRow(XWPFTableCell flagCell, XWPFTableRow row, int cellSize, List<String> rowDataList) {for (int i = 0; i < cellSize; i++) {XWPFTableCell cell = row.getCell(i);cell = cell == null ? row.createCell() : row.getCell(i);if (i < rowDataList.size()) {PoiWordUtils.copyCellAndSetValue(flagCell, cell, rowDataList.get(i));} else {// 數據不滿整行時,添加空列PoiWordUtils.copyCellAndSetValue(flagCell, cell, "");}}}/*** 根據模板cell 添加重復行** @param startCell 模板列的開始位置* @param endCell 模板列的結束位置* @param currentRow 創建的新行* @param repeatFlagRow 模板列所在的行* @param rowDataList 每行的數據*/private void addRowRepeat(int startCell, int endCell, XWPFTableRow currentRow, XWPFTableRow repeatFlagRow, List<String> rowDataList) {int cellSize = repeatFlagRow.getTableCells().size();for (int i = 0; i < cellSize; i++) {XWPFTableCell cell = currentRow.getCell(i);cell = cell == null ? currentRow.createCell() : currentRow.getCell(i);int flagCellIndex = i % (endCell - startCell + 1);XWPFTableCell repeatFlagCell = repeatFlagRow.getCell(flagCellIndex);if (i < rowDataList.size()) {PoiWordUtils.copyCellAndSetValue(repeatFlagCell, cell, rowDataList.get(i));} else {// 數據不滿整行時,添加空列PoiWordUtils.copyCellAndSetValue(repeatFlagCell, cell, "");}}}/*** 復制段落** @param sourcePar 原段落* @param targetPar* @param texts*/private void copyParagraph(XWPFParagraph sourcePar, XWPFParagraph targetPar, String... texts) {targetPar.setAlignment(sourcePar.getAlignment());targetPar.setVerticalAlignment(sourcePar.getVerticalAlignment());// 設置布局targetPar.setAlignment(sourcePar.getAlignment());targetPar.setVerticalAlignment(sourcePar.getVerticalAlignment());if (texts != null && texts.length > 0) {String[] arr = texts;XWPFRun xwpfRun = sourcePar.getRuns().isEmpty() ? sourcePar.getRuns().get(0) : null;for (int i = 0, len = texts.length; i < len; i++) {String text = arr[i];XWPFRun run = targetPar.createRun();if (xwpfRun != null) {String fontFamily = xwpfRun.getFontFamily();run.setText(text);run.setFontFamily(fontFamily);int fontSize = xwpfRun.getFontSize();run.setFontSize((fontSize == -1) ? DEFAULT_FONT_SIZE : fontSize);run.setBold(xwpfRun.isBold());run.setItalic(xwpfRun.isItalic());} else {run.setFontFamily("仿宋_GB2312");int fontSize = -1;run.setFontSize((fontSize == -1) ? DEFAULT_FONT_SIZE : fontSize);run.setBold(false);run.setItalic(false);}}}}public void setParamMap(Map<String, Object> paramMap) {this.paramMap = paramMap;} }FreeMarkerUtil類
package com.midea.dmr.cont.ctr;import com.itextpdf.text.Document; import com.itextpdf.text.Font; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerFontProvider; import com.midea.dmr.common.exception.ParamValidationException; import freemarker.cache.StringTemplateLoader; import freemarker.template.Configuration; import freemarker.template.Template;import java.io.*; import java.nio.charset.Charset; import java.util.Map; import java.util.logging.Logger;/*** freemarker工具類*/ public class FreeMarkerUtil {private static final Logger LOG = Logger.getLogger(FreeMarkerUtil.class.getName());private static final String ENCODE = "UTF-8";/*** freeMarker渲染** @param data* @param templateFile* @return* @throws*/public static String freeMarkerRender(Map<String, Object> data, byte[] templateFile) throws ParamValidationException {Writer writer = new StringWriter();try {Configuration configuration = initConfig();Template template = getTemplate(configuration, templateFile);template.process(data, writer);writer.flush();return writer.toString();} catch (Exception e) {LOG.info("freeMarker渲染異常:" + e.getMessage());throw new ParamValidationException("freeMarker渲染異常");} finally {try {writer.close();} catch (IOException ioe) {LOG.info("關閉文件流異常:" + ioe.getMessage());}}}/*** 模板轉換為pdf(使用默認頁眉頁腳)** @param templateFile* @return* @throws*/public static byte[] html2Pdf(String templateFile) throws Exception {ByteArrayOutputStream baos = new ByteArrayOutputStream();Document document = null;try {document = new Document(PageSize.A4, 90, 90, 60, 60);PdfWriter writer = PdfWriter.getInstance(document, baos);PDFBuilder builder = new PDFBuilder();writer.setPageEvent(builder);document.open();XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider();LOG.info("html2Pdf.parseXHtml,開始");com.itextpdf.tool.xml.XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(templateFile.getBytes(ENCODE)), null, Charset.forName(ENCODE), fontImp);LOG.info("html2Pdf.parseXHtml,結束");document.close();baos.flush();baos.close();} catch (Exception e) {LOG.info("轉換電子合同異常:" + e.getMessage());throw new Exception(e);}return baos.toByteArray();}/*** 模板轉換為pdf(使用默認頁眉頁腳)** @param templateFile* @return* @throws*/public static byte[] html2PdfNew(String templateFile) throws Exception {ByteArrayOutputStream baos = new ByteArrayOutputStream();Document document = null;try {document = new Document(PageSize.A4, 90, 90, 60, 60);BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);Font font = new Font(baseFont);Paragraph p = new Paragraph();p.setFont(font);PdfWriter writer = PdfWriter.getInstance(document, baos);PDFBuilder builder = new PDFBuilder();writer.setPageEvent(builder);document.open();document.add(p);XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider();LOG.info("html2Pdf1.parseXHtml,開始");com.itextpdf.tool.xml.XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(templateFile.getBytes(ENCODE)), null, Charset.forName(ENCODE), fontImp);LOG.info("html2Pdf1.parseXHtml,結束");document.close();baos.flush();baos.close();} catch (Exception e) {LOG.info("轉換電子合同異常1:" + e.getMessage());throw new Exception(e);}return baos.toByteArray();}/*** 模板轉換為pdf(使用自定義頁眉頁腳)** @param templateFile* @param builder* @return* @throws*/public static byte[] html2Pdf(String templateFile, PDFBuilder builder) throws ParamValidationException {ByteArrayOutputStream baos = new ByteArrayOutputStream();try {Document document = new Document(PageSize.A4, 90, 90, 60, 60);PdfWriter writer = PdfWriter.getInstance(document, baos);writer.setPageEvent(builder);document.open();XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider();com.itextpdf.tool.xml.XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(templateFile.getBytes()), null, Charset.forName(ENCODE), fontImp);document.close();baos.flush();baos.close();} catch (Exception e) {LOG.info("轉換電子合同異常:" + e.getMessage());throw new ParamValidationException("轉換電子合同異常");}return baos.toByteArray();}/*** 模板參數初始化** @return*/public static Configuration initConfig() {Configuration configuration = new Configuration();configuration.setDefaultEncoding(ENCODE);return configuration;}/*** 獲取模板** @param configuration* @return* @throws*/public static Template getTemplate(Configuration configuration, byte[] templateFile) throws ParamValidationException {StringTemplateLoader stl = new StringTemplateLoader();stl.putTemplate("template", new String(templateFile));configuration.setTemplateLoader(stl);try {return configuration.getTemplate("template");} catch (Exception e) {LOG.info("獲取模板異常:" + e.getMessage());throw new ParamValidationException("獲取模板異常");}} }ImageEntity類
package com.midea.dmr.cont.ctr;/*** Create by IntelliJ Idea 2018.2* <p>* 圖片實體對象** @author: qyp* Date: 2019-10-26 21:52*/ public class ImageEntity {/*** 圖片寬度*/private int width = 400;/*** 圖片高度*/private int height = 300;/*** 圖片地址*/private String url;public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;} }ImageUtils類
package com.midea.dmr.cont.ctr;import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlToken; import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps; import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D; import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;/*** Create by IntelliJ Idea 2018.2** @author: qyp* Date: 2019-10-26 21:55*/ public class ImageUtils {/*** 圖片類型枚舉*/enum ImageType {/*** 支持四種類型 JPG/JPEG, GIT, BMP, PNG*/JPG("JPG", XWPFDocument.PICTURE_TYPE_JPEG),JPEG("JPEG", XWPFDocument.PICTURE_TYPE_JPEG),PNG("PNG", XWPFDocument.PICTURE_TYPE_PNG);private String name;private Integer typeId;ImageType(String name, Integer type) {this.name = name;this.typeId = type;}public String getName() {return name;}public Integer getTypeId() {return typeId;}}public static void createPicture(XWPFRun run, String blipId, int id, int width, int height) {final int EMU = 9525;width *= EMU;height *= EMU;CTInline inline = run.getCTR().addNewDrawing().addNewInline();String picXml = "" +"<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +" <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +" <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +" <pic:nvPicPr>" +" <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" +" <pic:cNvPicPr/>" +" </pic:nvPicPr>" +" <pic:blipFill>" +" <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +" <a:stretch>" +" <a:fillRect/>" +" </a:stretch>" +" </pic:blipFill>" +" <pic:spPr>" +" <a:xfrm>" +" <a:off x=\"0\" y=\"0\"/>" +" <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" +" </a:xfrm>" +" <a:prstGeom prst=\"rect\">" +" <a:avLst/>" +" </a:prstGeom>" +" </pic:spPr>" +" </pic:pic>" +" </a:graphicData>" +"</a:graphic>";XmlToken xmlToken = null;try {xmlToken = XmlToken.Factory.parse(picXml);} catch(XmlException xe) {xe.printStackTrace();}inline.set(xmlToken);inline.setDistT(0);inline.setDistB(0);inline.setDistL(0);inline.setDistR(0);CTPositiveSize2D extent = inline.addNewExtent();extent.setCx(width);extent.setCy(height);CTNonVisualDrawingProps docPr = inline.addNewDocPr();docPr.setId(id);docPr.setName("Picture " + id);docPr.setDescr("Generated");}}MapperUtils類
package com.midea.dmr.cont.ctr;import ma.glasnost.orika.MapperFacade; import ma.glasnost.orika.MapperFactory; import ma.glasnost.orika.impl.DefaultMapperFactory;public class MapperUtils {private static MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();private static MapperFacade mapper = null;static {mapper = mapperFactory.getMapperFacade();}public static MapperFacade getMapper() {return mapper;}public static void main(String[] args) {}}PDFBuilder類
package com.midea.dmr.cont.ctr;import com.itextpdf.text.*; import com.itextpdf.text.pdf.*;/*** 設置頁眉頁腳處理類*/ public class PDFBuilder extends PdfPageEventHelper {// 頁眉private String header = " 2020年V1.0版";// 頁腳private String footer = "";// 字體大小private int presentFontSize = 9;// 紙張大小private Rectangle pageSize = PageSize.A4;// 模板private PdfTemplate pdfTemplate;// 基礎字體對象private BaseFont baseFont = null;// 利用基礎字體生成的字體對象,一般用于生成中文文字private Font headerFont = null;private Font footerFont = null;public PDFBuilder() {}public PDFBuilder(String header, int presentFontSize, Rectangle pageSize) {this.header = header;this.presentFontSize = presentFontSize;this.pageSize = pageSize;}public PDFBuilder(String header, String footer, int presentFontSize, Rectangle pageSize) {this.header = header;this.footer = footer;this.presentFontSize = presentFontSize;this.pageSize = pageSize;}/*** 文檔打開時初始化** @param writer* @param document*/@Overridepublic void onOpenDocument(PdfWriter writer, Document document) {// 設置邊框長寬pdfTemplate = writer.getDirectContent().createTemplate(50, 50);// 共 頁 的矩形的長寬高try {if (baseFont == null) {baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);}// 設置字體if (headerFont == null) {headerFont = new Font(baseFont, presentFontSize, Font.UNDERLINE);// 數據體字體}if (footerFont == null) {footerFont = new Font(baseFont, presentFontSize, Font.NORMAL);// 數據體字體}} catch (Exception e) { // LogUtil.error("初始化異常:", e); // throw new ServiceException("初始化異常");}}/*** 每頁關閉寫入頁眉頁腳** @param writer* @param document*/@Overridepublic void onEndPage(PdfWriter writer, Document document) {try {if (baseFont == null) { // baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);baseFont = BaseFont.createFont();}if (footerFont == null) {// 數據體字體footerFont = new Font(baseFont, presentFontSize, Font.NORMAL);}float right = 505.0f;float top = 782.0f;float rightMargin = 90.0f;float leftMargin = 90.0f;float left = 90.0f;float bottom = 60.0f;if (document != null) {right = document.right();top = document.top();rightMargin = document.rightMargin();leftMargin = document.leftMargin();left = document.left();bottom = document.bottom();}// 1.寫入頁眉ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, new Phrase(header, headerFont), right, top + 20, 0);// 2.寫入前半部分的 第 X頁/共String foot1 = "";int align; // if (footer.isEmpty()) {if (org.apache.commons.lang.StringUtils.isBlank(this.footer)) {int pageS = writer.getPageNumber();foot1 = " " + pageS + " /";align = Element.ALIGN_CENTER;} else {foot1 = this.footer;align = Element.ALIGN_LEFT;}Phrase phrase = new Phrase(foot1, footerFont);TabSettings tabSettings = new TabSettings();// 3.計算前半部分的foot1的長度,后面好定位最后一部分的'Y頁'這倆字的x軸坐標,字體長度也要計算進去 = lenfloat len = baseFont.getWidthPoint(foot1, presentFontSize);// 4.拿到當前的PdfContentBytePdfContentByte cb = writer.getDirectContent();// 5.寫入頁腳1,x軸就是(右margin+左margin + right() -left()- len)/2.0FColumnText.showTextAligned(cb, align, phrase, (rightMargin + right + leftMargin - left - len) / 2.0F + 20F, bottom - 20, 0);// 6.寫入頁腳2的模板(就是頁腳的Y頁這倆字)添加到文檔中,計算模板的和Y軸,X=(右邊界-左邊界 - 前半部分的len值)/2.0F +// len , y 軸和之前的保持一致,底邊界-20cb.addTemplate(pdfTemplate, (rightMargin + right + leftMargin - left) / 2.0F + 20F, bottom - 20); // 調節模版顯示的位置} catch (Exception e) { // LogUtil.error("系統異常:", e);System.err.println(e.getMessage());return;}}/*** 文檔關閉時寫入頁眉頁腳** @param writer* @param document*/@Overridepublic void onCloseDocument(PdfWriter writer, Document document) {// 關閉文檔的時候,將模板替換成實際的 Y 值,至此,page x of y 制作完畢。pdfTemplate.beginText();pdfTemplate.setFontAndSize(baseFont, presentFontSize);// 生成的模版的字體、顏色String foot2 = ""; // if (footer.isEmpty()) {if (org.apache.commons.lang.StringUtils.isBlank(footer)) {foot2 = " " + (writer.getPageNumber()) + " ";}pdfTemplate.showText(foot2);// 模版顯示的內容pdfTemplate.endText();pdfTemplate.closePath();} }PDFUtil類
package com.midea.dmr.cont.ctr;import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.*; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.pdfbox.pdmodel.PDDocument;import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map;//import com.midea.jr.ecr.common.log.util.LogUtil;public class PDFUtil {/*** 獲取PDDocument對象** @param bytes* @return*/public static PDDocument getPDDocument(byte[] bytes) {PDDocument pdf = null;try {pdf = PDDocument.load(bytes);return pdf;} catch (Exception e) {//LogUtil.error("", e);}return null;}/*** 獲取PDDocument對象** @param file* @return*/public static PDDocument getPDDocument(File file) {PDDocument pdf = null;try {pdf = PDDocument.load(file);return pdf;} catch (Exception e) {//LogUtil.error("", e);}return null;}/*** 獲取PDDocument對象** @param fileInputStream* @return*/public static PDDocument getPDDocument(FileInputStream fileInputStream) {PDDocument pdf = null;try {pdf = PDDocument.load(fileInputStream);return pdf;} catch (Exception e) {//LogUtil.error("", e);}return null;}/*** 獲取PDDocument對象** @param pdfFilePath* @return*/public static PDDocument getPDDocument(String pdfFilePath) {try {File file = new File(pdfFilePath);if (file.exists()) {throw new Exception("路徑" + pdfFilePath + "文件不存在");}if (!file.isFile()) {throw new Exception("路徑" + pdfFilePath + "不是文件");}if (!file.canRead()) {throw new Exception("文件" + pdfFilePath + "無法讀取");}return getPDDocument(file);} catch (Exception e) {//LogUtil.error("", e);}return null;}/*** 自己設置壓縮質量來把圖片壓縮成byte[]** @param image* 壓縮源圖片* @param quality* 壓縮質量,在0-1之間,* @return 返回的字節數組*/public static byte[] bufferedImageTobytes(BufferedImage image, float quality) {// 如果圖片空,返回空if (image == null) {return null;}// 得到指定Format圖片的writerIterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");// 得到迭代器ImageWriter writer = iter.next(); // 得到writer// 得到指定writer的輸出參數設置(ImageWriteParam )ImageWriteParam iwp = writer.getDefaultWriteParam();iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // 設置可否壓縮iwp.setCompressionQuality(quality); // 設置壓縮質量參數iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);ColorModel colorModel = ColorModel.getRGBdefault();// 指定壓縮時使用的色彩模式iwp.setDestinationType(new javax.imageio.ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16)));// 開始打包圖片,寫入byte[]ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 取得內存輸出流IIOImage iIamge = new IIOImage(image, null, null);byte[] bytes = null;try {// 此處因為ImageWriter中用來接收write信息的output要求必須是ImageOutput// 通過ImageIo中的靜態方法,得到byteArrayOutputStream的ImageOutputwriter.setOutput(ImageIO.createImageOutputStream(byteArrayOutputStream));writer.write(null, iIamge, iwp);bytes = byteArrayOutputStream.toByteArray();} catch (IOException e) {//LogUtil.error("", e);}return bytes;}/*** 向pdf表單填充數據** @param fields* @param map* @throws IOException* @throws DocumentException*/public static void fillData(AcroFields fields, Map<String, Object> map) throws IOException, DocumentException {List<String> keys = new ArrayList<String>();Map<String, AcroFields.Item> formFields = fields.getFields();for (String key : map.keySet()) {if (formFields.containsKey(key)) {String value = String.valueOf(map.get(key));fields.setField(key,value/*,true*/); // 為字段賦值,注意字段名稱是區分大小寫的keys.add(key);}}Iterator<String> itemsKey = formFields.keySet().iterator();while (itemsKey.hasNext()) {String itemKey = itemsKey.next();if (!keys.contains(itemKey)) {fields.setField(itemKey, " ");}}}/*** itext生成pdf** @param map* @param file* @return*/public static byte[] generatePDF(Map<String, Object> map, byte[] file) {try{ByteArrayOutputStream bos = new ByteArrayOutputStream();PdfReader reader = new PdfReader(file);PdfStamper ps = new PdfStamper(reader, bos);/* 使用中文字體 */BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();fontList.add(bf);/* 取出報表模板中的所有字段 */AcroFields fields = ps.getAcroFields();fields.setSubstitutionFonts(fontList);fillData(fields, map);/* 必須要調用這個,否則文檔不會生成的 如果為false那么生成的PDF文件還能編輯,一定要設為true */ps.setFormFlattening(true);ps.close();return bos.toByteArray();} catch (Exception e) {//LogUtil.error("", e);}return null;}/*** 給pdf文件添加水印* @param InPdfFile* @param markImagePath* @return*/public static byte[] addPdfMark(byte[] InPdfFile, byte[] markImagePath) {try {ByteArrayOutputStream bos = new ByteArrayOutputStream();PdfReader reader = new PdfReader(InPdfFile, "PDF".getBytes());PdfStamper stamp = new PdfStamper(reader, bos);Image img = Image.getInstance(markImagePath);// 插入水印int pageSize = reader.getNumberOfPages();// 原pdf文件的總頁數for(int i = 1; i <= pageSize; i++) {float pageWidth = reader.getPageSize(i).getWidth();float pageHeight = reader.getPageSize(i).getHeight();PdfContentByte under = stamp.getUnderContent(i); //水印在文本之下 // PdfGState gs = new PdfGState(); // gs.setFillOpacity(0.8f);// 設置透明度 // under.setGState(gs);float per =(pageWidth/img.getWidth()) <(pageHeight/img.getHeight())?(pageWidth/img.getWidth()):(pageHeight/img.getHeight());img.scalePercent(per*100);//依照比例縮放img.scaleAbsolute(pageWidth,pageHeight);//自定義大小img.setAbsolutePosition((pageWidth-img.getWidth())/2, (pageHeight-img.getHeight())/2);//圖片坐標--居中under.addImage(img);}stamp.close();return bos.toByteArray();}catch(Exception e){//LogUtil.error("", e);}return null;}/*** 給pdf文件添加水印 -cfca用* @param InPdfFile* @param markImagePath* @return*/public static byte[] addPdfMarkToCfca(byte[] InPdfFile, byte[] markImagePath) {try {ByteArrayOutputStream bos = new ByteArrayOutputStream(); // try (ByteArrayOutputStream bos = new ByteArrayOutputStream();){PdfReader reader = new PdfReader(InPdfFile, "PDF".getBytes());PdfStamper stamp = new PdfStamper(reader, bos);Image img = Image.getInstance(markImagePath);// 插入水印int pageSize = reader.getNumberOfPages();// 原pdf文件的總頁數for(int i = 1; i <= pageSize; i++) {float pageWidth = reader.getPageSize(i).getWidth();float pageHeight = reader.getPageSize(i).getHeight();PdfContentByte under = stamp.getUnderContent(i); //水印在文本之下 // PdfGState gs = new PdfGState(); // gs.setFillOpacity(0.8f);// 設置透明度 // under.setGState(gs); // img.scalePercent(50);//依照比例縮放float per =(pageWidth/img.getWidth()) <(pageHeight/img.getHeight())?(pageWidth/img.getWidth()):(pageHeight/img.getHeight());img.scalePercent(per*100);//依照比例縮放//img.scaleAbsolute(pageWidth,pageHeight);//自定義大小img.setAbsolutePosition(0, 0);//圖片坐標--居中under.addImage(img);}stamp.close();return bos.toByteArray();}catch(Exception e){//LogUtil.error("", e);}return null;} }PoiWordUtils類
package com.midea.dmr.cont.ctr;import org.apache.poi.xwpf.usermodel.*;import java.util.List; import java.util.Map;/*** Create by IntelliJ Idea 2018.2** @author: qyp* Date: 2019-10-26 2:12*/ public class PoiWordUtils {/*** 占位符第一個字符*/public static final String PREFIX_FIRST = "$";/*** 占位符第二個字符*/public static final String PREFIX_SECOND = "{";/*** 占位符的前綴*/public static final String PLACEHOLDER_PREFIX = PREFIX_FIRST + PREFIX_SECOND;/*** 占位符后綴*/public static final String PLACEHOLDER_END = "}";/*** 表格中需要動態添加行的獨特標記*/public static final String addRowText = "tbAddRow:";public static final String addRowRepeatText = "tbAddRowRepeat:";/*** 表格中占位符的開頭 ${tbAddRow: 例如${tbAddRow:tb1}*/public static final String addRowFlag = PLACEHOLDER_PREFIX + addRowText;/*** 表格中占位符的開頭 ${tbAddRowRepeat: 例如 ${tbAddRowRepeat:0,2,0,1} 第0行到第2行,第0列到第1列 為模板樣式*/public static final String addRowRepeatFlag = PLACEHOLDER_PREFIX + addRowRepeatText;/*** 重復矩陣的分隔符 比如:${tbAddRowRepeat:0,2,0,1} 分隔符為 ,*/public static final String tbRepeatMatrixSeparator = ",";/*** 占位符的后綴*/public static final String PLACEHOLDER_SUFFIX = "}";/*** 圖片占位符的前綴*/public static final String PICTURE_PREFIX = PLACEHOLDER_PREFIX + "image:";/*** 判斷當前行是不是標志表格中需要添加行** @param row* @return*/public static boolean isAddRow(XWPFTableRow row) {return isDynRow(row, addRowFlag);}/*** 添加重復模板動態行(以多行為模板)* @param row* @return*/public static boolean isAddRowRepeat(XWPFTableRow row) {return isDynRow(row, addRowRepeatFlag);}private static boolean isDynRow(XWPFTableRow row, String dynFlag) {if (row == null) {return false;}List<XWPFTableCell> tableCells = row.getTableCells();if (tableCells != null) {XWPFTableCell cell = tableCells.get(0);if (cell != null) {String text = cell.getText();if (text != null && text.startsWith(dynFlag)) {return true;}}}return false;}/*** 從參數map中獲取占位符對應的值** @param paramMap* @param key* @return*/public static Object getValueByPlaceholder(Map<String, Object> paramMap, String key) {if (paramMap != null) {if (key != null) {return paramMap.get(getKeyFromPlaceholder(key));}}return null;}/*** 后去占位符的重復行列矩陣* @param key 占位符* @return {0,2,0,1}*/public static String getTbRepeatMatrix(String key) {//Assert.assertNotNull("占位符為空", key);String $1 = key.replaceAll("\\" + PREFIX_FIRST + "\\" + PREFIX_SECOND + addRowRepeatText + "(.*)" + "\\" + PLACEHOLDER_SUFFIX, "$1");return $1;}/*** 從占位符中獲取key** @return*/public static String getKeyFromPlaceholder(String placeholder) { // return Optional.ofNullable(placeholder).map(p -> p.replaceAll("[\\$\\{\\}]", "")).get();return placeholder.replaceAll("[\\$\\{\\}]", "");}public static void main(String[] args) {String s = "${aa}";s = s.replaceAll(PLACEHOLDER_PREFIX + PLACEHOLDER_SUFFIX , "");System.out.println(s); // String keyFromPlaceholder = getKeyFromPlaceholder("${tbAddRow:tb1}"); // System.out.println(keyFromPlaceholder);}/*** 復制列的樣式,并且設置值* @param sourceCell* @param targetCell* @param text*/public static void copyCellAndSetValue(XWPFTableCell sourceCell, XWPFTableCell targetCell, String text) {//段落屬性List<XWPFParagraph> sourceCellParagraphs = sourceCell.getParagraphs();if (sourceCellParagraphs == null || sourceCellParagraphs.size() <= 0) {return;}XWPFParagraph sourcePar = sourceCellParagraphs.get(0);XWPFParagraph targetPar = targetCell.getParagraphs().get(0);// 設置段落的樣式targetPar.getCTP().setPPr(sourcePar.getCTP().getPPr());List<XWPFRun> sourceParRuns = sourcePar.getRuns();if (sourceParRuns != null && sourceParRuns.size() > 0) {// 如果當前cell中有runList<XWPFRun> runs = targetPar.getRuns(); // Optional.ofNullable(runs).ifPresent(rs -> rs.stream().forEach(r -> r.setText("", 0)));if (runs != null && runs.size() > 0) {runs.get(0).setText(text, 0);} else {XWPFRun cellR = targetPar.createRun();cellR.setText(text, 0);// 設置列的樣式位模板的樣式targetCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());}setTypeface(sourcePar, targetPar);} else {targetCell.setText(text);}}/*** 復制字體*/private static void setTypeface(XWPFParagraph sourcePar, XWPFParagraph targetPar) {XWPFRun sourceRun = sourcePar.getRuns().get(0);String fontFamily = sourceRun.getFontFamily();int fontSize = sourceRun.getFontSize(); // String color = sourceRun.getColor(); // String fontName = sourceRun.getFontName();boolean bold = sourceRun.isBold();boolean italic = sourceRun.isItalic(); // int kerning = sourceRun.getKerning(); // String style = sourcePar.getStyle();UnderlinePatterns underline = sourceRun.getUnderline();List<XWPFRun> runs = targetPar.getRuns();if(runs.isEmpty()){return;}XWPFRun targetRun = targetPar.getRuns().get(0);targetRun.setFontFamily(fontFamily); // targetRun.setFontSize(fontSize == -1 ? 10 : fontSize);targetRun.setBold(bold); // targetRun.setColor(color);targetRun.setItalic(italic); // targetRun.setKerning(kerning);targetRun.setUnderline(underline);//targetRun.setFontSize(fontSize);}/*** 判斷文本中時候包含$* @param text 文本* @return 包含返回true,不包含返回false*/public static boolean checkText(String text){boolean check = false;if(text.indexOf(PLACEHOLDER_PREFIX)!= -1){check = true;}return check;}/*** 獲得占位符替換的正則表達式* @return*/public static String getPlaceholderReg(String text) {return "\\" + PREFIX_FIRST + "\\" + PREFIX_SECOND + text + "\\" + PLACEHOLDER_SUFFIX;}public static String getDocKey(String mapKey) {return PLACEHOLDER_PREFIX + mapKey + PLACEHOLDER_SUFFIX;}/*** 判斷當前占位符是不是一個圖片占位符* @param text* @return*/public static boolean isPicture(String text) {return text.startsWith(PICTURE_PREFIX);}/*** 刪除一行的列* @param row*/public static void removeCells(XWPFTableRow row) {int size = row.getTableCells().size();try {for (int i = 0; i < size; i++) { // row.removeCell(i);}} catch (Exception e) {}} }總結
- 上一篇: 阿里云无影云电脑ubantu中文失败解决
- 下一篇: 继电器——开关作用