Convert doc to pdf - Font changes to a different one when hosted on Azure App Service - Gil Sans to Serif

Hi,
We are trying convert a word document(docx) to PDF document using Aspose Word library in .NET. We are able to generate PDF document using the code, but it is generating with incorrect font in PDF document.

The word document is using the font 'Gils Sans MT ’ but the output PDF is generated with font '‘Serif " .

Could you help me to resolve this issue?

@suren

To resolve the issue of font substitution when converting a Word document (DOCX) to PDF using the Aspose.Words library in .NET, you can follow these steps:

  1. Ensure Font Availability: Make sure that the font ‘Gil Sans MT’ is installed on the server where your Azure App Service is hosted. If the font is not available, Aspose.Words will substitute it with a default font, which in your case is ‘Serif’.

  2. Set Font Sources: You can specify custom font sources in your application. This allows Aspose.Words to look for fonts in specific directories. Here’s how you can set it up:

    using Aspose.Words;
    using Aspose.Words.Fonts;
    
    // Set the font sources
    FontSettings.DefaultInstance.SetFontsSources(new FontSourceBase[]
    {
        new SystemFontSource(), // System fonts
        new FolderFontSource("C:\\MyFonts\\", true) // Custom folder
    });
    
    // Load your document
    Document doc = new Document("path/to/your/document.docx");
    
    // Save the document as PDF
    doc.Save("path/to/output/document.pdf");
    
  3. Embed Fonts in PDF: If you want to ensure that the fonts are embedded in the PDF, you can use the PdfSaveOptions class to set the embedding options:

    PdfSaveOptions saveOptions = new PdfSaveOptions();
    saveOptions.EmbedFullFonts = true; // Embed all fonts
    
    // Save the document with the specified options
    doc.Save("path/to/output/document.pdf", saveOptions);
    
  4. Check for Font Substitution Warnings: Implement a warning callback to capture any font substitution warnings during the conversion process. This can help you identify if any fonts are being substituted:

    public class HandleDocumentWarnings : IWarningCallback
    {
        public void Warning(WarningInfo info)
        {
            if (info.WarningType == WarningType.FontSubstitution)
            {
                Console.WriteLine("Font substitution: " + info.Description);
            }
        }
    }
    
    // Set the warning callback
    doc.WarningCallback = new HandleDocumentWarnings();
    

By following these steps, you should be able to maintain the original font when converting your Word document to PDF. If the issue persists, consider checking the Azure App Service environment for any restrictions or limitations regarding font installations.

For more detailed information, you can refer to the official documentation on converting documents to PDF using Aspose.Words here.

1 Like

@suren Usually, such problems occur because the fonts used in your input document are not available on the machine where document is converted to PDF. The fonts are required to build document layout. 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/

1 Like

Hi All,

Thank you all for your quick responses. We have finally resolved the issue by applying a workaround.

We simply restructured the old document by opening it in a newer version of Microsoft Word and saving the file without making any changes. After doing this, the issue was fixed.

Thanks again for your support!

Best regards,
Suren Gamage

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