Skip to main content
All docs
V23.2

Load and Use Custom Fonts Without Installation on the System

  • 6 minutes to read

The Word Processing Document API and Spreadsheet Document API ship 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 RichEditDocumentServer and Workbook components substitute 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 printed or exported to PDF.

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

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

Note

Loaded fonts are not saved to the document. These fonts are used to print or export the document to PDF.

Example 1. Use Custom Fonts in the Word Processing Document API

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

using DevExpress.Drawing;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;

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

Custom Fonts in Word Processing Document API

Example 2. Use Custom Fonts in the 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");
            }

            DXFontRepository.Instance.Dispose();
        }
    }
}

The following image demonstrates the result:

Custom Fonts in Spreadsheet Document API

Obtain Information About Fonts in the Repository

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.

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