Configuring Fonts for Rendering Spreadsheets

Possible Usage Scenarios

Aspose.Cells APIs provide the facility to render spreadsheets in image formats as well as convert them to PDF & XPS formats. In order to maximize conversion fidelity, it is necessary that the fonts used in the spreadsheet be available in the operating system’s default font directory. In case the required fonts are not present, the Aspose.Cells APIs will try to substitute the required fonts with the ones available.

Selection of Fonts

Below is the process that Aspose.Cells APIs follow behind the scenes.

  1. The API tries to find the fonts on the file system matching the exact font name used in the spreadsheet.
  2. If the API cannot find the fonts with the exact same name, it attempts to use the default font specified under the Workbook’s DefaultStyle.Font property.
  3. If the API cannot locate the font defined under the workbook’s DefaultStyle.Font property, it attempts to use the font specified under PdfSaveOptions.DefaultFont or ImageOrPrintOptions.DefaultFont property.
  4. If the API cannot locate the font defined under PdfSaveOptions.DefaultFont or ImageOrPrintOptions.DefaultFont property, it attempts to use the font specified under FontConfigs.DefaultFontName property.
  5. If the API cannot locate the font defined under FontConfigs.DefaultFontName property, it attempts to select the most suitable fonts from all of the available fonts.
  6. Finally, if the API cannot find any fonts on the file system, it renders the spreadsheet using Arial.

Set Custom Font Folders

Aspose.Cells APIs search the operating system’s default font directory for the required fonts. In case the required fonts are not available in the system’s font directory, the APIs search through custom (user‑defined) directories. The FontConfigs class exposes a number of ways to set custom font directories as detailed below.

  1. FontConfigs.SetFontFolder: This method is useful if there is only one folder to be set.
  2. FontConfigs.SetFontFolders: This method is useful when the fonts reside in multiple folders and the user wishes to set all folders separately rather than combining all fonts in a single folder.
  3. FontConfigs.SetFontSources: This mechanism is useful when the user wishes to load fonts from multiple folders, a single font file, or font data from an array of bytes.

Font Substitution Mechanism

Aspose.Cells APIs also provide the ability to specify a substitute font for rendering purposes. This mechanism is helpful when a required font is not available on the machine where conversion has to take place. Users can provide a list of font names as an alternative to the originally required font. In order to achieve this, the Aspose.Cells APIs expose the FontConfigs.SetFontSubstitutes method, which accepts two parameters. The first parameter is of type string, which should be the name of the font that needs to be substituted. The second parameter is an array of type string; users can provide a list of font names as a substitution for the original font name (specified in the first parameter).

Here is a simple usage scenario.

Information Gathering

In addition to the above‑mentioned methods, the Aspose.Cells APIs also provide means to gather information on what sources and substitutions have been set.

  1. FontConfigs.GetFontSources method returns an array of type FontSourceBase containing the list of specified font sources. If no sources have been set, the method will return an empty array.
  2. FontConfigs.GetFontSubstitutes method accepts a parameter of type string allowing you to specify the font name for which substitution has been set. If no substitution has been set for the specified font name, the method will return null.

Advanced Topics

  • Set Default Font while rendering spreadsheet to images
  • Set DefaultFont property of PdfSaveOptions and ImageOrPrintOptions to have priority
  • Supported Font Formats
  • Worksheet to Image - Set Pixel Format for the Rendered Image