Fonts

GemBox.Spreadsheet needs access to fonts used in a spreadsheet when printing or saving to PDF, XPS, and image formats. By default, it will check these font locations on supported operating systems:

  • Windows: C:\Windows\Fonts\
  • Linux: /usr/share/fonts/, /usr/local/share/fonts/, and ~/.local/share/fonts/
  • macOS: /System/Library/Fonts/, /Library/Fonts/, and ~/Library/Fonts/

If a specific font is not located at the default location, you need to use the FontSettings static class to specify the location of that font. This is especially needed in environments with no fonts installed, like Docker.

Custom fonts

The following example shows how you can specify the location of a custom font (Almonte Snow.ttf) when saving a spreadsheet to PDF.

using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        // Set the directory path where the component will look for additional font files.
        // The "MyFonts" targets the subdirectory in the current directory, so besides the installed fonts,
        // the component will be able to use the fonts within the specified directory.
        FontSettings.FontsBaseDirectory = "MyFonts";

        var workbook = new ExcelFile();
        var worksheet = workbook.Worksheets.Add("Private Fonts");

        worksheet.Parent.Styles.Normal.Font.Name = "Almonte Snow";
        worksheet.Parent.Styles.Normal.Font.Size = 48 * 20;

        worksheet.Cells[0, 0].Value = "Hello World!";

        workbook.Save("Private Fonts.%OutputFileType%");
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        ' Set the directory path where the component will look for additional font files.
        ' The "MyFonts" targets the subdirectory in the current directory, so besides the installed fonts,
        ' the component will be able to use the fonts within the specified directory.
        FontSettings.FontsBaseDirectory = "MyFonts"

        Dim workbook As New ExcelFile()
        Dim worksheet = workbook.Worksheets.Add("Private Fonts")

        worksheet.Parent.Styles.Normal.Font.Name = "Almonte Snow"
        worksheet.Parent.Styles.Normal.Font.Size = 48 * 20

        worksheet.Cells(0, 0).Value = "Hello World!"

        workbook.Save("Private Fonts.%OutputFileType%")
    End Sub
End Module
Spreadsheet that uses custom font
Screenshot of custom (not installed) font's usage

Assembly fonts

GemBox.Spreadsheet supports retrieving fonts stored as resources inside a local or referenced assembly by specifying the FontSettings.FontsBaseResourceLocation property.

When you add fonts as resources to your application, make sure you're setting the Build Action as an Embedded resource. Or if your application is targeting .NET Framework and thus uses WPF, set it as Resource instead.

Setting font file as embedded resource

The following image shows a structure of an example solution with font files added as embedded resources, and the following table lists base resource locations you need to specify to use those fonts.

Packaging fonts in .NET application
Used fontFontSettings.FontsBaseResourceLocationNotes
Font1.ttf"/"Font resource files are in the root of the local assembly.
Font2.ttf"/MyFonts/"Font resource files are in the subfolder of the local assembly.
Font3.ttf"/MyClassLibrary;component/"Font resource files are in the root of the referenced assembly.
Font4.ttf"/MyClassLibrary;component/MyFonts/"Font resource files are in the subfolder of the referenced assembly.

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy