Issue with custom fonts converting from word to pdf

We have an issue converting a word document to pdf with a custom font.
I’ve tried several ways to setup the custom fonts, but so far it always failed. here is my code:

  string[] fontFolders = FontsLoader.GetFontFolders();
  Aspose.Words.Document doc = new Aspose.Words.Document(context.SourcePath);
  doc.FontSettings = new FontSettings();
  doc.FontSettings.SetFontsFolders(fontFolders, true);
  doc.WarningCallback = new WarningCallBack();
  Aspose.Pdf.Text.Font originalFont = null;
  try
  {
      originalFont = FontRepository.FindFont("BOLATUK Trial");
  }
  catch (Exception)
  {
      // Font is missing on destination machine
  }
  doc.Save(Path.Combine(context.WorkingDirectory, $"{Guid.NewGuid()}.pdf"));

the fontSources contain the correct folder that has the specific font.

bolatuk.zip (84.1 KB)
BOLATUK.docx (29.4 KB)

I attached the font and the word document containing that font. When I look at the pdf it always has the wrong font and I can also also see, that a substitution happens in the call back.
the fonts are all under C:\Windows\Fonts and are correctly set as sources.

Any pointers to what I am doing are appreciated.

@TimoMartin

It seems that the issue you are experiencing with converting a Word document to PDF using a custom font may be related to the availability of that font on the machine where the conversion is taking place. If Aspose.Words cannot locate the specified font, it will substitute it with a default font, which can lead to mismatches in appearance and layout.

Here are some steps you can take to troubleshoot and resolve the issue:

  1. Ensure Font Availability: Make sure that the custom font “BOLATUK Trial” is installed on the machine where the conversion is being executed. You can check this by looking in the C:\Windows\Fonts directory.

  2. Set Font Folders Correctly: You are already using FontsLoader.GetFontFolders() to set the font folders. Ensure that this method returns the correct path where your custom fonts are located. You can also manually specify the folder containing the custom font using doc.FontSettings.SetFontsFolders(new string[] { "C:\\Path\\To\\Your\\Fonts" }, true);.

  3. Implement Warning Callback: Since you mentioned that you see a substitution happening in the callback, ensure that your WarningCallBack implementation is correctly logging or handling font substitution warnings. This can help you identify when and why the substitution occurs.

  4. Check Font Licensing: If the font requires a license, ensure that you have set the license correctly using Aspose.Font.SetLicense() before performing the conversion.

  5. Refer to Documentation: For more detailed information on how Aspose.Words handles font substitution and how to specify font locations, you can refer to the official documentation on specifying TrueType fonts location and font substitution.

Here is a modified version of your code that includes a manual font folder path:

string[] fontFolders = new string[] { "C:\\Path\\To\\Your\\Fonts" }; // Specify your custom font folder
Aspose.Words.Document doc = new Aspose.Words.Document(context.SourcePath);
doc.FontSettings = new FontSettings();
doc.FontSettings.SetFontsFolders(fontFolders, true);
doc.WarningCallback = new WarningCallBack();

try
{
    Aspose.Pdf.Text.Font originalFont = FontRepository.FindFont("BOLATUK Trial");
}
catch (Exception)
{
    // Handle missing font case
}

doc.Save(Path.Combine(context.WorkingDirectory, $"{Guid.NewGuid()}.pdf"));

By following these steps, you should be able to resolve the font substitution issue during the conversion process. If the problem persists, consider checking the logs from your warning callback for more insights.

After going through the documentation I tried a setup with a font, that can’t have any licensing issues.
Here’s the document with the “Sunset Heavy Narrow” font. I can find the font in the following code, but the substitutation still happens.

Aspose.Words.Document doc = new Aspose.Words.Document(context.SourcePath);
FontSettings fontSettings = new FontSettings();
fontSettings.SetFontsFolder(@"C:\Windows\Fonts", true);
doc.FontSettings = fontSettings;

doc.FontSettings = new FontSettings();
doc.FontSettings.SetFontsFolders(fontFolders, true);
doc.WarningCallback = new WarningCallBack();

fontSettings = new FontSettings();
List<FontSourceBase> fontSources = new List<FontSourceBase>(fontSettings.GetFontsSources());

FolderFontSource folderFontSource = new FolderFontSource(@"C:\Windows\Fonts", true);
fontSources.Add(folderFontSource);

FontSourceBase[] updatedFontSources = fontSources.ToArray();

IList<PhysicalFontInfo> allFonts = updatedFontSources[0].GetAvailableFonts();
PhysicalFontInfo? sunSetFont = allFonts.FirstOrDefault(x => x.FullFontName.Contains("SUNSET", StringComparison.InvariantCultureIgnoreCase));
               
// this is not null
Console.WriteLine("FontFamilyName : " + sunSetFont.FontFamilyName);

doc.Save(Path.Combine(context.WorkingDirectory, $"{Guid.NewGuid()}.pdf"));

SUNSET.docx (29.4 KB)
sunset_heavy.zip (53.2 KB)

I attached the document and fonts again

@TimoMartin

It looks like your inquiry is mainly related to Aspose.Words. We are moving it to respective category where you will be assisted shortly.

@TimoMartin Unfortunately, I cannot reproduce the problem on my side. I used the following simple code for testing:

// `C:\Temp\fonts` contains additional fonts.
FontSettings.DefaultInstance.SetFontsSources(new FontSourceBase[] { new SystemFontSource(), new FolderFontSource(@"C:\Temp\fonts", true) });
Document doc = new Document(@"C:\Temp\in.docx");
doc.WarningCallback = new FontSubstitutionWarningCallback();
doc.Save(@"C:\Temp\out.pdf");
internal class FontSubstitutionWarningCallback : IWarningCallback
{
    public void Warning(WarningInfo info)
    {
        if (info.WarningType == WarningType.FontSubstitution)
            Console.WriteLine(info.Description);
    }
}

Here is the produced output: out.pdf (8.2 KB)

thanks for the reply. I know the issue now, but don’t know why it happens .

  doc.FontSettings = new FontSettings();
  doc.FontSettings.SetFontsFolders(fontFolders, true);

If I remove these 2 lines it works, elsewise I get the substitution warning, which is strange because I thought they should help to find the fonts initially.

@TimoMartin Probably the specified folders are not accessible. Also, setting font folders resets all other font sources specified in FontSettings.

The fontSettings on the document are null before I set it though and the folders are accessible. If the fontSettings are null on the document does this still reset some defaults?

@TimoMartin When Document.FontSettings are null, Aspose.Words uses default font setting, which are accessible through FontSettings.DefaultInstance. Newly created FontSettings instance already contains SystemFontSource. Setting FontsFolders resets all specified font sources.

Then it makes sense, thanks a lot

1 Like