Configuring Fonts for Rendering Spreadsheets
Possible Usage Scenarios
Aspose.Cells APIs provide the facility to render the spreadsheets in image formats as well as convert them to PDF & XPS formats. In order to maximize the conversion fidelity, it is necessary that the fonts used in the spreadsheet should be available in the operating system’s default font directory. In case the required fonts are not present then 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 scene.
- The API tries to find the fonts on the file system matching the exact font name used in the spreadsheet.
- If 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.
- If 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.
- If API cannot locate the font defined under PdfSaveOptions.DefaultFont or ImageOrPrintOptions.DefaultFont property, it attempts to use the font specified under FontConfigs.DefaultFontName property.
- If API cannot locate the font defined under FontConfigs.DefaultFontName property, it attempts to select the most suitable fonts from all of the available fonts.
- Finally, if 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 then the APIs search through the custom (user defined) directories. The FontConfigs class has exposed a number of ways to set custom font directories as detailed below.
- FontConfigs.setFontFolder: This method is useful if there is only one folder to be set.
- 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.
- FontConfigs.setFontSources: This mechanism is useful when the user wishes to load fonts from multiple folders or a single font file or font data from an array of bytes.
Font Substitution Mechanism
Aspose.Cells APIs also provide the ability to specify the 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 have exposed the FontConfigs.setFontSubstitutes method which accepts 2 parameters. The first parameter is of type String, which should be the name of the font which needs to be substituted. The second parameter is an array of type String. Users can provide a list of font names as substitutes to the original font (specified in the first parameter).
Here is a simple usage scenario.
//Substituting the Arial font with Times New Roman & Calibri
FontConfigs.setFontSubstitutes("Arial", new String[] { "Times New Roman", "Calibri" });
Information Gathering
In addition to above-mentioned methods, the Aspose.Cells APIs have also provided means to gather information on what sources and substitutions have been set.
- FontConfigs.getFontSources: This method returns an array of type FontSourceBase containing the list of specified font sources. In case, no sources have been set, the FontConfigs.getFontSources method will return an empty array.
- FontConfigs.getFontSubstitutes: This method accepts a parameter of type String allowing to specify the font name for which substitution has been set. In case, no substitution has been set for the specified font name then the FontConfigs.getFontSubstitutes method will return null.