Back to Devexpress

How to: Load and Use Custom Fonts in the Rich Text Editor

wpf-403520-controls-and-libraries-rich-text-editor-examples-formatting-how-to-load-and-use-custom-fonts-in-the-rich-text-editor.md

latest6.9 KB
Original Source

How to: Load and Use Custom Fonts in the Rich Text Editor

  • Jun 21, 2023
  • 3 minutes to read

The WPF Rich Text Editor 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 Rich Text Editor 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.

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. Loaded fonts are available to all RichEditControl 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 document paragraphs:

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

namespace WpfRichTextEditor
{
    public partial class MainWindow : DevExpress.Xpf.Core.ThemedWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            DXFontRepository.Instance.AddFont(@"Fonts\EmilysCandy-Regular.ttf");
            DXFontRepository.Instance.AddFont(@"Fonts\FrederickatheGreat-Regular.ttf");

            Document document = richEditControl1.Document;
            // Load a document.
            document.LoadDocument(@"Documents\AliceInWonderland.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);
        }

        private static void FormatCharacters(CharacterProperties formatting,
            float size, string fontName, Color color)
        {
            formatting.FontSize = size;
            formatting.FontName = fontName;
            formatting.ForeColor = color;
        }
    }
}
vb
Imports DevExpress.Drawing
Imports DevExpress.XtraRichEdit.API.Native
Imports System.Drawing

Public Class MainWindow
    Inherits DevExpress.Xpf.Core.ThemedWindow

    Public Sub New()
        InitializeComponent()
        DXFontRepository.Instance.AddFont("Fonts\EmilysCandy-Regular.ttf")
        DXFontRepository.Instance.AddFont("Fonts\FrederickatheGreat-Regular.ttf")

        Dim document As Document = richEditControl1.Document
        ' Load a document.
        document.LoadDocument("Documents\AliceInWonderland.docx")

        ' Format the first paragraph, which contains the book title. 
        Dim titleFormatting As CharacterProperties = 
          document.BeginUpdateCharacters(document.Paragraphs(0).Range)
        FormatCharacters(titleFormatting, 20, "Fredericka the Great", 
                        Color.FromArgb(&H2E, &H74, &HB5))
        document.EndUpdateCharacters(titleFormatting)

        ' Format the second paragraph, which contains the author's name. 
        Dim subtitleFormatting As CharacterProperties = 
          document.BeginUpdateCharacters(document.Paragraphs(1).Range)
        FormatCharacters(subtitleFormatting, 14, "Emilys Candy", 
                        Color.FromArgb(&H3B, &H38, &H38))
        document.EndUpdateCharacters(subtitleFormatting)
    End Sub

    Private Shared Sub FormatCharacters(ByVal formatting As CharacterProperties, ByVal size As Single,
                                        ByVal fontName As String, ByVal color As Color)
        formatting.FontSize = size
        formatting.FontName = fontName
        formatting.ForeColor = color
    End Sub
End Class

The following image demonstrates the result:

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.

Clear the Font Repository

Call the DXFontRepository.Clear method to remove all fonts from the font repository. Use the FontRepository.IsEmpty property to check whether the repository contains fonts.

csharp
using DevExpress.Drawing;
// ...

// Check whether the font repository is empty.
if (!DXFontRepository.Instance.IsEmpty)
    // If false, clear the font repository.
    DXFontRepository.Instance.Clear();
vb
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

Limitations

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

  2. Loaded fonts are not displayed in the drop-down list of the Rich Text Editor’s font editor.