Aspose.Words use embeded font from docx to generate pdf

Hi,

This is a question for the Aspose.Words product (not Aspose.Words.Cloud) but I didn’t have the option to just choose Aspose.Words from the dropdown.

I have a dotx file with some embedded font in it and it gets lost when I generate the pdf.
Is Aspose.Words able to use the embedded font in order to generate the pdf file or do I have to search for another solution ?

Thanks

@bogandy Aspose.Words supports embedded fonts and they should be used while generating PDF. Could you please attach your document here for testing? We will check the issue and provide you more information. Also, please specify which version of Aspose.Words you use for testing.

test.docx (276.2 KB)
Hi

I added the test document (test.docx)
I use aspose words version 20.4

Code I use is:

Document doc = new Document("test.docx");

FontSubstitutionWarningCollector callback = new FontSubstitutionWarningCollector();
doc.setWarningCallback(callback);

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setEmbedFullFonts(true);
saveOptions.setFontEmbeddingMode(PdfFontEmbeddingMode.EMBED_ALL);
doc.save("test.pdf", saveOptions);

callback.fontSubstitutionWarnings.forEach( warningInfo -> System.out.println(warningInfo.getDescription())); 

and

private static class FontSubstitutionWarningCollector implements IWarningCallback {
	public void warning(WarningInfo info) {
		if (info.getWarningType() == WarningType.FONT_SUBSTITUTION)
			fontSubstitutionWarnings.warning(info);
	}

	public WarningInfoCollection fontSubstitutionWarnings = new WarningInfoCollection();
}

The resulting pdf is:
test.pdf (1.5 MB)
As you can see if you open up fonts from acrobat reader, the Lato font is not there
test.pdf.png (29.4 KB)

Furthermore, the warning I get from the callback say this

Font 'Lato' has not been found. Using 'Calibri' font instead. Reason: alternative name from document.
Font 'Roboto Slab' has not been found. Using 'Times New Roman' font instead. Reason: alternative name from document.
Font 'Lato Light' has not been found. Using 'Calibri Light' font instead. Reason: alternative name from document.

Thanks for the help

@bogandy Thank you for additional information. I have investigated your document as as I can see the only embedded font in your document is Calibri. Here is fonttable.xml fragment from your document:

	<w:font w:name="Lato">
		<w:altName w:val="Calibri"/>
		<w:charset w:val="00"/>
		<w:family w:val="swiss"/>
		<w:pitch w:val="variable"/>
		<w:sig w:usb0="00000001" w:usb1="5000604B" w:usb2="00000000" w:usb3="00000000" w:csb0="00000093" w:csb1="00000000"/>
	</w:font>
	<w:font w:name="Calibri">
		<w:panose1 w:val="020F0502020204030204"/>
		<w:charset w:val="00"/>
		<w:family w:val="swiss"/>
		<w:pitch w:val="variable"/>
		<w:sig w:usb0="E4002EFF" w:usb1="C000247B" w:usb2="00000009" w:usb3="00000000" w:csb0="000001FF" w:csb1="00000000"/>
		<w:embedRegular r:id="rId1" w:subsetted="1" w:fontKey="{FB505F94-3A4D-460E-B32C-6B05402C4037}"/>
	</w:font>
	<w:font w:name="Lato Light">
		<w:altName w:val="Calibri Light"/>
		<w:charset w:val="00"/>
		<w:family w:val="swiss"/>
		<w:pitch w:val="variable"/>
		<w:sig w:usb0="00000001" w:usb1="5000604B" w:usb2="00000000" w:usb3="00000000" w:csb0="00000093" w:csb1="00000000"/>
	</w:font>
	<w:font w:name="Roboto Slab">
		<w:altName w:val="Times New Roman"/>
		<w:charset w:val="00"/>
		<w:family w:val="auto"/>
		<w:pitch w:val="variable"/>
		<w:sig w:usb0="00000001" w:usb1="5000205B" w:usb2="00000020" w:usb3="00000000" w:csb0="0000019F" w:csb1="00000000"/>
	</w:font>

Lato and Roboto fonts are Microsoft cloud fonts. Aspose.Words does not support cloud fonts and does not use cloud fonts downloaded by MS Office by default.
To get the expected result you have two options:

  1. Embed all required fonts into your MS Word document. Here is your document with all the fonts embedded and the result PDF produced by Aspose.Words. in_allfonts.docx (2.7 MB) out.pdf (32.4 KB)

  2. Another option is to provide Aspos.Words with the required fonts, by specifying their location. On my side cloud fonts are downloaded into the following folder: C:\Users\alexe\AppData\Local\Microsoft\FontCache\4\CloudFonts. If specify this folder as font source Aspose.Words uses Lato font:

doc.setFontSettings(new FontSettings());
doc.getFontSettings().setFontsSources(
       new FontSourceBase[] {
                new SystemFontSource(1),
                new FolderFontSource("C:\\Users\\alexe\\AppData\\Local\\Microsoft\\FontCache\\4\\CloudFonts", true, 2)});

Awesome

I didn’t think to check the fontconfig.xml file.
I embedded the fonts correctly now and it works.
Thank you!

I knew about option 2 but it’s more complicated for us to implement.
In fact, it’s how I test to make sure that aspose doesn’t find any fonts:

doc.getFontSettings().getDefaultInstance().setFontsSources(
new FontSourceBase[] { new FolderFontSource("fakeFontFolder", false) });

@bogandy You can also check if the particular font is embedded into the document using FontInfo. getEmbeddedFont method. It returns null if the font is not embedded.
in addition, see Document.getFontInfos property to get font infos read from from the document.

Very helpful.
Thank you!

I was wondering how I can extract the font from the word document and save it to a file.
Now I have the answer.

Thanks for all your help

@bogandy Please feel to ask in case of any issues. We are always glad to help you.