How to determine the O/S filename of an installed font?

后端 未结 1 357
长发绾君心
长发绾君心 2021-01-21 23:55

We use a third-party PDF Generator library which requires that you specify the TrueType or Type1 file name when using a font other than the 14 or so that are part of the default

相关标签:
1条回答
  • 2021-01-22 00:19

    If you don't mind poking around in the registry, take a look at

    HKLM\Software\Microsoft\Windows NT\CurrentVersion\Fonts
    

    It has pairs like

    Name = "Arial (TrueType)"
    Data = "arial.ttf"
    

    You can do this the necessary queries like this:

    static RegistryKey fontsKey =
        Registry.LocalMachine.OpenSubKey(
            @"Software\Microsoft\Windows NT\CurrentVersion\Fonts");
    
    static public string GetFontFile(string fontName)
    {
        return fontsKey.GetValue(fontName, string.Empty) as string;
    }
    

    A call to GetFontFile("Arial (TrueType)") returns "arial.ttf"

    You could of course modify the code to append the (TrueType) portion to the fontName, or to look through everything returned by fontsKey.GetValueNames() to find the best match.

    0 讨论(0)
提交回复
热议问题