Hello support team,
I am trying to save docx document as pdf and that docx document uses fonts which I don’t have installed on my machine.
Docx document which I am trying to save as pdf:
FONT TEST.DOCX (9.2 KB)
Pdf I am getting as result (see that fonts are different. Pdf uses default fonts):
11690011-146.pdf (24.5 KB)
I saw several topics with similar questions but all answers I found for those questions didn’t worked for me. Last what I tried was:
private void SaveAsPdf(Words.Document document)
{
Words.Saving.PdfSaveOptions options = new Words.Saving.PdfSaveOptions();
options.FontEmbeddingMode = PdfFontEmbeddingMode.EmbedAll;
options.EmbedFullFonts = true;
document.Save(pdfFileName, options);
}
and I got above attached pdf document.
If I embed fonts directly in docx document by going to File -> Options -> Save tab -> Check embed fonts in file, everything will work fine. Example document which works and has Check embed fonts in file checked:
Font Test - Embeded Fonts.docx (23.4 KB)
I am using two versions of Aspose - 16.7 and 23.2 and on both versions I am getting same results. Could you please help me to solve this problem?
@lkalinic The problem occurs because the fonts used in your input document are not available on the machine where document is converted to PDF. If Aspose.Words cannot find the font used in the document, the font is substituted. This might lead into fonts mismatch and document layout differences due to the different fonts metrics. You can implement IWarningCallback to get notifications when font substitution is performed.
Please see our documentation to learn where Aspose.Words looks for fonts:
https://docs.aspose.com/words/net/specifying-truetype-fonts-location/
If fonts are embedded into the source document, Aspose.Words uses the embedded fonts for rendering.
Hey @alexey.noskov
Thank you for your answer!
I also tried to install fonts on my local machine which I am using in my document but I am again getting same pdf with default fonts. If I understand correctly, Aspose will look for fonts in (C:\Windows\Fonts) folder and my font is there but it still not being rendered in my final pdf document. You can try too, here is the font:
pacifico.zip (51.8 KB)
I understand that part of the code I attached will embed fonts directly in my document but obviously this is not working in that way or I do not understand documentation correctly.
@lkalinic Unfortunately, I cannot reproduce the problem on my side. I have put your font into a separate folder and specified this folders as an additional font source. Here is my test code:
Document doc = new Document(@"C:\Temp\in.docx");
doc.FontSettings = new FontSettings();
// "C:\Temp\fonts\" folder contains Pacifico.ttf font file.
doc.FontSettings.SetFontsSources(new FontSourceBase[] { new SystemFontSource(), new FolderFontSource(@"C:\Temp\fonts\", true) });
doc.Save(@"C:\Temp\out.pdf");
Here is output PDF produced on my side using this code:
out.pdf (12.6 KB)
@alexey.noskov I implemented your solution and it works, thank you! I have one more question, which font types are supported by Aspose? I found out that it works with .ttf and .otf. Do we have anything else besides those two types?
Regards,
Luka