For the proper rendering of Word document to PDF, XPS, HTML Fixed or Image formats by using Aspose.Words for Java, you need to install the required fonts (which are used in source Word document) on the machine. Please refer to the following sections of documentation:
If you don’t want to install the fonts, then you can simply copy the latest versions of required font files into a separate folder and try running the following code of latest 21.4 version of Aspose.Words for Java:
Document doc = new Document("input.docx");
FontSettings fontSettings = new FontSettings();
addFontFolder(fontSettings, myDir + "CustomFonts/");
doc.setFontSettings(fontSettings);
doc.save("output.pdf");
this is i know but I found that some fonts are not available on the machine when converting word to pdf, but aspose uses fonts that I don’t know, but I clearly specify the default font
If instead of ‘PingFang SC’, you want Aspose.Words for Java to replace ‘宋体’ font with ‘Times New Roman’ or any other font, then please use the following code:
Document doc = new Document("C:\\Temp\\input.docx");
FontSettings fs = new FontSettings();
fs.getSubstitutionSettings().getTableSubstitution().addSubstitutes("宋体", new String[]{"Times New Roman"});
doc.setFontSettings(fs);
doc.save("c:\\temp\\awjava-21.4.pdf");
But fonts have many formats, such as Heiti SC Light and Heiti SC. I wonder if it can be replaced by regular expressions. All the beginning of Heiti SC is replaced with Arial.
Document doc = new Document("C:\\Temp\\input.docx");
FontSettings fs = new FontSettings();
ArrayList listOfFonts = new ArrayList();
// Collect complete Font Names that start with Heiti
FontInfoCollection fonts = doc.getFontInfos();
for (int i = 0; i < fonts.getCount(); i++)
if (fonts.get(i).getName().startsWith("Heiti"))
listOfFonts.add(fonts.get(i).getName());
// Add Font Substitutions
for (int i = 0; i < listOfFonts.size(); i++)
fs.getSubstitutionSettings().getTableSubstitution().addSubstitutes(
listOfFonts.get(i).toString(),
new String[]{"Arial"});
doc.setFontSettings(fs);
doc.save("c:\\temp\\awjava-21.4.pdf");