Office File API & Office-Inspired Desktop UI Controls - Use Custom Fonts without Installation on the System (v21.2)

Office-Inspired Products
20 January 2022

In our most recent major release (v21.2), we added custom font support to our Spreadsheet and Word Processing product lines. This new feature applies to the following products/libraries:

You can now load and apply custom fonts to avoid font substitution effects when you display, print, or export (to PDF) documents (when/if a document includes fonts not installed on the target operating system, in Azure App Service, or a Docker container). With this new feature, you can be certain that your documents will look as intended no matter which fonts are available on a user’s computer.

Custom fonts are stored in FontRepository. The FontRepository.AddFont method overloads allow you to load necessary fonts from files, streams, and byte arrays. We recommend that you add fonts to FontRepository before you import a document using these fonts (otherwise, the document’s layout will be recalculated each time a new custom font is loaded).

Loaded fonts are available to all instances of the DevExpress Spreadsheet and Word Processing components within a project (WinForms-WPF-Office File API).

You can load fonts in the following 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 help you apply this new feature in your next project, we created a simple example that uses the Rye Google font (the document is exported to PDF):

This example uses our Word Processing Document API library to load a Word document, format document content, and export the result to PDF.

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

// ...
// Load custom font.
FontRepository.Instance.AddFont(@"Fonts\Rye-Regular.ttf");

using (var wordProcessor = new RichEditDocumentServer()) {
Document document = wordProcessor.Document;
// Load document.
document.LoadDocument(@"Documents\HansInLuck.docx");

// Format first paragraph. 
var titleFormatting =
    document.BeginUpdateCharacters(document.Paragraphs[0].Range);
titleFormatting.FontSize = 30;
titleFormatting.FontName = "Rye";
titleFormatting.ForeColor = Color.FromArgb(0x94, 0x36, 0x34);
document.EndUpdateCharacters(titleFormatting);

// Save document as PDF.
wordProcessor.ExportToPdf(@"Documents\HansInLuck.pdf");
}

The FontRepository class ships with the GetFonts method. This method allows you to obtain information (such as font name and style) about all fonts in the repository. Call the Clear method to clear the font repository.

Limitations

Our custom font loading engine has the following limitations:

  • Loaded fonts are not embedded in your Word or Excel document. These fonts are used when you display the document in the Spreadsheet or Rich Text Editor’s UI, print, or save the document as PDF.
  • Loaded fonts are not displayed in the drop-down list of the Spreadsheet or Rich Text Editor’s font editor.

Your Feedback Matters

We’d love to know what you think of custom font support within our Office-inspired product line. Please share your thoughts in the comment section below or submit a support ticket via the DevExpress Support Center.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.