FontRepository Class
A repository of user-defined fonts.
Namespace: DevExpress.Office
Assembly: DevExpress.Office.v21.2.Core.dll
NuGet Package: DevExpress.Office.Core
Declaration
Remarks
The FontRepository class allows you to use fonts that are not installed on the operating system. Add required fonts to the font repository to eliminate font substitution effects when you display documents with non-standard fonts in the Rich Text Editor and Spreadsheet controls (WinForms/WPF), print these documents, and export these documents to PDF.
Use the FontRepository.Instance static property to access a FontRepository
instance.
Add Fonts to the Repository
Use the FontRepository.AddFont method overloads to add fonts to the font repository. You can load a font from a file, stream, or byte array.
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 recalculations of the document layout, add the required fonts to FontRepository
before you load a document that uses these fonts.
Example 1. Use Custom Fonts in the Word Processing Document API
The code sample below adds custom fonts to FontRepository
and uses these fonts to format document paragraphs. The resulting document is exported to PDF. This example uses the following Google fonts:
using DevExpress.Office;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
namespace ConsoleOfficeApp
{
class Program
{
static void Main(string[] args)
{
FontRepository.Instance.AddFont(@"Fonts\EmilysCandy-Regular.ttf");
FontRepository.Instance.AddFont(@"Fonts\FrederickatheGreat-Regular.ttf");
using (var wordProcessor = new RichEditDocumentServer())
{
Document document = wordProcessor.Document;
// Load a document.
document.LoadDocument(@"Documents\Alice in Wonderland.docx");
// Format the first paragraph, which contains the book title.
var titleFormatting =
document.BeginUpdateCharacters(document.Paragraphs[0].Range);
FormatCharacters(titleFormatting, 20,
"Fredericka the Great", Color.FromArgb(0x2E, 0x74, 0xB5));
document.EndUpdateCharacters(titleFormatting);
// Format the second paragraph, which contains the author's name.
var subtitleFormatting =
document.BeginUpdateCharacters(document.Paragraphs[1].Range);
FormatCharacters(subtitleFormatting, 14,
"Emilys Candy", Color.FromArgb(0x3B, 0x38, 0x38));
document.EndUpdateCharacters(subtitleFormatting);
// Export the document to PDF.
wordProcessor.ExportToPdf(@"Documents\Alice in Wonderland.pdf");
}
FontRepository.Instance.Dispose();
}
private static void FormatCharacters(CharacterProperties formatting,
float size, string fontName, Color color)
{
formatting.FontSize = size;
formatting.FontName = fontName;
formatting.ForeColor = color;
}
}
}
The following image demonstrates the result:
Example 2. Use Custom Fonts in the Spreadsheet Document API
The code sample below adds custom fonts to FontRepository
and uses these fonts to format cells in a workbook. The resulting document is exported to PDF. This example uses the following Google fonts:
using DevExpress.Office;
using DevExpress.Spreadsheet;
namespace ConsoleOfficeApp
{
class Program
{
static void Main(string[] args)
{
FontRepository.Instance.AddFont(@"Fonts\advent-pro.regular.ttf");
FontRepository.Instance.AddFont(@"Fonts\Roboto-Light.ttf");
using (Workbook workbook = new Workbook())
{
// Load a workbook.
workbook.LoadDocument(@"Documents\Sales Report.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);
// Export the document to PDF.
workbook.ExportToPdf(@"Documents\Sales Report.pdf");
}
FontRepository.Instance.Dispose();
}
}
}
The following image demonstrates the result:
Obtain Information About Fonts in the Repository
Use the FontRepository.GetFonts method to return a list of all fonts in the font repository. Each item in this list is a FontData object that contains font information.
The following code sample retrieves the names of all fonts in the repository and displays this information in the console window:
using DevExpress.Office;
using System;
using System.Collections.Generic;
// ...
if (FontRepository.Instance.IsEmpty)
Console.WriteLine("Font repository is empty.");
else
{
Console.WriteLine("Font repository contains the following fonts:");
IList<FontData> fonts = FontRepository.Instance.GetFonts();
for (int i = 0; i < fonts.Count; i++)
{
Console.WriteLine($" \u002A {fonts[i].Name}");
}
}
The following image demonstrates a sample console output:
Clear the Font Repository
Call the FontRepository.Clear method to remove all fonts from the font repository. Use the FontRepository.IsEmpty property to check whether the repository contains fonts.
using DevExpress.Office;
// ...
// Check whether the font repository is empty.
if (!FontRepository.Instance.IsEmpty)
// If false, clear the font repository.
FontRepository.Instance.Clear();