word文件和pdf文件:我确认安装了字体
新建文件夹.zip (73.7 KB)
代码:
public static void main(String[] args) throws Exception{
com.aspose.words.License license = new com.aspose.words.License();
license.setLicense("src/test/resources/lic/newlic.lic");
Document doc = new Document(new FileInputStream("C:\\Users\\Administrator\\Downloads\\000.docx"));
Map<String, String> map = new HashMap<>();
map.put("{{主职位}}","888");
map.put("{{面貌}}","999");
map.forEach((k,v)->{
FindReplaceOptions options = new FindReplaceOptions();
options.getApplyFont().setColor(Color.BLUE);
try {
doc.getRange().replace(k, v, options);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Downloads\\000.pdf");
doc.save(fileOutputStream, SaveFormat.PDF);
}
效果图:
第一个替换成功,颜色有效,第二个无效
image.png (74.5 KB)