Skip to main content
A newer version of this page is available.
All docs
V21.2
.NET Framework 4.5.2+

FontRepository.AddFont(String) Method

Loads a font from a file.

Namespace: DevExpress.Office

Assembly: DevExpress.Office.v21.2.Core.dll

NuGet Package: DevExpress.Office.Core

Declaration

public void AddFont(
    string fontFileName
)

Parameters

Name Type Description
fontFileName String

The full path to the font file.

Remarks

The AddFont method supports the following font formats:

  • 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.

Loaded fonts are not saved to the document. They are used to display the document in the Spreadsheet or Rich Text Editor’s UI (WinForms/WPF), print the document, 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 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:

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 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:

Custom Fonts in Spreadsheet Document API

See Also