Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

文档嵌套对同样式序号进行合并 #1166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ooxml.POIXMLDocument;
Expand All @@ -41,12 +42,7 @@
import org.apache.poi.util.IOUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

public class XmlXWPFDocumentMerge extends AbstractXWPFDocumentMerge {

Expand Down Expand Up @@ -301,6 +297,49 @@ protected Map<String, String> mergeExternalPicture(NiceXWPFDocument source, Nice
return blipIdsMap;
}

protected Map<BigInteger, BigInteger> mergeAbstractNumId(XWPFNumbering source, XWPFNumbering merged) {
List<XWPFAbstractNum> abstractNums = source.getAbstractNums();
// 获取source 文档abstractNum的lvl结构, key->lvl结构, value->abstractNumId
Map<String, BigInteger> sourceMap = new HashMap<>(abstractNums.size());
for (XWPFAbstractNum abstractNum : abstractNums) {
CTAbstractNum ctAbstractNum = abstractNum.getAbstractNum();
BigInteger abstractNumId = ctAbstractNum.getAbstractNumId();
String lvlString = getLvlString(ctAbstractNum);
sourceMap.put(lvlString, abstractNumId);
}

// 获取与源文档相同的AbstractNumId映射,key->mergedAbstractNumId, value->sourceAbstractNumId
Map<BigInteger, BigInteger> abstractNumMap = new HashMap<>();
List<XWPFAbstractNum> mergedAbstractNums = merged.getAbstractNums();
for (XWPFAbstractNum mergedAbstractNum : mergedAbstractNums) {
CTAbstractNum ctAbstractNum = mergedAbstractNum.getAbstractNum();
BigInteger abstractNumId = ctAbstractNum.getAbstractNumId();
String lvlString = getLvlString(ctAbstractNum);
if (sourceMap.containsKey(lvlString)) {
abstractNumMap.put(abstractNumId, sourceMap.get(lvlString));
}
}
return abstractNumMap;
}

/**
* 拼接numlvl关键结构用于去重
* @param ctAbstractNum
* @return
*/
private static String getLvlString(CTAbstractNum ctAbstractNum) {
List<CTLvl> lvlList = ctAbstractNum.getLvlList();
String lvlString = lvlList.stream().map(ctLvl -> {
BigInteger ilvl = ctLvl.getIlvl();
BigInteger startVal = ctLvl.getStart().getVal();
String numFmt = ctLvl.getNumFmt().getFormat();
String lvlText = ctLvl.getLvlText().getVal();
String lvlJc = ctLvl.getLvlJc().getVal().toString();
return StringUtils.join(ilvl, startVal, numFmt, lvlText, lvlJc);
}).collect(Collectors.joining());
return lvlString;
}

protected Map<String, String> mergeNumbering(NiceXWPFDocument source, NiceXWPFDocument merged) {
Map<String, String> numIdsMap = new HashMap<String, String>();
XWPFNumbering numberingMerge = merged.getNumbering();
Expand All @@ -312,6 +351,15 @@ protected Map<String, String> mergeNumbering(NiceXWPFDocument source, NiceXWPFDo
XWPFNumbering numbering = source.getNumbering();
if (null == numbering) numbering = source.createNumbering();
XWPFNumberingWrapper wrapper = new XWPFNumberingWrapper(numbering);
// 获取与source同样式的abstractNumId映射 key->mergedAbstractNumId, value->sourceAbstractNumId
Map<BigInteger, BigInteger> mergeAbstractNumId = mergeAbstractNumId(numbering, numberingMerge);
// 通样式的原numId映射,key->sourceAbstractNumId,value->sourceNumId
Map<BigInteger, BigInteger> sourceNumIdMap = new HashMap<>();
numbering.getNums().forEach(xwpfNum -> {
BigInteger numId = xwpfNum.getCTNum().getNumId();
BigInteger abstractNumId = xwpfNum.getCTNum().getAbstractNumId().getVal();
sourceNumIdMap.put(abstractNumId, numId);
});

XWPFAbstractNum xwpfAbstractNum;
CTAbstractNum cTAbstractNum;
Expand All @@ -333,6 +381,13 @@ protected Map<String, String> mergeNumbering(NiceXWPFDocument source, NiceXWPFDo
if (cTAbstractNum.isSetTmpl()) cTAbstractNum.unsetTmpl();
cache.put(xwpfNum.getCTNum().getAbstractNumId().getVal(), cTAbstractNum);
}
// source存在同样式的AbstractNumId,将merge的AbstractNumId进行替换
if (mergeAbstractNumId.containsKey(cTAbstractNum.getAbstractNumId())) {
BigInteger sourceAbstractNumId = mergeAbstractNumId.get(cTAbstractNum.getAbstractNumId());
cTAbstractNum.setAbstractNumId(sourceAbstractNumId);
numIdsMap.put(mergeNumId.toString(), sourceNumIdMap.get(sourceAbstractNumId).toString());
continue;
}
ret.put(mergeNumId, cTAbstractNum);
}
long nextId = wrapper.getNextAbstractNumID().longValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.deepoove.poi.tl.example;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.Includes;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

/**
* @author vinceLin054
* @version NumberMergeExample.java, v 0.1 2024年08月14日 14:34
*/
public class NumberMergeExample {


/**
* case1: 存在相同样式及不同样式的序号
* @throws Exception
*/
@Test
public void testNumberMergeCase1() throws Exception {
XWPFTemplate.compile("src/test/resources/number/case1_1.docx").render(new HashMap<String, Object>() {
{
put("term1", Includes.ofLocal("src/test/resources/number/case1_2.docx").create());
put("term2", Includes.ofLocal("src/test/resources/number/case1_3.docx").create());
}
}).writeToFile("src/test/resources/number/case1_result.docx");
}


/**
* case2: 不存在相同的样式
* @throws Exception
*/
@Test
public void testNumberMergeCase2() throws Exception {
XWPFTemplate.compile("src/test/resources/number/case2_1.docx").render(new HashMap<String, Object>() {
{
put("term", Includes.ofLocal("src/test/resources/number/case2_2.docx").create());
}
}).writeToFile("src/test/resources/number/case2_result.docx");
}
}
Binary file added poi-tl/src/test/resources/number/case1_1.docx
Binary file not shown.
Binary file added poi-tl/src/test/resources/number/case1_2.docx
Binary file not shown.
Binary file added poi-tl/src/test/resources/number/case1_3.docx
Binary file not shown.
Binary file added poi-tl/src/test/resources/number/case2_1.docx
Binary file not shown.
Binary file added poi-tl/src/test/resources/number/case2_2.docx
Binary file not shown.