ActiveReports 18 .NET Edition
Developers / Extensibility in ActiveReports / Custom Font Resolver
In This Topic
    Custom Font Resolver
    In This Topic

    You can run reports on different platforms, and it is important to ensure that a report uses the same fonts. With the ActiveReports' custom font resolver, you can configure fonts for Page, RDLX, and Section reports (in the CrossPlatform compatibility mode) on different platforms.

    Note: Custom font settings have higher priority over fonts from the config file or the system font settings.

    To learn about how to configure custom fonts in the ActiveReports.config file, see ActiveReports Configuration File.

    Use the Custom Font Resolver in Preview

    In some situations, you can use the custom fonts resolver through code by specifying the PageReport.FontResolver property, in the Windows Forms Viewer and the JavaScript Viewer as in the following code examples. 

    C# Code for Windows Forms Viewer
    Copy Code
    var report = new PageReport(…);
    report.FontResolver = …;
    viewer.LoadDocument(report.Document);
    
    C# Code for JavaScript Viewer
    Copy Code
    app.UseReportViewer(config => {
      config.FontResolver = ...
      config.UseFileStore(ResourcesRootDirectory)
    }); 
    

    Configure Fonts for Preview and Export

    You can configure fonts for preview and export on all platforms without installation as in the following examples. 

    Example 1

    Copy Code
    public sealed class WindowsFontResolver : GrapeCity.ActiveReports.IFontResolver
    {
       static readonly GrapeCity.Documents.Text.FontCollection _fonts = new GrapeCity.Documents.Text.FontCollection();
       static WindowsFontResolver()
       {
            GrapeCity.Documents.Text.Windows.FontLinkHelper.UpdateFontLinks(_fonts, true);
            _fonts.DefaultFont = _fonts.FindFamilyName("Arial");
       }
       public static GrapeCity.ActiveReports.IFontResolver Instance = new WindowsFontResolver();
       private WindowsFontResolver() { }
       GrapeCity.Documents.Text.FontCollection GrapeCity.ActiveReports.IFontResolver.GetFonts(string familyName, bool isBold, bool isItalic)
       {
            var fonts = new GrapeCity.Documents.Text.FontCollection();
            fonts.Add(_fonts.FindFamilyName(familyName, isBold, isItalic) ?? _fonts.DefaultFont);
            GrapeCity.Documents.Text.Windows.FontLinkHelper.UpdateEudcLinks(fonts);
            return fonts;
       }
    }                        
    

    Example 2

    Copy Code
    public sealed class DirectoryFontResolver : GrapeCity.ActiveReports.IFontResolver
    {
       static readonly GrapeCity.Documents.Text.FontCollection _fonts = new GrapeCity.Documents.Text.FontCollection();
       static DirectoryFontResolver()
       {
          // see https://developers.redhat.com/blog/2018/11/07/dotnet-special-folder-api-linux/
          _fonts.RegisterDirectory(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts));
          _fonts.DefaultFont = _fonts.FindFamilyName("Arial");
       }
       public static GrapeCity.ActiveReports.IFontResolver Instance = new DirectoryFontResolver();
       private DirectoryFontResolver() { }
       GrapeCity.Documents.Text.FontCollection GrapeCity.ActiveReports.IFontResolver.GetFonts(string familyName, bool isBold, bool isItalic)
       {
          var fonts = new GrapeCity.Documents.Text.FontCollection();
          var font = _fonts.FindFamilyName(familyName, isBold, isItalic);
          if (font != null) fonts.Add(font);
          fonts.Add(_fonts.DefaultFont);
          return fonts;
       }
    }                       
    

    See the FontResolver sample for more details.

    Link one Font to Another

    If you need to link just one font to another (for example, link an EUDC font to an installed font), you can do it with DsPdf and DsImaging APIs, using the following code.

    C# Code. PASTE to the beginning of the Main function
    Copy Code
    GrapeCity.Documents.Text.Windows.FontLinkHelper.UpdateFontLinks(null, true);
    var fonts = new System.Collections.Generic.List();
    GrapeCity.Documents.Text.FontCollection.SystemFonts.SelectByFamilyName("MS UI Gothic", fonts);
    if (fonts.Count > 0)
     { 
    
        var eudcFonts = new GrapeCity.Documents.Text.FontCollection();
        using (var stream = new System.IO.FileStream(@"C:\EudcFonts\DFHSG3J.tte", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
        eudcFonts.LoadFonts(stream);
        foreach (var font in fonts)
          { 
    
            font.ClearEudcFontLinks();
            foreach (var eudcFont in eudcFonts) font.AddEudcFont(eudcFont); 
          }
      }        
    
    See Also

    Samples