Java使用Aspose Words合并rtf文件,比较耗时

您好:
问题:在使用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里面的文件就可以了。

@imzdong

在使用Aspose.Words合并RTF文件时,合并多个文件的时间消耗可能会受到多种因素的影响,包括文件的大小、内容复杂性以及合并逻辑的实现方式。根据您提供的代码和描述,合并60个文件需要18秒,304个文件需要137秒,600个文件需要374秒,这样的性能表现可能是正常的,尤其是在处理大量文档时。

以下是一些可能的优化建议,以提高合并RTF文件的效率:

  1. 减少临时文件的使用:在合并过程中,您使用了临时文件夹来存储加载的文档。确保临时文件夹的路径是有效的,并且在合并过程中尽量减少对临时文件的频繁读写。

  2. 优化文档加载:在加载每个RTF文件时,您可以考虑使用更高效的加载选项。例如,您可以尝试使用LoadFormat.RTF来明确指定文件格式,这可能会加快加载速度。

  3. 并行处理:如果您的环境支持多线程,您可以考虑将文件的加载和合并过程并行化。这样可以充分利用多核处理器的优势,减少整体处理时间。

  4. 更新字段的时机:在合并所有文档后再调用doc.updateFields(),而不是在每次合并后更新字段,这样可以减少不必要的计算。

  5. 检查文档内容:确保合并的RTF文件内容尽量简化,复杂的格式和内容可能会增加处理时间。

  6. 使用最新版本的Aspose.Words:确保您使用的是Aspose.Words的最新版本,因为新版本通常会包含性能改进和bug修复。

通过这些优化,您可能会看到合并过程的时间有所减少。如果问题仍然存在,建议您在Aspose的支持论坛上寻求进一步的帮助,提供更多的上下文信息和性能数据,以便社区成员能够提供更具体的建议。

希望这些建议对您有所帮助!

@imzdong 执行时间取决于文件的复杂程度以及 CPU 和内存的大小。 我在我的机器上用最新的 25.2 版本检查了您的代码,有 608 个文件,结果如下:

appendDocument Time taken: 97s
save Time taken: 51s

速度要快得多。 也请使用最新版本进行检查。