在调用doc.save()方法保存为PDF格式之前,可以使用Aspose.Words的替换规则功能来替换字体,以解决图片字体缺失乱码问题。下面是一个示例代码,演示了如何使用替换规则来替换字体:

public static void main(String[] args) throws Exception {
    License aposeLic = new License();
    aposeLic.setLicense("D:\\work\\project\\prit-research-comment\\src\\main\\resources\\Aspose.Words.lic");

    Document doc = new Document("D:\\work\\project\\prit-research-comment\\src\\main\\resources\\科锐国际调研纪要.doc");

    // 创建字体替换规则
    FontSettings fontSettings = new FontSettings();
    FontSubstitutionSettings fontSubstitutionSettings = new FontSubstitutionSettings();
    fontSubstitutionSettings.getTableSubstitution().addSubstitutes("Arial", "SimSun");
    fontSettings.setFontSubstitution(fontSubstitutionSettings);

    // 应用字体替换规则
    doc.setFontSettings(fontSettings);

    // 设置样式
    doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getFont().setSize(10.5);
    doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getFont().setBold(true);
    doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getFont().setName("宋体");
    doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getParagraphFormat().setLineSpacing(18);

    // 保存为PDF格式
    doc.save("D:\\work\\project\\prit-research-comment\\src\\main\\resources\\科锐国际调研纪要.pdf", SaveFormat.PDF);
}

在上述代码中,我们首先创建了一个FontSettings对象,并设置了字体替换规则。然后,通过setFontSettings()方法将字体替换规则应用到文档中。接下来,我们设置了样式,并最后调用doc.save()方法将文档保存为PDF格式。

在字体替换规则中,我们使用fontSubstitutionSettings.getTableSubstitution().addSubstitutes()方法来添加字体替换规则。在这个示例中,我们将Arial字体替换为SimSun字体。你可以根据需要修改替换规则来适应你的具体情况。


原文地址: https://www.cveoy.top/t/topic/pLsg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录