Font issue when generating PDF from Word using Aspose.Words for Java

When using aspose.word to convert Word to PDF using doc.save() method using streams instead of local files, the font styling of the word does not match with that of the generated PDF. We are using a low code-no code solution to build the word document which returns a output byte stream which is fed to the aspose API. The word document generated consuming the output stream displays the correct stream. However, the PDF stream generated by Aspose does not seem to render the correct font.

To confirm, we are also running this conversion on our app server which is hosted as a container. Could you please confirm the source of the font directory which aspose uses internally and can the same be achieve programatically to specify an external end point as the source of the font style?

@SaikatP Most likely the problem occurs because the fonts used in the document are not available in the environment where document is rendered to PDF. If Aspose.Words cannot find the fonts used in the document the fonts are substituted. This might lead into the layout differences due to differences in fonts metrics. You can implement IWarningCallback to get a notification when font substitution is performed.
The following articles can be useful for you:
https://docs.aspose.com/words/java/specify-truetype-fonts-location/
https://docs.aspose.com/words/java/install-truetype-fonts-on-linux/

If after providing the required fonts the problem still resists, please attach your input and output documents here for testing. We will check the issue and provide you more information.

Hi @alexey.noskov , thanks for the quick reply. Currently based on the suggestion to use true type fonts, I added logic to use MemoryFontSource to load the different fonts that are needed in the pdf generation. However, after adding the changes, I am seeing the PDF generated to use Famwood font which I believe in the default substitute font. Could you please help me on the below code if I am setting the font source correctly.

byte[] TNRfontBytes = TNRString.getBytes(java.nio.charset.Charset.forName("UTF-8"));
byte[] MingLiufontBytes = MingLiuString.getBytes(java.nio.charset.Charset.forName("UTF-8"));
byte[] KaiufontBytes = KaiuString.getBytes(java.nio.charset.Charset.forName("UTF-8"));

com.aspose.words.MemoryFontSource TNRmemoryFontSource = new com.aspose.words.MemoryFontSource(TNRfontBytes, 1);
com.aspose.words.MemoryFontSource MingLiumemoryFontSource = new com.aspose.words.MemoryFontSource(MingLiufontBytes, 0);
com.aspose.words.MemoryFontSource KaiumemoryFontSource = new com.aspose.words.MemoryFontSource(KaiufontBytes, 0);

com.aspose.words.Document doc = new com.aspose.words.Document(worddocIS);

doc.setFontSettings(new com.aspose.words.FontSettings());
doc.getFontSettings().setFontsSources(new com.aspose.words.FontSourceBase[] { TNRmemoryFontSource, MingLiumemoryFontSource, KaiumemoryFontSource });

com.aspose.words.PdfSaveOptions saveOptions = new com.aspose.words.PdfSaveOptions();
saveOptions.setEmbedFullFonts(true);
doc.save(out, saveOptions);

Note: I cannot attach the documents here in the public post since the content is a bit confidential.

@SaikatP I tested the scenario on my side using the following simple code and it work fine:

byte[] fontBytes = Files.readAllBytes(Paths.get("C:\\temp\\fonts\\calibri.ttf"));
MemoryFontSource mfs = new MemoryFontSource(fontBytes);
FontSettings.getDefaultInstance().setFontsSources(new FontSourceBase[] { mfs });
        
Document doc = new Document("C:\\Temp\\in.docx");
doc.setWarningCallback(new FontSubstitutionWarningCollector());
doc.save("C:\\temp\\out.pdf");

Please make sure TNRfontBytes, MingLiufontBytes and KaiufontBytes contains valid font data.

Hi @alexey.noskov ,

Thanks a lot for your suggestion. It seems the issue was with the byte stream not having the correct font data. Currently the font is getting picked. Thanks for the help.

However, there is a different issue found, where we have added a checkbox (with the tick enabled) in the input word stream but it does not appear in the generated PDF. Based on suggestions, we have also tried to include the windings-regular font but it did not resolve the issue. Could you please help on this. Appreciate a lot.

Note: To confirm the empty checkbox appears fine, the issue is with the check mark not rendering.

Update: @alexey.noskov , please ignore this issue. It was using a different font. Thanks a lot for the guidance on the previous issues.

Regards,
Saikat

@SaikatP It is perfect that you managed to resolve the problem. Please feel free to ask in case of any other issues, we are always glad to help you.