Is it possible to set a 'FallbackFont'

Is it possible to set a default fallback font when converting to PDF?

I am aware of the DefaultFont property on certain LoadOptions classes.

My use case that is that I have “try anything to convert” method which does not using any LoadOptions and the document is using Calibri font which is not available on a Linux container, so I’d like this to just fallback to Arial or similar.

This is the error I get, which makes perfect sense.

Can’t create PDF font with name = Calibri → PDF: Font Calibri was not found

@rthomas95

You can define a fallback (default) font for the conversion by using FontSettings and assigning it to the LoadOptions.FontSettings property before loading the document. The FontSettings class provides a setDefaultFont (Java) / DefaultFont (C#) property that specifies the font to use when the original font is missing [1][4].

Example (Java)

// Create FontSettings and set the fallback font
com.aspose.words.FontSettings fontSettings = new com.aspose.words.FontSettings();
fontSettings.setDefaultFont("Arial");

// Apply the FontSettings to LoadOptions
com.aspose.words.LoadOptions loadOptions = new com.aspose.words.LoadOptions();
loadOptions.setFontSettings(fontSettings);

// Load the document with the options
com.aspose.words.Document doc = new com.aspose.words.Document("input.docx", loadOptions);

// Save as PDF
doc.save("output.pdf");

Example (C#)

// Create FontSettings and set the fallback font
var fontSettings = new Aspose.Words.FontSettings();
fontSettings.DefaultFont = "Arial";

// Apply the FontSettings to LoadOptions
var loadOptions = new Aspose.Words.LoadOptions
{
    FontSettings = fontSettings
};

// Load the document with the options
var doc = new Aspose.Words.Document("input.docx", loadOptions);

// Save as PDF
doc.Save("output.pdf");

By configuring the DefaultFont you ensure that any missing fonts (e.g., Calibri on a Linux container) are substituted with the specified fallback font during the PDF conversion. If you need further assistance, let us know the Aspose product/version and language you are using.

Hello @rthomas95 ,

Please disregard the previous response from our bot. Unfortunately, at the moment our library does not provide a way to set a default font for all input formats without using format-specific load options, as font handling differs between formats due to the use of different third-party libraries during the conversion process.

After discussing this with our development team, they suggested trying an alternative approach for setting a default font by using IFontSubstituteLoadOptions. Below, I have provided a code example demonstrating how this can be used.

var possibleConversions = Converter.GetPossibleConversions("docx");
var loadOptions = possibleConversions.LoadOptions;
var fontSubstituteLoadOptions = loadOptions as IFontSubstituteLoadOptions;
if (fontSubstituteLoadOptions!=null)
{
    fontSubstituteLoadOptions.DefaultFont = "Arial";
}
using (Converter converter = new Converter("input.docx", (LoadContext loadContext) => loadOptions))
{
    var convertOptions = new PdfConvertOptions();
    converter.Convert("output.pdf", convertOptions);
}

As you can see from our API, some load options that expose a DefaultFont property implement the IFontSubstituteLoadOptions interface, which allows you to define a default font. In this case, you only need to replace the document type in GetPossibleConversions().

Please note that this approach will not work for all input formats; however, it should be helpful for the most commonly used ones. If this approach does not fully meet your requirements, we would appreciate it if you could describe your use case in more detail and share your expectations regarding how you would like this property to be implemented in our library.

1 Like

Thank you for this. This works for the vast majority of use cases, only type it doesn’t work on in Publisher (.pub) but this is very a rare format these days so I’m not too worried about it.

Thank you!

Hello @rthomas95 ,

You’re very welcome. I have passed your appreciation on to our developers, and they have also agreed to investigate the possibility of configuring a default font for the .pub format. If this turns out to be feasible, we will certainly consider adding it in future versions, so please keep an eye on our release notes.

This is a lifesaver for those of us running GroupDocs in Docker containers! Missing fonts on Linux is such a common headache. The IFontSubstituteLoadOptions approach is much cleaner than manually installing every MS font into the container.

1 Like

Hello @EleenD09 ,

Thank you for the feedback! We’re glad to hear that this approach helped you avoid missing font issues on Linux when running in Docker. Please feel free to let us know if you have any questions or additional scenarios you’d like us to review.