Hi , 我的问题,合并word包含toc,toc的宽度和被合并文件不一致。帮忙看下,感谢
技术栈:jdk21 + spring boot 3.2.5 + aspose-words-21.5.0-jdk17
合并代码:
public static final String tocFontName = "Arial";
private static final License ASPOSE_LICENSE = new License();
static {
try {
//Aspose.WordsProductFamily.lic license.xml
URL resource = AsposeUtil.class.getClassLoader().getResource("license.xml");
if (resource != null) {
InputStream stream = resource.openStream();
ASPOSE_LICENSE.setLicense(stream);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws Exception {
String prefixPath = "D:\\WorkSpace\\Test\\merge\\6\\";
Map<String, String> names = Map.of("Table 14.1.1.1.1_Shell_Solution_HC3_Oncology_01_dev_test_Dong-0625-Test-01-IN63935_Production_20250701 (6).docx",
"Table 14.1.1.1.1_Shell_Solution_HC3_Oncology_01_dev_test_Dong-0625-Test-01-IN63935_Production_20250701 (6)",
"Table 14.1_Shell_Solution_HC3_Oncology_01_dev_test_Dong-0625-Test-01-IN63935_Production_20250701 (3).docx",
"Table 14.1_Shell_Solution_HC3_Oncology_01_dev_test_Dong-0625-Test-01-IN63935_Production_20250701 (3)");
String mergeDocName = "merge6.docx";
String savePath = prefixPath + File.separator + mergeDocName;
String tempPath = prefixPath + File.separator + "temp";
//setFontDir();
File tempFile = new File(tempPath);
if (!tempFile.exists()) {
tempFile.mkdir();
}
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.pushFont();
builder.getFont().setSize(18);
builder.getFont().setNameAscii(tocFontName);
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.setLineSpacing(12);
paragraphFormat.setAlignment(ParagraphAlignment.CENTER);
builder.writeln("Table of Contents");
builder.popFont();
builder.writeln("");
FieldToc fieldToc = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
fieldToc.setEntryLevelRange("1-3");
fieldToc.setInsertHyperlinks(true);
LoadOptions loadOptions = new LoadOptions();
loadOptions.setTempFolder(tempPath);
long start = System.currentTimeMillis();
loadOptions.setLoadFormat(ImportFormatMode.KEEP_SOURCE_FORMATTING);
names.forEach((fileName, tocTitle) -> {
try {
Document src = new Document(prefixPath + File.separator + fileName, loadOptions);
DocumentBuilder srcBuilder = new DocumentBuilder(src);
FieldTC fieldTc = (FieldTC) srcBuilder.insertField(FieldType.FIELD_TOC_ENTRY, true);
fieldTc.setText(tocTitle);
fieldTc.setEntryLevel("1");
doc.appendDocument(src, ImportFormatMode.KEEP_SOURCE_FORMATTING);
} catch (Exception e) {
e.printStackTrace();
}
});
doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getFont().setSize(12);
doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getParagraphFormat().setLineSpacing(12);
long ss1 = System.currentTimeMillis();
doc.updateFields();
long ss2 = System.currentTimeMillis();
doc.setWarningCallback(warningInfo -> {
if (warningInfo.getWarningType() == WarningType.FONT_SUBSTITUTION) {
} else {
}
});
doc.save(savePath, SaveFormat.DOCX);
}
合并文件和合并后的文件。
merge-word.zip (30.8 KB)