From 7efe804fa8620786efacb61e6e8e5790424b2408 Mon Sep 17 00:00:00 2001 From: chengli <783535094@qq.com> Date: Sat, 9 Jul 2022 14:28:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=82=E8=BD=BD=E9=99=84=E4=BB=B6~=E8=A7=A3?= =?UTF-8?q?=E6=9E=90word=E5=86=85=E7=9A=84=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/readtable/ReadTable.java | 92 +++++++++++++++---- .../util/readword/ReadWordUtil.java | 74 ++++++++++++--- 2 files changed, 133 insertions(+), 33 deletions(-) diff --git a/com.actionsoft.apps.coe.pal.datamigration/src/com/actionsoft/apps/coe/pal/datamigration/util/readtable/ReadTable.java b/com.actionsoft.apps.coe.pal.datamigration/src/com/actionsoft/apps/coe/pal/datamigration/util/readtable/ReadTable.java index 743c5ecc..f147e59a 100644 --- a/com.actionsoft.apps.coe.pal.datamigration/src/com/actionsoft/apps/coe/pal/datamigration/util/readtable/ReadTable.java +++ b/com.actionsoft.apps.coe.pal.datamigration/src/com/actionsoft/apps/coe/pal/datamigration/util/readtable/ReadTable.java @@ -23,10 +23,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.*; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.util.*; /** @@ -98,30 +95,42 @@ public class ReadTable { e.printStackTrace(); } } - //解析文档附件 - PALRepositoryModel palRepositoryModel = ReadWordUtil.getRepositoryByName(wsId, docxname.replace(".docx", "")); - if (palRepositoryModel == null) { - return; - } - String id = palRepositoryModel.getId(); - + DCPluginProfile dcProfilepdfdoc = DCProfileManager.getDCProfile("com.actionsoft.apps.coe.pal.datamigration", "migration"); //进行文件解析 写基本信息 - DCPluginProfile dcProfilepdfdoc = DCProfileManager.getDCProfile("com.actionsoft.apps.coe.pal.datamigration", "migration"); + //DCPluginProfile dcProfilepdfdoc = DCProfileManager.getDCProfile("com.actionsoft.apps.coe.pal.datamigration", "migration"); DCContext dcContextpdfdoc = new DCContext(userContext, dcProfilepdfdoc, "com.actionsoft.apps.coe.pal.datamigration", groupValue, fileValue, docname); InputStream docfile = SDK.getDCAPI().read(dcContextpdfdoc); //画一个图形 并返回图形ID String shapId = createOneMap(wsId, docname.replace(".doc", ""), userContext, docfile); + if(shapId.isEmpty()){ + return; + } + String title = fileFieldMap.get("标题").getData().toString(); + //解析文档附件 + PALRepositoryModel palRepositoryModel = ReadWordUtil.getRepositoryByName(wsId, title); + if (palRepositoryModel == null) { + return; + } + String id = palRepositoryModel.getId(); + //将文件挂载到附件里面 + DCContext dcContextorigin = new DCContext(userContext, dcProfilepdfdoc, "com.actionsoft.apps.coe.pal.datamigration", groupValue, fileValue, fileName); + InputStream originfile = SDK.getDCAPI().read(dcContextorigin); + try { + new ReadWordUtil().writeFileTodisk(userContext, "", fileName, originfile, palRepositoryModel.getId(),"f"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } //先把默认属性给他弄上~~~然后再赋值 - BaseModel defineModel = CoeDesignerAPIManager.getInstance().getDefinition(palRepositoryModel.getId(), 0); + /*BaseModel defineModel = CoeDesignerAPIManager.getInstance().getDefinition(palRepositoryModel.getId(), 0); if (defineModel == null) { defineModel = CoeDesignerUtil.createModel(palRepositoryModel.getId(), 0); defineModel.setCreateHistory(false); - } - String define = defineModel.getDefinition(); + }*/ + /*String define = defineModel.getDefinition(); JSONObject definition = JSONObject.parseObject(define); JSONObject elements = definition.getJSONObject("elements"); - Map> methodAttrsMap = new HashMap<>(); + Map> methodAttrsMap = new HashMap<>();*/ //handleShapeDefaultAttr("", palRepositoryModel, elements, methodAttrsMap, docfile); //writeAttrbute(userContext, docfile, docname.replace(".doc", ""), elements.getJSONObject(shapId)); //解析附件进行复制 @@ -169,7 +178,15 @@ public class ReadTable { * @return */ public String createOneMap(String plId, String name, UserContext userContext, InputStream docfile) { - PALRepositoryModel palRepositoryModel = ReadWordUtil.getRepositoryByName(plId, name); + boolean result = analysisWordTable(docfile); + String title = fileFieldMap.get("标题").getData().toString(); + if(title.isEmpty()){ + return ""; + } + PALRepositoryModel palRepositoryModel = ReadWordUtil.getRepositoryByName(plId, title); + if(palRepositoryModel == null){ + return ""; + } BaseModel defineModel = CoeDesignerAPIManager.getInstance().getDefinition(palRepositoryModel.getId(), 0); if (defineModel == null) { defineModel = CoeDesignerUtil.createModel(palRepositoryModel.getId(), 0); @@ -185,7 +202,7 @@ public class ReadTable { //增加一个表单图形 JSONObject shapeze = ShapeUtil.getProcessShapeDefinitionByName("data.form", "form"); String shapeIdz = UUIDGener.getObjectId(); - shapeze.put("text", name);//不生效 + shapeze.put("text", title);//不生效 shapeze.put("level", 0); JSONObject props1 = shapeze.getJSONObject("props");// 位置大小 shapeze.put("id", shapeIdz); @@ -212,6 +229,7 @@ public class ReadTable { * @return */ public boolean analysisWordTable(InputStream file) { + boolean ischeck = false; try { HWPFDocument doc = new HWPFDocument(file); //XWPFDocument doc = new XWPFDocument(file); @@ -222,6 +240,22 @@ public class ReadTable { if (StringUtils.isEmpty(text)) { continue; } + if(nowPara.text().contains("文件编码")){ + ischeck = true; + } + if(ischeck){ + boolean isTitle = isTitle(nowPara); + if(isTitle){ + String title = ""; + if(fieldMap.get("标题") != null){ + title = fieldMap.get("标题").toString(); + } + title = title+nowPara.text().replace("\r",""); + fieldMap.put("标题", title); + fileFieldMap.put("标题", new WordField<>(title)); + } + } + /*WordAttribute wordAttribute = wordAttributeMap.get(text.trim()); if (null == wordAttribute) { continue; @@ -316,7 +350,6 @@ public class ReadTable { * @param methodAttrsMap */ public void handleShapeDefaultAttr(String wsId, PALRepositoryModel palModel, JSONObject elements, Map> methodAttrsMap, InputStream file) { - boolean result = analysisWordTable(file); for (String key : elements.keySet()) { JSONObject shape = elements.getJSONObject(key); if ("linker".equals(shape.getString("name"))) { @@ -395,4 +428,25 @@ public class ReadTable { object2.put("value", ""); return object2; } + /** + * 判断是否为文档标题 + * 标志:黑体一号 + * @param paragraph + * @return + */ + public boolean isTitle(Paragraph paragraph){ + int size = paragraph.numCharacterRuns(); + String fontName = ""; + int fontSize = 0; + boolean bold ; + for (int j = 0; j < size; j++) { + CharacterRun characterRun = paragraph.getCharacterRun(j); + fontName = characterRun.getFontName(); + if (fontName.contains("黑体")){ + //与文档fontsize标号2倍关系 ,黑体一号 等于 26号size + return characterRun.getFontSize() == 44; + } + } + return false; + } } diff --git a/com.actionsoft.apps.coe.pal.datamigration/src/com/actionsoft/apps/coe/pal/datamigration/util/readword/ReadWordUtil.java b/com.actionsoft.apps.coe.pal.datamigration/src/com/actionsoft/apps/coe/pal/datamigration/util/readword/ReadWordUtil.java index dd18336c..e252c132 100644 --- a/com.actionsoft.apps.coe.pal.datamigration/src/com/actionsoft/apps/coe/pal/datamigration/util/readword/ReadWordUtil.java +++ b/com.actionsoft.apps.coe.pal.datamigration/src/com/actionsoft/apps/coe/pal/datamigration/util/readword/ReadWordUtil.java @@ -82,6 +82,10 @@ public class ReadWordUtil { public void writeAttrbute(UserContext userContext, InputStream file, String name, String wsdl) { boolean result = analysisWordTable(file); if (result) { + String title = fileFieldMap.get("标题").getData().toString(); + if(title.isEmpty()){ + return; + } //根据名称获取PAL制度模型 PALRepositoryModel palRepositoryModel = getRepositoryByName(wsdl, name); if (palRepositoryModel != null) { @@ -171,28 +175,37 @@ public class ReadWordUtil { //将文件挂载到附件里面 DCContext dcContextorigin = new DCContext(userContext, dcProfilepdfdoc, "com.actionsoft.apps.coe.pal.datamigration", groupValue, fileValue, fileName); InputStream originfile = SDK.getDCAPI().read(dcContextorigin); - PALRepositoryModel palRepositoryModel = getRepositoryByName(wsId, docname.replace(".doc", "")); - if (palRepositoryModel != null) { - try { - writeFileTodisk(userContext, "", fileName, originfile, palRepositoryModel.getId(),"f"); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } + //进行文件解析 DCContext dcContextpdfdoc = new DCContext(userContext, dcProfilepdfdoc, "com.actionsoft.apps.coe.pal.datamigration", groupValue, fileValue, docname); InputStream docfile = SDK.getDCAPI().read(dcContextpdfdoc); - ReadWordUtil tmp = new ReadWordUtil(); - tmp.writeAttrbute(userContext, docfile, docname.replace(".doc", ""), wsId); + //ReadWordUtil tmp = new ReadWordUtil(); + writeAttrbute(userContext, docfile, fileName, wsId); + String title = fileFieldMap.get("标题").getData().toString(); + if(title.isEmpty()){ + return; + } + PALRepositoryModel palRepositoryModel = getRepositoryByName(wsId, title); + if (palRepositoryModel != null) { + try { + writeFileTodisk(userContext, "", fileName, originfile, palRepositoryModel.getId(), "f"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } else { + return; + } //创建图形 DCContext dcContextpdf1 = new DCContext(userContext, dcProfilepdfdoc, "com.actionsoft.apps.coe.pal.datamigration", groupValue, fileValue, docxname); InputStream docxfile = SDK.getDCAPI().read(dcContextpdf1); - new CreateMaps().updateMaps(userContext, wsId, docxfile, docxname.replace(".docx", "")); + new CreateMaps().updateMaps(userContext, wsId, docxfile, title); } public boolean analysisWordTable(InputStream file) { + fileFieldMap.clear(); + fieldMap.clear(); try { HWPFDocument doc = new HWPFDocument(file); //XWPFDocument doc = new XWPFDocument(file); @@ -203,6 +216,16 @@ public class ReadWordUtil { if (StringUtils.isEmpty(text)) { continue; } + boolean isTitle = isTitle(nowPara); + if(isTitle){ + String title = ""; + if(fieldMap.get("标题") != null){ + title = fieldMap.get("标题").toString(); + } + title = title+nowPara.text().replace("\r",""); + fieldMap.put("标题", title); + fileFieldMap.put("标题", new WordField<>(title)); + } WordAttribute wordAttribute = wordAttributeMap.get(text.trim()); if ("目的".equals(text.trim())) { int index = i + 1 > range.numParagraphs() ? i : i + 1; @@ -429,6 +452,7 @@ public class ReadWordUtil { /** * 将文件挂载到附件 + * * @param me * @param reid * @param filename @@ -437,7 +461,7 @@ public class ReadWordUtil { * @param type * @throws FileNotFoundException */ - public void writeFileTodisk(UserContext me, String reid, String filename, InputStream inputStream, String pl_uuid,String type) throws FileNotFoundException { + public void writeFileTodisk(UserContext me, String reid, String filename, InputStream inputStream, String pl_uuid, String type) throws FileNotFoundException { //InputStream ins = new FileInputStream(""); DCPluginProfile fileProfile = DCProfileManager.getDCProfile(CoEConstant.APP_ID, CoeFileConstant.COE_UPFILE); DCContext dcContextpdf = new DCContext(me, fileProfile, CoEConstant.APP_ID, "file", pl_uuid, filename); @@ -445,10 +469,10 @@ public class ReadWordUtil { //创建数据库数据 UpfileModel model = new UpfileModel(); model.setPl_uuid(pl_uuid); - if("f".equals(type)){ + if ("f".equals(type)) { model.setShape_uuid(""); model.setType("f"); - }else { + } else { model.setShape_uuid(reid); model.setType("s"); } @@ -460,6 +484,28 @@ public class ReadWordUtil { new UpFileDao().create(model); } + /** + * 判断是否为文档标题 + * 标志:黑体一号 + * @param paragraph + * @return + */ + public boolean isTitle(Paragraph paragraph){ + int size = paragraph.numCharacterRuns(); + String fontName = ""; + int fontSize = 0; + boolean bold ; + for (int j = 0; j < size; j++) { + CharacterRun characterRun = paragraph.getCharacterRun(j); + fontName = characterRun.getFontName(); + if (fontName.contains("黑体")){ + //与文档fontsize标号2倍关系 ,黑体一号 等于 26号size + return characterRun.isBold() && characterRun.getFontSize() == 52; + } + } + return false; + } + /** * 根据名字寻找palmode *