windowsforms-403516-controls-and-libraries-spreadsheet-examples-formatting-how-to-load-and-use-custom-fonts-in-the-spreadsheet-control.md
The WinForms Spreadsheet control ships with the DXFontRepository class that allows you to use fonts that are not installed on the current operating system. When you load a document that uses such fonts, the Spreadsheet substitutes missing fonts with the fonts available on the current machine. The DXFontRepository class allows you to load and use custom fonts in your application to prevent font substitution when documents are displayed, printed, or exported to PDF.
Use the DXFontRepository.Instance static property to access a DXFontRepository instance.
Use the DXFontRepository.AddFont method overloads to add fonts to the font repository. You can load a font from a file, stream, or byte array. Loaded fonts are available to all SpreadsheetControl instances within your project.
Supported font formats include:
TrueType fonts (.TTF)
OpenType fonts that use CFF (Compact Font Format) glyph outlines (.OTF)
OpenType Font Collections (.TTC, .OTC) that contain multiple fonts in a single file
To avoid excessive document layout recalculations, add the required fonts to DXFontRepository before you load a document that uses these fonts.
The code sample below adds the following Google fonts fonts to DXFontRepository and uses these fonts to format cells in a workbook:
using DevExpress.Drawing;
using DevExpress.Spreadsheet;
namespace WinFormsSpreadsheet
{
public partial class Form1 : DevExpress.XtraBars.Ribbon.RibbonForm
{
public Form1()
{
InitializeComponent();
DXFontRepository.Instance.AddFont(@"Fonts\advent-pro.regular.ttf");
DXFontRepository.Instance.AddFont(@"Fonts\Roboto-Light.ttf");
IWorkbook workbook = spreadsheetControl1.Document;
workbook.LoadDocument(@"Documents\SalesReport.xlsx", DocumentFormat.Xlsx);
Worksheet worksheet = workbook.Worksheets["Report"];
// Return the cell that contains the document title.
Cell cell = worksheet.Cells["B2"];
// Specify font settings (font name and size).
cell.Font.Name = "Advent Pro";
cell.Font.Size = 26;
// Return the cell range that contains report data.
CellRange range = worksheet.Range["B4:F20"];
// Start range format update.
var rangeFormatting = range.BeginUpdateFormatting();
// Specify font settings (font name and size).
rangeFormatting.Font.Name = "Roboto Light";
rangeFormatting.Font.Size = 14;
// End range format update.
range.EndUpdateFormatting(rangeFormatting);
}
}
}
Imports DevExpress.Drawing
Imports DevExpress.Spreadsheet
Namespace WinFormsSpreadsheet
Public Class Form1
Inherits DevExpress.XtraBars.Ribbon.RibbonForm
Public Sub New()
InitializeComponent()
DXFontRepository.Instance.AddFont("Fonts\advent-pro.regular.ttf")
DXFontRepository.Instance.AddFont("Fonts\Roboto-Light.ttf")
Dim workbook As IWorkbook = spreadsheetControl1.Document
workbook.LoadDocument("Documents\SalesReport.xlsx", DocumentFormat.Xlsx)
Dim worksheet As Worksheet = workbook.Worksheets("Report")
' Return the cell that contains the document title.
Dim cell As Cell = worksheet.Cells("B2")
' Specify font settings (font name and size).
cell.Font.Name = "Advent Pro"
cell.Font.Size = 26
' Return the cell range that contains report data.
Dim range As CellRange = worksheet.Range("B4:F20")
' Start range format update.
Dim rangeFormatting As Formatting = range.BeginUpdateFormatting()
' Specify font settings (font name and size).
rangeFormatting.Font.Name = "Roboto Light"
rangeFormatting.Font.Size = 14
' End range format update.
range.EndUpdateFormatting(rangeFormatting)
End Sub
End Class
End Namespace
The following image demonstrates the result:
Use the DXFontRepository.GetFonts method to return a list of all fonts in the font repository. Each item in this list is a DXFontData object that contains font information.
Call the DXFontRepository.Clear method to remove all fonts from the font repository. Use the DXFontRepository.IsEmpty property to check whether the repository contains fonts.
using DevExpress.Drawing;
// ...
// Check whether the font repository is empty.
if (!DXFontRepository.Instance.IsEmpty)
// If false, clear the font repository.
DXFontRepository.Instance.Clear();
Imports DevExpress.Drawing
' ...
' Check whether the font repository is empty.
If Not DXFontRepository.Instance.IsEmpty Then
' If false, clear the font repository.
DXFontRepository.Instance.Clear()
End If
Loaded fonts are not saved to a document. These fonts are used to display the document in the control’s UI, print the document, or export the document to PDF.
Loaded fonts are not displayed in the drop-down list of the Spreadsheet control’s font editor.