We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Font Substitution for Word to PDF conversion

I am trying to convert word doc to PDF. I referred below link-
https://docs.aspose.com/words/java/manipulate-and-substitute-truetype-fonts/

So I tried this code-

destinationFilePath = processFileNamesForExtensionReplace(filePath, "pdf");

FontSettings fontSettings = new FontSettings();
TableSubstitutionRule substitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();

substitutionRule.addSubstitutes("Times New Roman", new String[] { "Algerian" });
substitutionRule.addSubstitutes("Arial", new String[] { "Algerian" });
substitutionRule.addSubstitutes("Courier New", new String[] { "Algerian" });

LoadOptions lo = new LoadOptions();
lo.setFontSettings(fontSettings);

Document doc = new Document(filePath, lo);

doc.save(destinationFilePath, SaveFormat.PDF);

This is not reflecting newly assigned font in converted PDF file.

@bagadeprasanna As I can see table substitution works properly. I used this code for testing:

FontSettings fontSettings = new FontSettings();
fontSettings.setFontsFolder("C:\\Temp\\fonts", true);
TableSubstitutionRule substitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();

substitutionRule.addSubstitutes("Times New Roman", new String[] { "Algerian" });
substitutionRule.addSubstitutes("Arial", new String[] { "Algerian" });
substitutionRule.addSubstitutes("Courier New", new String[] { "Algerian" });

LoadOptions lo = new LoadOptions();
lo.setFontSettings(fontSettings);

Document doc = new Document("C:\\Temp\\in.docx", lo);
doc.setWarningCallback(new FontSubstitutionWarningCollector());
doc.save("C:\\Temp\\out.pdf", SaveFormat.PDF);
private static class FontSubstitutionWarningCollector implements IWarningCallback {
    public void warning(WarningInfo info) {
        if (info.getWarningType() == WarningType.FONT_SUBSTITUTION)
            System.out.println(info.getDescription());
    }
}

This code shows the following warnings on my side:

Font 'Times New Roman' has not been found. Using 'Algerian' font instead. Reason: table substitution.
Font 'Arial' has not been found. Using 'Algerian' font instead. Reason: table substitution.
Font 'Courier New' has not been found. Using 'Algerian' font instead. Reason: table substitution.

Note, the specified fonts folder (fontSettings.setFontsFolder("C:\\Temp\\fonts", true);) does not contain Times New Roman, Arial and Courier New fonts. Substitution is only performed if Aspose.Words cannot find the font specified in the document.

@alexey.noskov-
I am trying to do it on Linux.
Earlier I used below code and it used to work with aspose-words-18.8-jdk16.jar-

destinationFilePath = processFileNamesForExtensionReplace(filePath,"pdf");
Document doc = new Document(filePath);
DocumentBuilder builder = new DocumentBuilder(doc);

HandleFontsWarnings substitutionWarningHandler = new HandleFontsWarnings();
doc.setWarningCallback(substitutionWarningHandler);

LocalDateTime myObj = LocalDateTime.now();

FontSettings fontSettings = new FontSettings();
fontSettings.setEnableFontSubstitution(true);

fontSettings.setFontSubstitutes("Times New Roman", "Liberation Serif");
fontSettings.setFontSubstitutes("Arial", "Liberation Sans");
fontSettings.setFontSubstitutes("Courier New", "Liberation Mono");
doc.setFontSettings(fontSettings);
doc.save(destinationFilePath,SaveFormat.PDF);

Now I am using aspose-words-22.1.0-jdk17.jar so I had to discard this code as I could not use setEnableFontSubstitution and setFontSubstitutes with new jar. So I tried the code you have mentioned in previous reply. It is not producing exactly the same result as this code did for me earlier. What can I do to make this code work with aspose-words-22.1.0-jdk17.jar? I want to always go for font substitution.

@bagadeprasanna Could you please attach your source, the current output and expected output documents? We will check the documents and provide you more information.
In addition, please provide the output of your HandleFontsWarnings. Does Apose.Words not perform the font substitution on your side?
Also, lease clarify whether you would like to perform font substitution even if the font specified in the source document is available.

@alexey.noskov- I am attaching a zip file containing all the necessary files. **The executions are done on RHEL OSArchive.zip (97.4 KB)
**
Test 1-
If you run wordToPDF1 with aspose-words-18.8-jdk16.jar then I get wordToPDF1.pdf which is the output I want.
Test 2-
If you run wordToPDF1 with aspose-words-22.1.0-jdk17.jar then I get output in wordToPDF2.pdf which is different than wordToPDF1.pdf.

Below are the messages from command line(these are not substitutions from my code I do not know why they are not taking effect)-

Font 'Calibri' has not been found. Using 'Liberation Sans' font instead. Reason: table substitution.
Font 'Times New Roman' has not been found. Using 'FreeSerif' font instead. Reason: table substitution.
Font 'Courier New' has not been found. Using 'FreeMono' font instead. Reason: table substitution.
Font 'Arial' has not been found. Using 'Liberation Sans' font instead. Reason: table substitution.

I want to have output like wordToPDF1.pdf using aspose-words-22.1.0-jdk17.jar. What modifications do I need to do in wordToPDF2.java?
I want to perform font substitution even if the font specified in the source document is available.

@bagadeprasanna The problem occurs because substitution table is not empty. Here is how it looks like for Linux:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<TableSubstitutionSettings xmlns="Aspose.Words">
	<SubstitutesTable>
		<Item OriginalFont="Arial" SubstituteFonts="Liberation Sans, FreeSans, Garuda, DejaVu Sans" />
		<Item OriginalFont="Charcoal" SubstituteFonts="FreeSerif" />
		<Item OriginalFont="Comic Sans MS" SubstituteFonts="DejaVu Sans" />
		<Item OriginalFont="Courier New" SubstituteFonts="FreeMono, Liberation Mono, DejaVu Sans Mono" />
		<Item OriginalFont="Georgia" SubstituteFonts="Norasi, Liberation Serif, FreeSerif, DejaVu Serif" />
		<Item OriginalFont="Helvetica" SubstituteFonts="FreeSans" />
		<Item OriginalFont="Lucida Grande" SubstituteFonts="FreeMono, Liberation Mono, DejaVu Sans" />
		<Item OriginalFont="Lucida Sans Unicode" SubstituteFonts="FreeMono, Liberation Mono, DejaVu Sans" />
		<Item OriginalFont="Lucida Console" SubstituteFonts="FreeMono, Liberation Mono, DejaVu Sans Mono" />
		<Item OriginalFont="New York" SubstituteFonts="DejaVu Serif" />
		<Item OriginalFont="Tahoma" SubstituteFonts="DejaVu Sans, Kalimati" />
		<Item OriginalFont="Times New Roman" SubstituteFonts="FreeSerif, Liberation Serif, DejaVu Serif" />
		<Item OriginalFont="Palatino Linotype" SubstituteFonts="FreeSerif" />
		<Item OriginalFont="Verdana" SubstituteFonts="DejaVu Sans Mono" />
		<Item OriginalFont="Trebuchet MS" SubstituteFonts="Liberation Sans, FreeSans, Garuda, DejaVu Sans" />
		<Item OriginalFont="Impact" SubstituteFonts="Rekha, DejaVu Sans" />
		<Item OriginalFont="Arabic Transparent" SubstituteFonts="KacstArt" />
		<Item OriginalFont="Arial Baltic" SubstituteFonts="Liberation Sans, FreeSans, Garuda, DejaVu Sans" />
		<Item OriginalFont="Arial CE" SubstituteFonts="Liberation Sans, FreeSans, Garuda, DejaVu Sans" />
		<Item OriginalFont="Arial Cyr" SubstituteFonts="Liberation Sans, FreeSans, Garuda, DejaVu Sans" />
		<Item OriginalFont="Arial Greek" SubstituteFonts="Liberation Sans, FreeSans, Garuda, DejaVu Sans" />
		<Item OriginalFont="Arial TUR" SubstituteFonts="Liberation Sans, FreeSans, Garuda, DejaVu Sans" />
		<Item OriginalFont="Courier New Baltic" SubstituteFonts="FreeMono, Liberation Mono, DejaVu Sans Mono" />
		<Item OriginalFont="Courier New CE" SubstituteFonts="FreeMono, Liberation Mono, DejaVu Sans Mono" />
		<Item OriginalFont="Courier New Cyr" SubstituteFonts="FreeMono, Liberation Mono, DejaVu Sans Mono" />
		<Item OriginalFont="Courier New Greek" SubstituteFonts="FreeMono, Liberation Mono, DejaVu Sans Mono" />
		<Item OriginalFont="Courier New TUR" SubstituteFonts="FreeMono, Liberation Mono, DejaVu Sans Mono" />
		<Item OriginalFont="Courier" SubstituteFonts="FreeMono, Liberation Mono, DejaVu Sans Mono" />
		<Item OriginalFont="Tahoma Armenian" SubstituteFonts="DejaVu Sans" />
		<Item OriginalFont="Times" SubstituteFonts="FreeSerif, Liberation Serif, DejaVu Serif" />
		<Item OriginalFont="Times New Roman Baltic" SubstituteFonts="FreeSerif, Liberation Serif, DejaVu Serif" />
		<Item OriginalFont="Times New Roman CE" SubstituteFonts="FreeSerif, Liberation Serif, DejaVu Serif" />
		<Item OriginalFont="Times New Roman Cyr" SubstituteFonts="FreeSerif, Liberation Serif, DejaVu Serif" />
		<Item OriginalFont="Times New Roman Greek" SubstituteFonts="FreeSerif, Liberation Serif, DejaVu Serif" />
		<Item OriginalFont="Times New Roman TUR" SubstituteFonts="FreeSerif, Liberation Serif, DejaVu Serif" />
		<Item OriginalFont="Microsoft Sans Serif" SubstituteFonts="DejaVu Sans" />
		<Item OriginalFont="MS UI Gothic" SubstituteFonts="TakaoPGothic" />
		<Item OriginalFont="PMingLiU-ExtB" SubstituteFonts="FreeSerif" />
		<Item OriginalFont="Cambria Math" SubstituteFonts="FreeSerif Italic" />
		<Item OriginalFont="Calibri" SubstituteFonts="Liberation Sans" />
		<Item OriginalFont="MS PGothic" SubstituteFonts="TakaoPGothic" />
		<Item OriginalFont="Arial Unicode MS" SubstituteFonts="TakaoPGothic" />
		<Item OriginalFont="Microsoft YaHei" SubstituteFonts="MSungGB18030C-Medium" />
	</SubstitutesTable>
</TableSubstitutionSettings>

You can get this XML using the following code:

TableSubstitutionRule substitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();
substitutionRule.save("/temp/out.xml");

You can edit this XML file by adding your substitution rules and them load it using TableSubstitutionRule.load method.