FontsRepository.RegisterFont data Parameter

2 posts, 0 answers
  1. Mi
    Mi avatar
    16 posts
    Member since:
    Apr 2011

    Posted 02 Mar 2015 Link to this post

    The help is little bit scarce on registering a font: see

    There is a data parameter at the end, which should contain the raw data for the font. Looking for more information about the subject I came across where 

    public class FontData {
      public bool IsValid { get; set; }
      public byte[] Bytes { get; set; }
      public string FontFamilyName { get; set; }
      public bool IsItalic { get; set; }
      public bool IsBold { get; set; }

    is defined. Which is used from to get the Bytes property of the above class to register a font:

    PdfFontsServiceFontData result = e.Result;
    FontFamily fontFamily = new FontFamily(result.FontFamilyName); FontStyle fontStyle = GetFontStyle(result.IsItalic); FontWeight fontWeight = GetFontWeight(result.IsBold);
    FontsRepository.RegisterFont(fontFamily, fontStyle, fontWeight, result.Bytes);

    So is this the right way to create the font data:

    FontFamily fontFamily = new FontFamily(fontFamilyName);
    FontStyle fontStyle = isItalic ? FontStyles.Italic : FontStyles.Normal;
    FontWeight fontWeight = isBold ? FontWeights.Bold : FontWeights.Normal;
    Typeface typeface = new Typeface(fontFamily, fontStyle, fontWeight, FontStretches.Normal);
    GlyphTypeface glyphTypeface;
    if (typeface.TryGetGlyphTypeface(out glyphTypeface)) {
       using (var memoryStream = new MemoryStream()) {
           data.Bytes = memoryStream.ToArray();
           data.IsValid = true;



  2. Kammen
    Kammen avatar
    313 posts

    Posted 04 Mar 2015 Link to this post


    Thank you for your feedback. We will consider some improvements to the mentioned article. Here is some additional information on the matter.

    1. In WPF you don't need to register a font, unless the font is not installed on the machine. The FontsRepository can access all fonts that are installed and register them for you.

    2. If you want to use a font that is not installed on the machine, then you should register it. Every font can be described by 3 components: font family - the family of the font; font style - the font style (normal, italic, etc.); the font weight - the weight of the font (normal, bold, narrow, etc.). These components are the key that later is used to create an instance of this font. For example if you have arialitalic.ttf font file, that contains the italic version of the Arial font, and want to register it in your application, your code should look like this:
    FontsRepository.RegisterFont(new FontFamily("Arial"), FontStyles.Italic, FontWeights.Normal, File.ReadAllBytes("arialitalic.ttf"));

    And you can use it later in your application by calling the following method:
    FontsRepository.TryCreateFont(new FontFamily("Arial"), FontStyles.Italic, FontWeights.Normal, out font)

    Hope this makes things clearer. If you have other questions do not hesitate to contact us.


    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

Back to Top