xtrareports-404270-feature-guide-to-devexpress-reports-use-custom-fonts.md
DevExpress Reporting tools allow you to use custom fonts in your reports. The following font formats are available:
Variable fonts (VF) are not supported.
You can install the required font on your machine or server to use in a report and the End-User Report Designer. Follow the instructions below for your operating system.
Download font files. If font files are zipped, right-click the .zip folder and then click Extract to unzip them. If you are prompted to allow the program to make changes to your computer, and if you trust the source of the font, click Yes. Re-login to complete the installation.
Note
Windows Server OS installs fonts based on the user. Make sure the font is installed for all users or for the user who runs the application.
Install the fontconfig library to manage fonts. Copy the font file in the fonts directory and then refresh the font cache. In the Linux docker file, you can use the usr/local/share/fonts/ directory, and the following commands:
COPY ["FontsFolderNameHere/MyFontFileName.ttf", "usr/local/share/fonts/"]
RUN apt-get -y install fontconfig
RUN fc-cache -vf
Different Linux distributions use slightly different commands:
sudo apt-get -y install fontconfig
cp <font name> ~/.local/share/fonts
fc-cache -vfkq
apt-get -y install libfontconfig1
sudo cp <font name> /usr/local/share/fonts/example/
fc-cache -fv
apk add fontconfig
cp <font name> /usr/share/fonts/
fc-cache -f
You can install the ttf-mscorefonts-installer package to add Microsoft TrueType core fonts (Arial, Times New Roman, Courier New, and so on) to your system. Run the following terminal command to install this package and accept the license agreement:
sudo apt-get install ttf-mscorefonts-installer
sudo apt install ttf-mscorefonts-installer -y
apk --no-cache add msttcorefonts-installer
You can also use the SkiaSharp library to use fonts without fontconfig installation. Add the SkiaSharp.NativeAssets.Linux.NoDependencies package to your project. Copy font files to the /usr/share/fonts folder. The Skia library automatically uses fonts from this folder.
You can use the DevExpress.Drawing.DXFontRepository class to register custom fonts without installation. This approach is available on Windows and Linux operating systems.
The following code snippet registers a font distributed with an application and uses this font in a report style.
In this example, the customFontStyle is the report style created in the report and added to the StyleSheet collection. The style is applied to the XRLabel control in the report. Although you can use the XRControl.Font property to set the control’s font directly, applying styles is common practice. For more information on report styles, review the following help topic: Report Visual Styles.
View Example: How to use a custom font distributed with the application
using DevExpress.Drawing;
using System;
protected void Application_Start(object sender, EventArgs e) {
DXFontRepository.Instance.AddFont(@"Fonts\MissFajardose-Regular.ttf");
DevExpress.XtraReports.Web.ASPxWebDocumentViewer.StaticInitialize();
}
Imports DevExpress.Drawing
Imports System
Protected Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
DXFontRepository.Instance.AddFont("Fonts\MissFajardose-Regular.ttf")
DevExpress.XtraReports.Web.ASPxWebDocumentViewer.StaticInitialize()
End Sub
using DevExpress.Drawing;
public SampleReport()
{
InitializeComponent();
customFontStyle.Font = new DXFont("Miss Fajardose", 48F, DXFontStyle.Regular, DXGraphicsUnit.Point);
}
Imports DevExpress.Drawing
Public Sub New()
InitializeComponent()
customFontStyle.Font = New DXFont("Miss Fajardose", 48F, DXFontStyle.Regular, DXGraphicsUnit.Point)
End Sub
You can use custom fonts in End-User Report Designer for Web. Call the updateFont method with a specified key-value pair where key is the font name and value is its display name.
The following code snippet registers a custom font in the Web Report Designer’s client-side Init event.
<dx:ASPxReportDesigner ID="ASPxReportDesigner1" runat="server">
<ClientSideEvents Init="function(s){ s.designerModel.updateFont({'Miss Fajardose': 'Miss Fajardose'}); delete DevExpress.JS.Widgets.availableFonts['Arial']; } " />
</dx:ASPxReportDesigner>
The following example detects and loads custom fonts used within a report. The application obtains a list of all fonts used in the report and loads them from the Google Fonts resource, when available. Loaded fonts are added to the DevExpress.Drawing.DXFontRepository automatically and made available without additional installation requirements.
View Example: Reporting for ASP.NET Core — Detect and Load Custom Fonts with Ease (DXFontRepository)
View Example: Reporting for WPF — How to Use a Custom Font Distributed with the Application
Your report design and layout may rely on a font type that is not available in the application’s hosting environment. As a result, the font cannot be installed on the client machine, in a Docker image, in Azure, or in another host/container. In such cases, your report replaces unavailable fonts with default fonts, which may alter the appearance of report pages from the original design.
The DevExpress Reporting suite helps you ensure that a report uses the correct fonts regardless of the hosting environment. A report will notify you about missing typefaces so that you can obtain the required font data. Once you obtain these fonts, add them to your report’s DXFontRepository, thus making them available to report controls.
The DXFontRepository.QueryNotFoundFont event fires for every unavailable font type. Handle this event to do the following:
This implementation ensures that DXFontRepository contains all required font types before document generation begins.
The following code sample contains simple code that registers the preloaded “Sankofa Display” font in case it is unavailable in the application environment:
private static void Report_QueryNotFoundFont(object sender, NotFoundFontEventArgs e) {
if (e.RequestedFont == "Sankofa Display") {
string font = Environment.CurrentDirectory + "\\Data\\SankofaDisplay-Regular.ttf";
e.FontFileData = File.ReadAllBytes(font);
}
}
Private Shared Sub Report_QueryNotFoundFont(ByVal sender As Object, ByVal e As NotFoundFontEventArgs)
If e.RequestedFont = "Sankofa Display" Then
Dim font As String = Environment.CurrentDirectory & "\Data\SankofaDisplay-Regular.ttf"
e.FontFileData = File.ReadAllBytes(font)
End If
End Sub
You can use the DXFontRepository.QueryNotFoundFont event to identify missing fonts and download them from a hosting service. The following example implements a service that asynchronously downloads missing fonts from Google Fonts and adds them to DXFontRepository.
Use your personal Google API Key to run this example. For instructions on how to obtain your key, see Google Fonts Developer API.
Assign your API Key to the apiKey variable in the FontCollectorService.cs file before you launch the example.
Review license agreements associated with fonts that you use. Use and redistribution permissions may vary. The service used in this example (Google Fonts) hosts fonts that are open source and available at no cost. Review the following page for details: Google Fonts FAQ.
The report in this example contains a few fonts that may be missing in many hosting environments: Ga Maamli, Roboto, and Nerko One. The example obtains these fonts if missing and makes them available to report controls. When exported to PDF, the report uses the original fonts: