//package com.casic.missiles.utils; // //import cn.hutool.core.codec.Base64; //import com.alibaba.fastjson.JSONObject; //import com.google.common.collect.Lists; //import org.apache.poi.xwpf.usermodel.*; //import org.apache.xmlbeans.XmlException; //import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject; //import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor; //import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing; // //import java.io.*; //import java.util.List; //import java.util.Map; //import java.util.Set; // //public class PictureUtil { // public void create(String filePath, JSONObject jsonObject) { // try { // FileInputStream template = new FileInputStream(new File(filePath)); // //获取docx解析对象 // XWPFDocument document = new XWPFDocument(template); // List<String[]> tableList = Lists.newArrayList(); // // //解析替换表格对象 // List<XWPFTable> tables = document.getTables(); // for (int i = 0; i < tables.size(); i++) { // //只处理行数大于等于2的表格,且不循环表头 // XWPFTable table = tables.get(i); // if (table.getRows().size() > 1) { // //判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入 // if (WordUtil.checkText(table.getText())) { // List<XWPFTableRow> rows = table.getRows(); // //遍历表格,并替换模板 // eachTable(rows, jsonObject); // } // } // } // // // workBook写入输出流 // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // document.write(baos); // baos.flush(); // } catch (Exception e) { // e.printStackTrace(); // } // } // /** // * 遍历表格 // * // * @param rows 表格行对象 // * @param textMap 需要替换的信息集合 // */ // public static void eachTable(List<XWPFTableRow> rows, JSONObject textMap) { // for (XWPFTableRow row : rows) { // List<XWPFTableCell> cells = row.getTableCells(); // for (XWPFTableCell cell : cells) { // //判断单元格是否需要替换 // if (checkText(cell.getText())) { // List<XWPFParagraph> paragraphs = cell.getParagraphs(); // for (XWPFParagraph paragraph : paragraphs) { // List<XWPFRun> runs = paragraph.getRuns(); // for (XWPFRun run : runs) { //// run.setText(changeValue(run.toString(), textMap), 0); // //新增制证照片单独处理 // if (run.toString().indexOf("${jpeg}") != -1 && textMap.containsKey("jpeg")){ // addPic(run,textMap, 1188000, 1728000,0, 0); // break; // } // String value = changeValue(run.toString(), textMap); // setWrap(value, run); // } // } // } // } // } // } // // /** // * 匹配传入信息集合与模板 // * // * @param value 模板需要替换的区域 // * @param textMap 传入信息集合 // * @return 模板需要替换区域信息集合对应值 // */ // public static String changeValue(String value, JSONObject textMap) { // // boolean flag = false; // Set<Map.Entry<String, Object>> textSets = textMap.entrySet(); // for (Map.Entry<String, Object> textSet : textSets) { // //匹配模板与替换值 格式${key} // String key = "${" + textSet.getKey() + "}"; // if (value.indexOf(key) != -1) { // value = textSet.getValue() == null ? "" : ("" + textSet.getValue()); // flag = true; // break; // } // } // //模板未匹配到区域替换为空 // if (checkText(value)) { // value = ""; // } // return value; // } // public static void setWrap(String value, XWPFRun run) { // if ( value.indexOf("\n") > 0) { // //设置换行 // String[] text = value.split("\n"); // for (int f = 0; f < text.length; f++) { // if (f == 0) { // run.setText(text[f].trim(),0); // } else { //// run.addCarriageReturn();//硬回车 // // 换行 // run.addBreak(); // run.setText(text[f]); // } // } // } else { // run.setText((String) value,0); // } // } // // /** // * @param ctGraphicalObject 图片数据 // * @param deskFileName 图片描述 // * @param width 宽 // * @param height 高 // * @param leftOffset 水平偏移 left // * @param topOffset 垂直偏移 top // * @param behind 文字上方,文字下方 // * @return // * @throws Exception // */ // public static CTAnchor getAnchorWithGraphic(CTGraphicalObject ctGraphicalObject, String deskFileName, int width, int height, int leftOffset, int topOffset, boolean behind) { // String anchorXML ="" // +"<wp:anchor xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" simplePos=\"0\" relativeHeight=\"0\" behindDoc=\"" + ((behind) ? 1 : 0) + "\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">" // +" <wp:simplePos x=\"0\" y=\"0\"/>" // +" <wp:positionH relativeFrom=\"column\">" // +" <wp:posOffset>"+ leftOffset + "</wp:posOffset>" // +" </wp:positionH>" // +" <wp:positionV relativeFrom=\"line\">" // +" <wp:posOffset>"+ topOffset +"</wp:posOffset>" // +" </wp:positionV>" // +" <wp:extent cx=\"" + width + "\" cy=\"" + height + "\"/>" // +" <wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>" // +" <wp:wrapNone/>" // +" <wp:docPr id=\"1\" name=\"Drawing 0\" descr=\"" +deskFileName+ "\"/><wp:cNvGraphicFramePr/>" // +"</wp:anchor>"; // try { // CTDrawing drawing = CTDrawing.Factory.parse(anchorXML); // CTAnchor anchor = drawing.getAnchorArray(0); // anchor.setGraphic(ctGraphicalObject); // return anchor; // } catch (XmlException e) { // e.printStackTrace(); // return null; // } // } // // /** // * 新增制证照片单独处理 // * @param run // * @param textMap // */ // public static void addPic(XWPFRun run, JSONObject textMap, int width, int height, int leftOffset, int topOffset){ // String runText = run.toString().trim(); // byte[] zjzp = Base64.decode(textMap.getString("jpeg")); // try(ByteArrayInputStream byteInputStream = new ByteArrayInputStream(zjzp)) { // //1、添加图片 // run.addPicture(byteInputStream,XWPFDocument.PICTURE_TYPE_JPEG,"照片", width, height); // //2、获取图片 // CTDrawing cTDrawing = run.getCTR().getDrawingArray(0); // CTGraphicalObject cTGraphicalObject = cTDrawing.getInlineArray(0).getGraphic(); // //3、设置属性 // CTAnchor ctAnchor = getAnchorWithGraphic(cTGraphicalObject,"照片", width, height,leftOffset, topOffset,false); // cTDrawing.setAnchorArray(new CTAnchor[]{ctAnchor}); // cTDrawing.removeInline(0); // run.setText(runText.replace("${jpeg}",""), 0); // }catch (Exception e){ // e.printStackTrace(); // } // } //}