Aspose.Words 使用示例:设置字体替代和保存为 PDF
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"); //设置样式 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类。以下是一个示例:
FontSettings fontSettings = new FontSettings();
// 设置字体替代
fontSettings.setSubstitutionSettings(new DefaultFontSubstitutionSettings("宋体"));
// 应用字体设置
doc.setFontSettings(fontSettings);
将上述代码添加到原有代码中的合适位置即可。这样,在将文档保存为PDF格式时,如果原始文档中使用的字体不可用,将会使用"宋体"字体进行替代。
原文地址: https://www.cveoy.top/t/topic/pL00 著作权归作者所有。请勿转载和采集!