From a2cfd417f673603633c6d46713d320bb1501f88c Mon Sep 17 00:00:00 2001
From: 许鹏程 <1821349743@qq.com>
Date: 星期二, 08 八月 2023 16:03:30 +0800
Subject: [PATCH] xn commit
---
doc/print.md | 48 ++++
src/main/java/com/product/print/util/WordReplaceUtil.java | 6
doc/img_1.png | 0
src/main/java/com/product/print/util/DynamicTableRenderPolicy.java | 204 ++++++----------
doc/img_2.png | 0
src/main/java/com/product/print/util/PrintPoiUtil.java | 138 +++++++++++
src/main/java/com/product/print/util/TableEmptyHandler.java | 101 ++++++++
src/main/java/com/product/print/util/FlowOpinionRenderPolicy.java | 112 +++++++++
src/main/java/com/product/print/util/PrintTest.java | 41 +++
doc/img_3.png | 0
src/main/java/com/product/print/service/PrintRealizeService.java | 40 ++
11 files changed, 552 insertions(+), 138 deletions(-)
diff --git a/doc/img_1.png b/doc/img_1.png
new file mode 100644
index 0000000..b5d5be4
--- /dev/null
+++ b/doc/img_1.png
Binary files differ
diff --git a/doc/img_2.png b/doc/img_2.png
new file mode 100644
index 0000000..855f7f4
--- /dev/null
+++ b/doc/img_2.png
Binary files differ
diff --git a/doc/img_3.png b/doc/img_3.png
new file mode 100644
index 0000000..17ba69d
--- /dev/null
+++ b/doc/img_3.png
Binary files differ
diff --git a/doc/print.md b/doc/print.md
new file mode 100644
index 0000000..eaf3fe2
--- /dev/null
+++ b/doc/print.md
@@ -0,0 +1,48 @@
+# 鎵撳嵃妯℃澘閰嶇疆璇存槑
+
+## 鏍囩
+
+### {{var}}
+
+ 鐢ㄤ簬杈撳嚭鍙橀噺锛屽锛歿{name}}锛岃緭鍑哄彉閲弉ame鐨勫��
+
+#### 
+
+### {{@var}}
+
+ 鐢ㄤ簬杈撳嚭鎸囧畾娴佺▼鑺傜偣鎰忚
+ 渚嬪瓙锛�
+ 1.{{@瀹℃牳}}锛岃緭鍑烘祦绋嬩腑鑺傜偣鍚嶇О涓衡�滃鏍糕�濈殑鎰忚
+ 2.{{@xxxx-xxxx-xxxx-xxxx}},杈撳嚭娴佺▼涓妭鐐箄uid涓衡�渪xxx-xxxx-xxxx-xxxx鈥濈殑鎰忚
+### {{@var|var2}}
+
+ 鐢ㄤ簬鍚屼竴浣嶇疆杈撳嚭涓嶅悓娴佺▼鑺傜偣鎰忚
+ 鐢ㄦ硶涓巤{@var}}涓�鑷达紝鍙槸鏀寔澶氫釜鑺傜偣锛岃妭鐐逛箣闂寸敤鈥渱鈥濆垎闅旓紝浼樺厛绾т粠宸﹀埌鍙�
+#### 
+
+## <span id="table">鍔ㄦ�佽〃鏍硷紙瀛愯〃锛�</a>
+
+ {{琛ㄥ悕}}锛屾牴鎹�濊〃鍚嶁�滆幏鍙朏ieldSetEntity涓殑瀛愯〃鏁版嵁锛岀嫭鍗犱竴琛�,娓叉煋瀹屾垚鍚庝細鑷姩鍒犻櫎璇ヨ
+
+### 瀛愯〃鏍囩
+
+ {{$~index~}}锛� 鍥哄畾鍐欐硶锛岃緭鍑哄瓙琛ㄨ鍙�
+ {{$瀛楁鍚峿}锛氳緭鍑哄瓙琛ㄤ腑鎸囧畾瀛楁鐨勫��
+
+### 
+
+## 娴佺▼鎰忚
+灏嗘墍鏈夋祦绋嬫剰瑙佽緭鍑猴紝杈撳嚭椤哄簭涓哄姙鐞嗘椂闂村�掑簭
+
+鏍囩閰嶇疆涓嶽鍔ㄦ�佽〃鏍硷紙瀛愯〃锛塢(#table)鍔ㄦ�佽〃鏍硷紙瀛愯〃锛変竴鑷�
+
+ 鏍囩涓殑鍙橀噺涓哄浐瀹氬��:"lx_flow_opinion",瀹屾暣鏍囩涓猴細{{lx_flow_opinion}}
+### 琛ㄦ牸鍐呭鏍囩
+
+ {{$node_title}} 鑺傜偣鍚嶇О
+ {{$opinion}} 鑺傜偣鎰忚锛屽鏈夌鍚嶅垯浼氬皢绛惧悕鍥剧墖涓�骞惰緭鍑�
+ 鏇村鍙橀噺鍙傝绯荤粺娴佺▼璇︽儏琛紙product_sys_flow_detail锛夊崟涓殑鍙橀噺
+### 鍚屼竴浣嶇疆涓嶅悓鑺傜偣鎰忚
+####
+
+
diff --git a/src/main/java/com/product/print/service/PrintRealizeService.java b/src/main/java/com/product/print/service/PrintRealizeService.java
index 5426ec5..4687f24 100644
--- a/src/main/java/com/product/print/service/PrintRealizeService.java
+++ b/src/main/java/com/product/print/service/PrintRealizeService.java
@@ -10,6 +10,8 @@
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
+import com.deepoove.poi.render.RenderContext;
+import com.deepoove.poi.util.RegexUtils;
import com.product.common.lang.StringUtils;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.config.Global;
@@ -23,16 +25,21 @@
import com.product.print.config.CmnConst;
import com.product.print.service.ide.IPrintRealizeService;
import com.product.print.util.DynamicTableRenderPolicy;
+import com.product.print.util.FlowOpinionRenderPolicy;
+import com.product.print.util.TableEmptyHandler;
import com.product.tool.flow.service.FlowDetailService;
import com.product.util.BaseUtil;
import com.product.util.SystemParamReplace;
+import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -57,7 +64,7 @@
public static void dataConvertCheckedData(FieldSetEntity fse) {
TextRenderData selSymbol = new TextRenderData(CmnConst.PRINT_CHECKED_CHAR, new Style(CmnConst.PRINT_FONT, 14));
- TextRenderData unselSymbol = new TextRenderData(CmnConst.PRINT_UNCHECKED_CHAR, new Style(CmnConst.PRINT_FONT, 14));
+ TextRenderData unselSymbol = new TextRenderData(CmnConst.PRINT_UNCHECKED_CHAR, new Style(CmnConst.PRINT_FONT, 18));
//鑾峰彇琛ㄥ崟瀛楁
Object[] fields = fse.getMeta().getFields();
@@ -186,6 +193,9 @@
for (int i = 0; i < fields.length; i++) {
String field = fields[i].toString();
FieldSetEntity metaEntity = fse.getMeta().getFieldMeta(field);
+ if (metaEntity == null) {
+ continue;
+ }
String fieldType = metaEntity.getString("field_type");
//鍒ゆ柇鏄惁鎷ユ湁娴佺▼鏍囪瘑
if ("flowsign".equals(fieldType)) {
@@ -217,7 +227,7 @@
replaceWord(localTempPathWord, file.getPath(), fse);
file.delete();
- String replaceParams = SystemParamReplace.replaceParams(printConf.getString(CmnConst.PRINT_FILE_NAME), fse);
+ String replaceParams = SystemParamReplace.replaceParams(Optional.ofNullable(printConf.getString(CmnConst.PRINT_FILE_NAME)).orElse(printConf.getString(CmnConst.PRINT_NAME)), fse);
printConf.setValue(CmnConst.PRINT_FILE_NAME, replaceParams);
if (isConvertPdf) {
try {
@@ -261,16 +271,36 @@
//鑾峰彇瀛愯〃鏁版嵁
Map<String, DataTableEntity> subDataMap = dataFse.getSubData();
ConfigureBuilder config = Configure.createDefault().builder();
+ config.addPlugin('@', new FlowOpinionRenderPolicy(flowOpinion));
+ config.addPlugin('$', new com.deepoove.poi.policy.DynamicTableRenderPolicy() {
+ @Override
+ public void doRender(RenderContext<Object> context) throws Exception {
+ return;
+ }
+
+ @Override
+ public void render(XWPFTable table, Object data) throws Exception {
+ return;
+ }
+ });
+ config.buildGrammerRegex("(#)?([\\w\\u4e00-\\u9fa5]+)(\\.?[\\w\\u4e00-\\u9fa5\\|]*)*(#)?");
+ TableEmptyHandler tableEmptyHandler = new TableEmptyHandler();
if (!CollectionUtil.isEmpty(subDataMap)) {
for (Map.Entry<String, DataTableEntity> entry : subDataMap.entrySet()) {
cloneValues.put(entry.getKey(), entry.getValue().getData().stream().map(item -> (Map<String, Object>) ((Map) item.getValues())).collect(Collectors.toList()));
config.bind(entry.getKey(), new DynamicTableRenderPolicy(entry.getKey()));
+ tableEmptyHandler.addTag(entry.getKey());
}
}
- if(flowOpinion!=null && flowOpinion.size()>0){
- cloneValues.put("lx_flow_opinion", flowOpinion);
- config.bind("lx_flow_opinion", new DynamicTableRenderPolicy("lx_flow_opinion"));
+ if (flowOpinion != null) {
+ if (!CollectionUtil.isEmpty(flowOpinion)) {
+ cloneValues.put("lx_flow_opinion", flowOpinion);
+ config.bind("lx_flow_opinion", new DynamicTableRenderPolicy("lx_flow_opinion"));
+ }
+ tableEmptyHandler.addTag("lx_flow_opinion");
}
+ config.setValidErrorHandler(tableEmptyHandler);
+
try {
//妫�鏌ヨ緭鍑烘枃浠舵槸鍚﹀瓨鍦紝涓嶅瓨鍦ㄥ垯鍒涘缓
FileUtil.touch(outPath);
diff --git a/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java b/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java
index 731c237..daa0044 100644
--- a/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java
+++ b/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java
@@ -1,8 +1,10 @@
package com.product.print.util;
-import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.collection.CollectionUtil;
import com.deepoove.poi.exception.RenderException;
import com.deepoove.poi.render.RenderContext;
+import com.deepoove.poi.template.ElementTemplate;
+import com.deepoove.poi.template.MetaTemplate;
import com.deepoove.poi.template.run.RunTemplate;
import com.deepoove.poi.util.TableTools;
import com.product.common.lang.StringUtils;
@@ -11,18 +13,11 @@
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.Base64;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -37,12 +32,14 @@
private final String indexKey = "~index~";
- public DynamicTableRenderPolicy(String replaceKey) {
+ public DynamicTableRenderPolicy(String replaceKey) {
this.replaceKey = replaceKey;
}
- private XWPFRun run;
+ private List<MetaTemplate> metaTemplateList;
+
+
@Override
public void doRender(RenderContext<Object> context) throws Exception {
@@ -55,8 +52,13 @@
}
XWPFTableCell cell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
XWPFTable table = cell.getTableRow().getTable();
+ this.metaTemplateList = context.getTemplate().getElementTemplates();
+ ElementTemplate eleTemplate = context.getEleTemplate();
+ int index = metaTemplateList.indexOf(eleTemplate);
+ this.metaTemplateList = metaTemplateList.subList(index + 1, metaTemplateList.size());
render(table, context.getData());
} catch (Exception e) {
+ e.printStackTrace();
throw new RenderException("Dynamic render table error:" + e.getMessage(), e);
}
}
@@ -124,133 +126,79 @@
}
fieldNames[i] = replaceKey;
}
- //鑾峰彇fieldRow鎵�鍦ㄧ殑涓嬫爣
- int fieldRowIndex = xwpfTable.getRows().indexOf(fieldRow);
- for (int i = 0; i < subTableData.size(); i++) {
- Map<String, Object> map = subTableData.get(i);
- //鍒涘缓涓�琛屽湪fieldRowIndex涓嬮潰
- XWPFTableRow row = xwpfTable.insertNewTableRow(fieldRowIndex + 1 + i);
- copyTableRow(row, fieldRow);
- //璁剧疆row鐨勫睘鎬т笌fieldRow涓�鑷�
- row.setHeight(fieldRow.getHeight());
- //閬嶅巻瀛楁姣忎釜瀛楁鍒涘缓涓�涓崟鍏冩牸
- for (int j = 0; j < fieldNames.length; j++) {
- //褰撳墠鍗曞厓鏍�
- XWPFTableCell cell;
- //鍒ゆ柇row涓j涓崟鍏冩牸鏄惁瀛樺湪
- if (row.getTableCells().size() > j) {
- cell = row.getCell(j);
+ if (subTableData != null && subTableData.size() > 0) {
+ for (int i = 0; i < subTableData.size(); i++) {
+ Map<String, Object> map = subTableData.get(i);
+ XWPFTableRow row;
+ if (i == 0) {
+ row = fieldRow;
} else {
- cell = row.createCell();
+ //鍒涘缓涓�琛屽湪fieldRowIndex涓嬮潰
+ row = xwpfTable.insertNewTableRow(xwpfTable.getRows().size());
+ PrintPoiUtil.copyTableRow(row, fieldRow);
}
- //璁剧疆鍗曞厓鏍肩殑鍊间粠map涓彇鍑�
- //鍒ゆ柇鏄惁鏄簭鍙峰垪
- if (indexKey.equals(fieldNames[j])) {
- cell.setText(String.valueOf(i + 1));
- continue;
- }
- Object value = map.get(fieldNames[j]);
- if (value == null) {
- value = "";
- }
- String text = cell.getText();
- //鍒犻櫎鍗曞厓鏍间腑鐨勬棫鍐呭
- if (!StringUtils.isEmpty(text)) {
- List<XWPFParagraph> paragraphs = cell.getParagraphs();
- if (paragraphs.size() > 1) {
- cell.removeParagraph(1);
- }
- List<XWPFRun> runs = paragraphs.get(0).getRuns();
- //娓呯┖鏂囧瓧
- for (int k = 0; k < runs.size(); k++) {
- runs.get(k).setText("", 0);
- }
- }
- cell.setText(value.toString());
- if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldNames[j]) && !StringUtils.isEmpty(map.get("sign_attachment_uuid"))) {
- //鎰忚妗� 鎻掑叆绛惧悕鍥剧墖鍒板崟鍏冩牸鍙充笅瑙掍綅缃�
+ //閬嶅巻瀛楁姣忎釜瀛楁鍒涘缓涓�涓崟鍏冩牸
+ for (int j = 0; j < fieldNames.length; j++) {
+ //褰撳墠鍗曞厓鏍�
+ XWPFTableCell cell = row.getCell(j);
+ //娓呯┖鍗曞厓鏍煎唴瀹�
XWPFParagraph xwpfParagraph = cell.addParagraph();
- xwpfParagraph.setAlignment(ParagraphAlignment.RIGHT);
- XWPFRun run = xwpfParagraph.createRun();
- String signAttachmentBase64 = map.get("sign_attachment_uuid").toString();
- //灏哹ase64杞崲瀛楄妭娴�
- byte[] bytes = Base64.getDecoder().decode(signAttachmentBase64.split(",")[1]);
- //灏嗗瓧鑺傛祦杞崲涓鸿緭鍏ユ祦
- InputStream inputStream = new ByteArrayInputStream(bytes);
- try {
- //鎹㈣鎻掑叆鍥剧墖
- run.addPicture(inputStream, XWPFDocument.PICTURE_TYPE_PNG, "sign.png", Units.toEMU(50), Units.toEMU(20));
- inputStream.close();
- } catch (InvalidFormatException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
+ int index = cell.getParagraphs().indexOf(xwpfParagraph);
+ PrintPoiUtil.copyParagraph(xwpfParagraph, cell.getParagraphs().get(0));
+ //鍒犻櫎鎵�鏈夌殑run
+ for (int k = xwpfParagraph.getRuns().size() - 1; k >= 0; k--) {
+ xwpfParagraph.removeRun(k);
}
- }
+ XWPFRun xwpfRun = xwpfParagraph.createRun();
+ PrintPoiUtil.copyRun(xwpfRun, cell.getParagraphs().get(0).getRuns().get(0));
+ //鍒犻櫎cell涓殑娈佃惤浣嗘帓闄ゆ柊澧炵殑娈佃惤
+ for (int k = cell.getParagraphs().size() - 1; k >= 0; k--) {
+ if (k < index) {
+ cell.removeParagraph(k);
+ }
+ }
+ //鍒ゆ柇鏄惁鏄簭鍙峰垪
+ if (indexKey.equals(fieldNames[j])) {
+ cell.setText(String.valueOf(i + 1));
+ continue;
+ }
+ Object value = map.get(fieldNames[j]);
+ if (value == null) {
+ value = "";
+ }
+ xwpfRun.setText(value.toString(), 0);
+ if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldNames[j]) && !StringUtils.isEmpty(map.get("sign_attachment_uuid"))) {
+ //鎰忚妗� 鎻掑叆绛惧悕鍥剧墖鍒板崟鍏冩牸鍙充笅瑙掍綅缃�
+ xwpfParagraph = cell.addParagraph();
+ xwpfParagraph.setAlignment(ParagraphAlignment.RIGHT);
+ XWPFRun run = xwpfParagraph.createRun();
+ String signAttachmentBase64 = map.get("sign_attachment_uuid").toString();
+ //灏哹ase64杞崲瀛楄妭娴�
+ byte[] bytes = Base64.getDecoder().decode(signAttachmentBase64.split(",")[1]);
+ //灏嗗瓧鑺傛祦杞崲涓鸿緭鍏ユ祦
+ InputStream inputStream = new ByteArrayInputStream(bytes);
+ try {
+ //鎹㈣鎻掑叆鍥剧墖
+ run.addPicture(inputStream, XWPFDocument.PICTURE_TYPE_PNG, "sign.png", Units.toEMU(50), Units.toEMU(20));
+ inputStream.close();
+ } catch (InvalidFormatException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
}
+ } else {
+
+// 鍒犻櫎fieldRow
+ int index = xwpfTable.getRows().indexOf(fieldRow);
+ xwpfTable.removeRow(index);
}
//鍒犻櫎璧峰琛�
xwpfTable.removeRow(startRowIndex);
- //鍒犻櫎fieldRow
- xwpfTable.removeRow(fieldRowIndex - 1);
-
-// //璇诲彇瀹屾瘯鍚庡垹闄ゆ渶鍚庝竴琛�
-// xwpfTable.removeRow(rows.size() - 1);
- //鑾峰彇鎵�鏈夌殑琛屽垽鏂崟鍏冩牸鏄惁鏈夊�兼病鏈夊氨鍒犻櫎璇ヨ
-// List<XWPFTableRow> tableRows = xwpfTable.getRows();
-// List<XWPFTableRow> deleteRows = new ArrayList<>();
-// for (int i = 0; i < tableRows.size(); i++) {
-// XWPFTableRow row = tableRows.get(i);
-// boolean flag = false;
-// for (int j = 0; j < row.getTableCells().size(); j++) {
-// XWPFTableCell cell = row.getTableCells().get(j);
-// if (StringUtils.isNotEmpty(cell.getText())) {
-// flag = true;
-// break;
-// }
-// }
-// if (!flag) {
-// deleteRows.add(row);
-// }
-// }
-// //閬嶅巻瑕佸垹闄ょ殑琛岃幏鍙栨墍鍦ㄧ殑涓嬫爣杩涜鍒犻櫎
-// for (int i = 0; i < deleteRows.size(); i++) {
-// XWPFTableRow row = deleteRows.get(i);
-// int index = xwpfTable.getRows().indexOf(row);
-// xwpfTable.removeRow(index);
-// }
-// //閬嶅巻鏁版嵁闆嗗悎锛屾瘡涓猰ap瀵瑰簲涓�琛屾暟鎹�
-// for (int i = 0; i < subTableData.size(); i++) {
-// Map<String, Object> map = subTableData.get(i);
-// //鍒涘缓涓�琛�
-// XWPFTableRow row = xwpfTable.createRow();
-// //閬嶅巻瀛楁姣忎釜瀛楁鍒涘缓涓�涓崟鍏冩牸
-// for (int j = 0; j < fieldNames.length; j++) {
-// //褰撳墠鍗曞厓鏍�
-// XWPFTableCell cell;
-// //鍒ゆ柇row涓j涓崟鍏冩牸鏄惁瀛樺湪
-// if (row.getTableCells().size() > j) {
-// cell = row.getTableCells().get(j);
-// } else {
-// cell = row.createCell();
-// }
-// //璁剧疆鍗曞厓鏍肩殑鍊间粠map涓彇鍑�
-// //鍒ゆ柇鏄惁鏄簭鍙峰垪
-// if (indexKey.equals(fieldNames[j])) {
-// cell.setText(String.valueOf(i + 1));
-// continue;
-// }
-// Object value = map.get(fieldNames[j]);
-// if (value == null) {
-// value = "";
-// }
-// //璁剧疆鍗曞厓鏍肩殑鍊�
-// cell.setText(value.toString());
-// }
-// }
-
}
/**
diff --git a/src/main/java/com/product/print/util/FlowOpinionRenderPolicy.java b/src/main/java/com/product/print/util/FlowOpinionRenderPolicy.java
new file mode 100644
index 0000000..7dcc8b5
--- /dev/null
+++ b/src/main/java/com/product/print/util/FlowOpinionRenderPolicy.java
@@ -0,0 +1,112 @@
+package com.product.print.util;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.deepoove.poi.data.TextRenderData;
+import com.deepoove.poi.policy.AbstractRenderPolicy;
+import com.deepoove.poi.render.RenderContext;
+import com.deepoove.poi.xwpf.BodyContainer;
+import com.deepoove.poi.xwpf.BodyContainerFactory;
+import com.product.common.lang.StringUtils;
+import org.apache.poi.util.Units;
+import org.apache.poi.xwpf.usermodel.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.Base64;
+import java.util.Optional;
+
+/**
+ * 鑷畾涔夎В鏋恵{@var}}鏍囩
+ *
+ * @Author cheng
+ * @Date 2023/6/27 15:50
+ * @Desc
+ */
+public class FlowOpinionRenderPolicy extends AbstractRenderPolicy<TextRenderData> {
+ /**
+ * 鎰忚鍒楄〃
+ */
+ private JSONArray opinionArray;
+
+ public FlowOpinionRenderPolicy(JSONArray opinionArray) {
+ this.opinionArray = opinionArray;
+ }
+
+
+ @Override
+ public void doRender(RenderContext<TextRenderData> context) throws Exception {
+ XWPFRun run = context.getRun();
+ //鏍囩鍚嶇О
+ String tagName = context.getEleTemplate().getTagName();
+ JSONObject opinion=null;
+ //鏀寔澶氫釜鏍囩鍚嶇О锛岀敤|鍒嗛殧 渚嬪锛歿{@var1|var2|var3...}} 鎸夌収椤哄簭鏌ユ壘鎰忚鍒楄〃
+ for (String name : tagName.split("\\|")) {
+ opinion = getOpinion(name);
+ if(opinion!=null){
+ break;
+ }
+ }
+ if (opinion == null) {
+ //濡傛灉鎰忚鍒楄〃涓病鏈夋壘鍒帮紝鍒欑洿鎺ユ浛鎹负绌�
+ run.setText("", 0);
+ return;
+ }
+ XWPFParagraph parent = (XWPFParagraph) run.getParent();
+ XWPFRun newRun = run;
+ //鍒ゆ柇鏄惁鏈夌鍚�
+ if (!StringUtils.isEmpty(opinion.getString("sign_attachment_uuid"))) {
+ XWPFParagraph paragraph = createParagraph(run);
+ //璁剧疆绛惧悕鐨勬钀藉眳鍙�
+ paragraph.setAlignment(ParagraphAlignment.RIGHT);
+ //鎻掑叆涓�涓柊鐨剅un
+ newRun = paragraph.createRun();
+ String signAttachmentBase64 = opinion.getString("sign_attachment_uuid");
+ String[] split = signAttachmentBase64.split(",");
+ //灏哹ase64杞崲瀛楄妭娴�
+ byte[] bytes = Base64.getDecoder().decode(split[split.length - 1]);
+ //灏嗗瓧鑺傛祦杞负杈撳叆娴�
+ InputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
+ //鎻掑叆鍥剧墖
+ newRun.addPicture(byteArrayInputStream, XWPFDocument.PICTURE_TYPE_PNG, "sign.png", Units.toEMU(50), Units.toEMU(20));
+ byteArrayInputStream.close();
+ newRun.setText("", 0);
+ }
+ XWPFParagraph paragraph = createParagraph(newRun);
+ //娈佃惤鏍峰紡涓庡師濮嬫钀戒竴鑷�
+ paragraph.getCTP().setPPr(parent.getCTP().getPPr());
+ //璁剧疆鍒涘缓鏂扮殑run
+ XWPFRun textRun = paragraph.createRun();
+ //澶嶅埗鍘熷run
+ PrintPoiUtil.copyRun(textRun, run);
+ //璁剧疆run鐨勫唴瀹�
+ textRun.setText(Optional.ofNullable(opinion.getString("opinion")).orElse("鏈~鍐欏姙鐞嗘剰瑙�"), 0);
+ //鍒犻櫎娈佃惤parent锛屽鏋滃彧鍒犻櫎run锛屽垯浼氬鑷存钀戒腑杩樻湁涓�涓┖鐨剅un
+ parent.getCTP().newCursor().removeXml();
+ }
+
+ public XWPFParagraph createParagraph(XWPFRun run) {
+ //鑾峰彇body瀹瑰櫒
+ BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(run);
+ //鎻掑叆涓�涓柊鐨勬钀�
+ XWPFParagraph paragraph = bodyContainer.insertNewParagraph(run);
+ return paragraph;
+ }
+
+ public JSONObject getOpinion(String tagName) {
+ //鍏堢敤tagName鍘绘剰瑙佸垪琛ㄤ腑鏌ユ壘node_uuid鏄惁瀛樺湪锛屼笉瀛樺湪鍒欎娇鐢╪ode_title鍘绘煡鎵�
+ for (int i = 0; i < opinionArray.size(); i++) {
+ JSONObject opinion = opinionArray.getJSONObject(i);
+ if (tagName.equals(opinion.getString("node_uuid"))) {
+ return opinion;
+ }
+ }
+ for (int i = 0; i < opinionArray.size(); i++) {
+ JSONObject opinion = opinionArray.getJSONObject(i);
+ if (tagName.equals(opinion.getString("node_title"))) {
+ return opinion;
+ }
+ }
+ return null;
+ }
+}
diff --git a/src/main/java/com/product/print/util/PrintPoiUtil.java b/src/main/java/com/product/print/util/PrintPoiUtil.java
new file mode 100644
index 0000000..2dac1cd
--- /dev/null
+++ b/src/main/java/com/product/print/util/PrintPoiUtil.java
@@ -0,0 +1,138 @@
+package com.product.print.util;
+
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.xwpf.usermodel.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+
+/**
+ * @Author cheng
+ * @Date 2023/8/5 16:40
+ * @Desc
+ */
+public class PrintPoiUtil {
+ /**
+ * 澶嶅埗琛岋紝浠巗ource鍒皌arget
+ *
+ * @param target
+ * @param source
+ */
+ public static void copyTableRow(XWPFTableRow target, XWPFTableRow source) {
+ // 澶嶅埗鏍峰紡
+ if (source.getCtRow() != null) {
+ target.getCtRow().setTrPr(source.getCtRow().getTrPr());
+ }
+ // 澶嶅埗鍗曞厓鏍�
+ for (int i = 0; i < source.getTableCells().size(); i++) {
+ XWPFTableCell cell1 = target.getCell(i);
+ XWPFTableCell cell2 = source.getCell(i);
+ if (cell1 == null) {
+ cell1 = target.addNewTableCell();
+ }
+ copyTableCell(cell1, cell2);
+ }
+
+ }
+
+ /**
+ * 澶嶅埗鍗曞厓鏍硷紝浠巗ource鍒皌arget
+ *
+ * @param target
+ * @param source
+ */
+ public static void copyTableCell(XWPFTableCell target, XWPFTableCell source) {
+ // 鍒楀睘鎬�
+ if (source.getCTTc() != null) {
+ target.getCTTc().setTcPr(source.getCTTc().getTcPr());
+ }
+ // 鍒犻櫎娈佃惤
+ for (int pos = 0; pos < target.getParagraphs().size(); pos++) {
+ target.removeParagraph(pos);
+ }
+ // 娣诲姞娈佃惤
+ for (XWPFParagraph sp : source.getParagraphs()) {
+ XWPFParagraph targetP = target.addParagraph();
+ copyParagraph(targetP, sp);
+ }
+ }
+
+ /**
+ * 澶嶅埗鍥剧墖鍒皌arget
+ *
+ * @param target
+ * @param picture
+ * @throws IOException
+ * @throws InvalidFormatException
+ */
+ public static void copyPicture(XWPFRun target, XWPFPicture picture) throws IOException, InvalidFormatException {
+
+ String filename = picture.getPictureData().getFileName();
+ InputStream pictureData = new ByteArrayInputStream(picture
+ .getPictureData().getData());
+ int pictureType = picture.getPictureData().getPictureType();
+ int width = (int) picture.getCTPicture().getSpPr().getXfrm().getExt()
+ .getCx();
+
+ int height = (int) picture.getCTPicture().getSpPr().getXfrm().getExt()
+ .getCy();
+
+ // target.addBreak();
+ target.addPicture(pictureData, pictureType, filename, width, height);
+ // target.addBreak(BreakType.PAGE);
+ }
+
+ /**
+ * 澶嶅埗娈佃惤锛屼粠source鍒皌arget
+ *
+ * @param target
+ * @param source
+ */
+ public static void copyParagraph(XWPFParagraph target, XWPFParagraph source) {
+
+ // 璁剧疆娈佃惤鏍峰紡
+ target.getCTP().setPPr(source.getCTP().getPPr());
+
+ // 绉婚櫎鎵�鏈夌殑run
+ for (int pos = target.getRuns().size() - 1; pos >= 0; pos--) {
+ target.removeRun(pos);
+ }
+
+ // copy 鏂扮殑run
+ for (XWPFRun s : source.getRuns()) {
+ XWPFRun targetrun = target.createRun();
+ copyRun(targetrun, s);
+ }
+
+
+ }
+
+ /**
+ * 澶嶅埗RUN锛屼粠source鍒皌arget
+ *
+ * @param target
+ * @param source
+ */
+ public static void copyRun(XWPFRun target, XWPFRun source) {
+ // 璁剧疆run灞炴��
+ target.getCTR().setRPr(source.getCTR().getRPr());
+ // 璁剧疆鏂囨湰
+ target.setText(source.text());
+ // 澶勭悊鍥剧墖
+ List<XWPFPicture> pictures = source.getEmbeddedPictures();
+
+ for (XWPFPicture picture : pictures) {
+ try {
+ copyPicture(target, picture);
+ } catch (InvalidFormatException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ System.out.println(source.getColor());
+ System.out.println(target.getColor());
+ }
+}
diff --git a/src/main/java/com/product/print/util/PrintTest.java b/src/main/java/com/product/print/util/PrintTest.java
new file mode 100644
index 0000000..653f73f
--- /dev/null
+++ b/src/main/java/com/product/print/util/PrintTest.java
@@ -0,0 +1,41 @@
+package com.product.print.util;
+
+import cn.hutool.core.io.FileUtil;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.deepoove.poi.XWPFTemplate;
+import com.deepoove.poi.config.Configure;
+import com.deepoove.poi.config.ConfigureBuilder;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @Author cheng
+ * @Date 2023/8/5 14:18
+ * @Desc
+ */
+public class PrintTest {
+
+ static String templatePath = "d:\\desktop\\APP_NOTICE_DECIDE.docx";
+
+ static String outPath = "d:\\desktop\\APP_NOTICE_DECIDE_out.docx";
+
+ public static void main(String[] args) throws IOException {
+ Map<String, Object> values = new HashMap<>();
+ values.put("name", "璁搁箯绋�");
+ ConfigureBuilder config = Configure.createDefault().builder();
+ JSONArray array = new JSONArray();
+ JSONObject object=new JSONObject();
+ object.put("node_uuid","flow");
+ object.put("node_title","node_title");
+ object.put("node_content","node_content");
+ object.put("opinion","opinion");
+ object.put("sign_attachment_uuid","sign_attachment_uuid");
+ array.add(object);
+ config.addPlugin('@', new FlowOpinionRenderPolicy(array));
+ FileUtil.touch(outPath);
+ XWPFTemplate.compile(templatePath, config.build()).render(values).writeToFile(outPath);
+ }
+}
diff --git a/src/main/java/com/product/print/util/TableEmptyHandler.java b/src/main/java/com/product/print/util/TableEmptyHandler.java
new file mode 100644
index 0000000..62f54ba
--- /dev/null
+++ b/src/main/java/com/product/print/util/TableEmptyHandler.java
@@ -0,0 +1,101 @@
+package com.product.print.util;
+
+import cn.hutool.core.util.ArrayUtil;
+import com.deepoove.poi.config.Configure;
+import com.deepoove.poi.render.RenderContext;
+import com.deepoove.poi.template.run.RunTemplate;
+import com.product.core.exception.BaseException;
+import com.product.print.config.CmnCode;
+import org.apache.poi.xwpf.usermodel.*;
+
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @Author cheng
+ * @Date 2023/8/7 10:04
+ * @Desc 琛ㄦ牸鏁版嵁绌烘椂
+ */
+public class TableEmptyHandler extends Configure.ClearHandler {
+
+ private String[] tagName;
+
+ public TableEmptyHandler() {
+ }
+
+ public void addTag(String tagName) {
+ if (this.tagName == null) {
+ this.tagName = new String[]{};
+ }
+ this.tagName = ArrayUtil.append(this.tagName, tagName);
+ }
+
+ @Override
+ public void handler(RenderContext<?> context) {
+ String tagName = context.getEleTemplate().getTagName();
+ if (this.tagName == null || ArrayUtil.indexOf(this.tagName, tagName) == -1) {
+ super.handler(context);
+ return;
+ }
+ RunTemplate runTemplate = (RunTemplate) context.getEleTemplate();
+ XWPFRun run = runTemplate.getRun();
+ XWPFTableCell cell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
+ XWPFTable xwpfTable = cell.getTableRow().getTable();
+ doRemove(xwpfTable, tagName);
+ }
+
+ private void doRemove(XWPFTable xwpfTable, String tagName) {
+
+ List<XWPFTableRow> rows = xwpfTable.getRows();
+ //璇诲彇rows涓殑鍐呭
+ String tableExpression = "{{" + tagName + "}}";
+ //鑾峰彇琛ㄦ牸璧峰琛屽拰缁撴潫琛�
+ int startRowIndex = -1;
+ rows:
+ for (int i = 0; i < rows.size(); i++) {
+ XWPFTableRow row = rows.get(i);
+ for (int j = 0; j < row.getTableCells().size(); j++) {
+ XWPFTableCell cell = row.getTableCells().get(j);
+ String text = cell.getText();
+ if (tableExpression.equals(text)) {
+ startRowIndex = i;
+ break rows;
+ }
+
+ }
+ }
+ if (startRowIndex == -1) {
+ return;
+ }
+
+ //鍦ㄨ〃鏍间腑鏌ユ壘瀛愯〃瀛楁浠{$寮�澶磢}浠}缁撳熬鐨勫唴瀹�
+ String regex = "\\{\\{\\$[a-zA-Z0-9_]+\\}\\}";
+ Pattern pattern = Pattern.compile(regex);
+ //瀛楁鎵�鍦ㄨ
+ XWPFTableRow fieldRow = null;
+ for (int i = startRowIndex; i < rows.size(); i++) {
+ XWPFTableRow row = rows.get(i);
+ for (int j = 0; j < row.getTableCells().size(); j++) {
+ XWPFTableCell cell = row.getTableCells().get(j);
+ String text = cell.getText();
+ Matcher matcher = pattern.matcher(text);
+ if (matcher.find()) {
+ fieldRow = row;
+ i = rows.size();
+ break;
+ }
+ }
+ }
+ //鍒犻櫎璧峰琛�
+ xwpfTable.removeRow(startRowIndex);
+ if (fieldRow == null) {
+ return;
+ }
+ //鑾峰彇瀛楁鎵�鍦ㄨ鐨勭储寮�
+ int fieldRowIndex = xwpfTable.getRows().indexOf(fieldRow);
+ //鍒犻櫎璧峰琛屽埌瀛楁鎵�鍦ㄨ涔嬮棿鐨勬墍鏈夎
+ xwpfTable.removeRow(fieldRowIndex);
+
+ }
+}
diff --git a/src/main/java/com/product/print/util/WordReplaceUtil.java b/src/main/java/com/product/print/util/WordReplaceUtil.java
index 09e12f0..29a4159 100644
--- a/src/main/java/com/product/print/util/WordReplaceUtil.java
+++ b/src/main/java/com/product/print/util/WordReplaceUtil.java
@@ -22,11 +22,7 @@
values.put("name", "璁搁箯绋�");
values.put("n1", 101);
values.put("n2", "20-1");
- XWPFTemplate template = XWPFTemplate.compile("D:\\Desktop\\APP_NOTICE_DECIDE.docx").render(values);
- File file = new File("D:\\Desktop\\test.docx");
- OutputStream out = new ByteArrayOutputStream();
- template.write(out);
- template.close();
+
}
--
Gitblit v1.9.2