Aspose words转换word为pdf,页数不对了

技术栈:jdk21 + spring boot 3.2.5 + aspose-words-21.5.0-jdk17
转换代码:

public static boolean doc2pdf(String inPath, String outPath) {

        File file = new File(outPath);
        try (FileOutputStream os = new FileOutputStream(file)) {
            Document doc = new Document(inPath);
            doc.save(os, SaveFormat.PDF);
        } catch (Exception e) {
            log.error("[doc2pdf]: convert to pdf failed, inPath={}, outPath={}, exp=", inPath, outPath, e);
            return false;
        }
        return true;
    }

源文件(只有一页)
Table-1.docx (4.1 KB)
转换后的pdf(两页)
Table-1.pdf (30.1 KB)

please help, ty。

@imzdong 您需要使用此代码才能得到正确的结果:

LoadOptions loadOptions = new LoadOptions();
loadOptions.getLanguagePreferences().setDefaultEditingLanguage(EditingLanguage.CHINESE_PRC);

Document doc = new Document("Table-1.docx", loadOptions);
doc.save("output.pdf");

output.pdf (29.1 KB)

感谢哈,麻烦给解释下为啥这么改就可以了?
我的word中也没有中文.
我设置了这个参数为EditingLanguage.CHINESE_PRC。对英文,或者其他语言有没有影响?
word中我根据什么去判断修改这个参数呢?
感谢。

@imzdong 英文和中文的布局不同,MS Word 有时会根据语言和地域显示不同的内容。 为了正确显示中文文档,我们通常会在 MS Word 和 Aspose.Words 中将中文设置为首选语言:

感谢,但是我们公司的产品都是对外的,英文。
我项目中怎么解决这个问题呢?
LoadOptions loadOptions = new LoadOptions();
loadOptions.getLanguagePreferences().setDefaultEditingLanguage(EditingLanguage.CHINESE_PRC);

如果我设置这样,会对英文有影响么?

@imzdong 通常情况下,它不会影响英文,但在某些情况下,没有任何其他选项可以获得正确的中文文件输出。 如果你会遇到一些问题,或许可以考虑根据文档语言切换加载选项。

@vyacheslav.deryushev 好的 非常感谢

1 Like