From 64a5b62b0bff5112113732e46165e56e17a4564e Mon Sep 17 00:00:00 2001 From: yujh_java Date: Tue, 22 Jul 2025 09:08:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=8A=9E=E6=B3=95=E6=89=8B?= =?UTF-8?q?=E5=86=8C=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pal/pal/output/util/OutputWordUtil.java | 178 +++++++++++++----- 1 file changed, 135 insertions(+), 43 deletions(-) diff --git a/com.actionsoft.apps.coe.pal/src/com/actionsoft/apps/coe/pal/pal/output/util/OutputWordUtil.java b/com.actionsoft.apps.coe.pal/src/com/actionsoft/apps/coe/pal/pal/output/util/OutputWordUtil.java index 28290618..0a1e87a4 100755 --- a/com.actionsoft.apps.coe.pal/src/com/actionsoft/apps/coe/pal/pal/output/util/OutputWordUtil.java +++ b/com.actionsoft.apps.coe.pal/src/com/actionsoft/apps/coe/pal/pal/output/util/OutputWordUtil.java @@ -2881,58 +2881,150 @@ public class OutputWordUtil { * 判断是否需求合并管理要求 * @param uuid */ - public static void mergeMRWord(String filePath, String uuid){ + public static void mergeMRWord(String filePath, String uuid) { Map stringJSONObjectMap = PALRepositoryQueryAPIManager.queryRepositoryAttributeById(uuid); JSONObject attrJson = stringJSONObjectMap.get("management_requirements_Location"); String text = attrJson.getString("text"); - if("不显示管理要求".equals(text)){ + if ("不显示管理要求".equals(text)) { return; } BO mrBO = SDK.getBOAPI().getByKeyField("BO_EU_PROCESS_MR", "FILEUUID", uuid); - if(null!=mrBO){ - String status = mrBO.getString("STATUS"); - if(status.equals("1")){//如果为生效状态 - //先获取管理要求附件 - List systemfile = SDK.getBOAPI().getFiles(mrBO.getId(), "SYSTEMFILE"); - FormFile formFile = systemfile.get(0); - DCContext fileDCContext = SDK.getBOAPI().getFileDCContext(formFile); - //开始合并 - Document docs1 = new Document(); - docs1.loadFromFile(filePath); - // 加载文档2 - Document docs2 = new Document(); - docs2.loadFromFile(fileDCContext.getFilePath()); - SectionCollection sections1 = docs1.getSections(); - //先确认要找到的段落 - int targetParagraphIndex = 0; - if("流程图前".equals(text)){ - for (int i = 1; i < sections1.getCount(); i++) { - Section section = sections1.get(i); - for (int j = 0; j < section.getParagraphs().getCount(); j++) { - Paragraph para = section.getParagraphs().get(j); - if (para.getText().contains("流程图")) { - targetParagraphIndex = i; - break; - } - } - } - }else if("活动说明后".equals(text)){ - for (int i = 1; i < sections1.getCount(); i++) { - Section section = sections1.get(i); - for (int j = 0; j < section.getParagraphs().getCount(); j++) { - Paragraph para = section.getParagraphs().get(j); - if (para.getText().contains("流程说明")) { - targetParagraphIndex = i+1; - break; - } - } + if (null == mrBO || !"1".equals(mrBO.getString("STATUS"))) { + return; + } + + List systemfile = SDK.getBOAPI().getFiles(mrBO.getId(), "SYSTEMFILE"); + if (systemfile.isEmpty()) { + return; + } + FormFile formFile = systemfile.get(0); + DCContext fileDCContext = SDK.getBOAPI().getFileDCContext(formFile); + + // 加载目标文件和待合并文档2 + Document docs1 = new Document(); + docs1.loadFromFile(filePath); + Document docs2 = new Document(); + docs2.loadFromFile(fileDCContext.getFilePath()); + + // 定位插入位置:根据text判断是“流程图前”还是“活动说明后(表格后)” + Paragraph targetPara = null; + Section targetSection = null; + int targetParaIndex = -1; + if ("流程图前".equals(text)) { + // 保持原有“流程图前”逻辑(适配文件中“# 2. 流程图”) + for (Object obj : docs1.getSections()) { + Section sec = (Section) obj; + int i = 0; + for (Object parObj : sec.getParagraphs()) { + Paragraph para = (Paragraph) parObj; + if (para.getText().trim().contains("2. 流程图") || para.getText().trim().equals("流程图")) { + targetPara = para; + targetSection = sec; + targetParaIndex = i; + break; } + i++; + } + if (targetPara != null) { + break; } - sections1.insert(targetParagraphIndex, docs2.getSections().get(0).deepClone()); - // 保存文档2 - docs1.saveToFile(filePath, FileFormat.Docx_2013); - docs1.dispose(); } + } else if ("活动说明后".equals(text)) { + boolean foundProcessDesc = false; // 标记已找到“3. 流程说明”标题 + int lastTableParaIndex = -1; // 记录流程说明表格最后一行的索引 + for (Object obj : docs1.getSections()) { + Section sec = (Section) obj; + int i = 0; + for (Object paragraph : sec.getParagraphs()) { + Paragraph para = (Paragraph) paragraph; + String paraText = para.getText().trim(); + + // 修正1:匹配文件中实际的“3. 流程说明”标题(含#和数字) + if (paraText.contains("3. 流程说明") || paraText.contains("流程说明")) { + foundProcessDesc = true; + continue; + } + // 修正2:在“流程说明”后,持续追踪表格最后一行(含|符号) + if (foundProcessDesc) { + // 表格行含|(如“|01|线上审批55555|...”) + if (paraText.contains("|")) { + lastTableParaIndex = i; // 持续更新表格最后一行索引 + } else { + // 遇到非表格内容时,插入位置为表格最后一行的下一行 + if (lastTableParaIndex != -1) { + targetParaIndex = i; // 插入到当前非表格段落前(即表格下方) + targetPara = para; + targetSection = sec; + break; + } + } + } + i++; + } + + + if (targetPara != null) { + break; + } + } + } + + // 未找到目标位置则退出 + if (targetPara == null || targetSection == null) { + return; + } + + // 插入文档2内容(保持格式与文件协调) + Section doc2Sec = docs2.getSections().get(0); + boolean isFirstPara = true; + for (Object obj : doc2Sec.getParagraphs()) { + Paragraph doc2Para = (Paragraph) obj; + Paragraph clonedPara = (Paragraph) doc2Para.deepClone(); + ParagraphFormat format = clonedPara.getFormat(); + + // 核心格式:确保同页且不挤压 + format.setPageBreakBefore(false); + format.setPageBreakAfter(false); + format.setKeepLines(true); + format.setKeepFollow(true); + format.setLineSpacingRule(LineSpacingRule.At_Least); + format.setLineSpacing(12f); // 适配文件中行高 + + // 第一段与表格保持6磅间距(避免贴紧表格底部) + if (isFirstPara) { + float beforeSpacing = format.getBeforeSpacing(); + format.setBeforeSpacing(beforeSpacing > 0 ? beforeSpacing : 6f); + isFirstPara = false; + } + + // 插入到目标位置(表格下方) + targetSection.getParagraphs().insert(targetParaIndex, clonedPara); + } + + // 调整后续段落(如矩阵标题)与插入内容的间距 + ParagraphFormat targetFormat = targetPara.getFormat(); + targetFormat.setBeforeSpacing(6f); // 与插入内容保持6磅间距 + targetFormat.setPageBreakBefore(false); + + // 保存文档 + docs1.saveToFile(filePath, FileFormat.Docx_2013); + docs1.dispose(); + docs2.dispose(); + } + + // 新增:清除文档中的分页符(针对文档2) + private static void clearPageBreaks(Document doc) { + for (Object obj : doc.getSections()) { + Section section= (Section) obj; + // 遍历段落,移除手动分页符(假设分页符以特殊字符或元素存在) + for (Object obj1 : section.getParagraphs()) { + Paragraph para = (Paragraph) obj1; + String paraText = para.getText(); + if (paraText.contains("\f")) { + para.setText(paraText.replace("\f", "")); // 清除分页符字符 + } + } + } } }