How to install OpenType (file type: otf) Font in app Start using C #

I need to install the Myriad pro website. otf on the client machine using C #. Please suggest. i tried to install according to the code given in the post

   [DllImport("gdi32", EntryPoint = "AddFontResource")]
    public static extern int AddFontResourceA(string lpFileName);

    [System.Runtime.InteropServices.DllImport("shfolder.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]

    private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder,
    IntPtr hToken, int dwFlags, StringBuilder lpszPath);

    private const int CSIDL_FONTS = 0x0014;
    private const int MAX_PATH = 260;

    // PInvoke to 'register' fonts and broadcast addition
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    private static extern int AddFontResource(string lpszFilename);
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    private static extern int CreateScalableFontResource(uint fdwHidden, string
    lpszFontRes, string lpszFontFile, string lpszCurrentPath);

    private static IntPtr HWND_BROADCAST = new IntPtr(0xffff);
    private const uint WM_FONTCHANGE = 0x001D;

    public Window1()
    {
        InitializeComponent();
        InstallFont();
    }

    internal static void InstallFont()
    {

        string fontsPath = GetFontsPath();

        string ttfFile = System.IO.Path.Combine(fontsPath, "MyriadPro-Semibold.otf");

        System.IO.File.Copy(@"C:\MyriadPro-Semibold.otf", ttfFile);

        int ret;

        if (System.IO.File.Exists(ttfFile))
        {
            //Add font resource
            ret = AddFontResource(ttfFile);
            //Add registry entry so the font is also available next session
            Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
                "MyriadPro-Semibold(TrueType)", "MyriadPro-Semibold.otf", RegistryValueKind.String);
            //Broadcast to let all top-level windows know about change
            ret = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, new IntPtr(0), new IntPtr(0));
        }
    }


    private static string GetFontsPath()
    {
        StringBuilder sb = new StringBuilder(MAX_PATH);
        SHGetFolderPath(IntPtr.Zero, CSIDL_FONTS, IntPtr.Zero, 0, sb);
        return sb.ToString();
    }

    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,IntPtr lParam);
}

      

After executing the code, I can see the registry entry and also copied to the system Fonts folder, but I cannot view the font from the Microsoft office word.

Am I missing anything here?

+2


source to share


1 answer


Where it says

Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
                "MyriadPro-Semibold(TrueType)", "MyriadPro-Semibold.otf", RegistryValueKind.String);

      

If it won't

Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
                "MyriadPro-Semibold(TrueType)", "C:\windows\fonts\MyriadPro-Semibold.otf", RegistryValueKind.String);

      



Where

C:\windows\fonts\

      

is this a custom font folder?

+1


source







All Articles