Skip to main content
All docs
V24.1

DXFontRepository Class

A repository of user-defined fonts.

Namespace: DevExpress.Drawing

Assembly: DevExpress.Drawing.v24.1.dll

NuGet Package: DevExpress.Drawing

Declaration

public class DXFontRepository :
    IDisposable

The following members return DXFontRepository objects:

Remarks

The DXFontRepository 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. The class is available for the following products:

  • Word Processing Document API
  • Spreadsheet Document API
  • PDF Document API (used in grahpics content only)
  • Reporting
  • Charts
  • Rich Text Editor and Spreadsheet controls (WinForms/WPF)

Use the FontRepository.Instance static property to access a DXFontRepository instance.

Loaded fonts are not saved to the document - they are used to display, print, or export Word and Spreadsheet documents. Custom fonts are applied to the report when it is exported to PDF or an image from code.

Add Fonts to the Repository

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.

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 layout recalculations, add the fonts to DXFontRepository before you load a document, report, or chart that uses these fonts.

The code sample below adds custom fonts to DXFontRepository. This example uses the following Google fonts:

using DevExpress.Drawing;
//...
DXFontRepository.Instance.AddFont(@"Fonts\EmilysCandy-Regular.ttf");
DXFontRepository.Instance.AddFont(@"Fonts\FrederickatheGreat-Regular.ttf");
// you usage code here
//...

Use Fonts in Word Processing Document API

The code sample belows uses fonts added to the repository to format the document content. The resulting document is exported to PDF.

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");
}

private static void FormatCharacters(CharacterProperties formatting,
float size, string fontName, Color color)
{
  formatting.FontSize = size;
  formatting.FontName = fontName;
  formatting.ForeColor = color;
}

The image below illustrates the result.

word processing custom fonts

Use Fonts in Spreadsheet Document API

The code sample below adds custom fonts to DXFontRepository 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.Drawing;
using DevExpress.Spreadsheet;

namespace ConsoleOfficeApp {
    class Program {
        static void Main(string[] args) {
            DXFontRepository.Instance.AddFont(@"Fonts\advent-pro.regular.ttf");
            DXFontRepository.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");
            }
        }
    }
}

The image below illustrates the result.

spreadsheet custom fonts

Use Fonts in Reporting

The code sample below adds custom fonts to DXFontRepository and uses these fonts to format report labels. The resulting report is exported to PDF. This example uses the following Google fonts:

using DevExpress.Drawing;
//...
    public XtraReport1()
    {
        InitializeComponent();
        DXFont labelFont  = new DXFont("Advent Pro", 26);
        DXFont cellFont = new DXFont("Roboto", 14);

        xrLabel4.Font = labelFont;
        xrTableCell4.Font = cellFont;
        xrTableCell7.Font= cellFont;
        xrTableCell10.Font = cellFont;
        xrTableCell13.Font= cellFont;

        XtraReport1 report = new XtraReport1();
        report.ExportToPdf("result.pdf");

    }

The image below demonstrates the result.

reporting result

Note

Custom fonts are not displayed in the report preview.

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 DXFontData 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.Drawing;
using System;
using System.Collections.Generic;
// ...

if (DXFontRepository.Instance.IsEmpty)
    Console.WriteLine("Font repository is empty.");
else
{
    Console.WriteLine("Font repository contains the following fonts:");
    IList<DXFontData> fonts = DXFontRepository.Instance.GetFonts();
    for (int i = 0; i < fonts.Count; i++)
    {
        Console.WriteLine($"  \u002A {fonts[i].Name}");
    }
}

The following image demonstrates a sample console output:

Console Output

Clear the Font Repository

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();

Inheritance

Object
DXFontRepository
See Also