您好:
问题:在使用com.aspose.words合并rtf文件并且包含toc,合并60个文件需要18s, 304个文件需要137s。600个文件需要374s。这样是正常的么?还是我的合并逻辑有问题?如果有问题要怎么优化?
环境:windows+ ‘com.aspose:aspose-words:22.9:jdk17’ + jdk21
public static void mergeRtfWithTocAspose(List<MergeRtfDto> mergeRtfDtos, String savePath) throws Exception {
File file = new File(savePath);
String parent = file.getParentFile().getAbsolutePath();
String tempPath = parent + parent + "temp";
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.0D);
builder.getFont().setNameAscii("Times New Roman");
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.setLineSpacing(12.0D);
paragraphFormat.setAlignment(1);
builder.writeln("Table of Contents");
builder.popFont();
builder.writeln("");
FieldToc fieldToc = (FieldToc)builder.insertField(13, true);
fieldToc.setEntryLevelRange("1-3");
fieldToc.setInsertHyperlinks(true);
LoadOptions loadOptions = new LoadOptions();
loadOptions.setTempFolder(tempPath);
long start = System.currentTimeMillis();
for (MergeRtfDto dto : mergeRtfDtos) {
String rtfFile = dto.getRtfFile();
Document src = new Document(rtfFile, loadOptions);
DocumentBuilder srcBuilder = new DocumentBuilder(src);
FieldTC fieldTc = (FieldTC)srcBuilder.insertField(9, true);
fieldTc.setText(dto.getTocTitle());
fieldTc.setEntryLevel("1");
doc.appendDocument(src, 1);
}
System.out.println("appendDocument Time taken: " + (System.currentTimeMillis() - start)/1000 + "s");
long ss = System.currentTimeMillis();
doc = proofreadingPageOrientationAndRemoveLastBlankPage(doc);
doc.getStyles().getByStyleIdentifier(19).getFont().setSize(12.0D);
doc.getStyles().getByStyleIdentifier(19).getParagraphFormat().setLineSpacing(12.0D);
doc.updateFields();
RtfSaveOptions rtfSaveOptions = new RtfSaveOptions();
rtfSaveOptions.setMemoryOptimization(true);
rtfSaveOptions.setExportCompactSize(true);
rtfSaveOptions.setTempFolder(tempPath);
doc.save(savePath, rtfSaveOptions);
System.out.println("save Time taken: " + (System.currentTimeMillis() - ss)/1000 + "s");
}
public class MergeRtfDto {
private String rtfFile;
private String tocTitle;
private Integer num;
}
测试数据
test-data.zip (114.5 KB)
测试几百个文件的时候,直接复制test里面的文件就可以了。