954,170 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Installing a font programmatically on web server

Hi,

I am developing an ASP .NET website that needs to create images using a custom font. Obviously, to do this, I need the font installed on the web server. As I am using a hosting site, and I have no direct access to the server the site will be hosted on, I need a script that will install the font on the server.

I have found the following solution online that uses the Windows API and theoretically should install the font:

...
using System.Runtime.InteropServices;
...
public partial class InstallFont : System.Web.UI.Page
{
    [DllImport("gdi32", EntryPoint = "AddFontResource")]
    public static extern int AddFontResourceA(string lpFileName);

   protected void Page_Load(object sender, EventArgs e)
   {
        ...
        int result = -1;

        result = AddFontResourceA(@"C:\ExampleFont.TTF");
        Response.Write((result == 0) ? "Font is already installed." : 
        "Font installed successfully.");
   }
}


This runs like a charm and tells me that the fonthas been installed, however, when I try to create a Font object with the font that was supposedly just installed, it does not work.

I am sure that AddFontResource is working though because when I check Microsoft Word (for testing purposes) the font shows up in its list of fonts present on the machine.

Does anyone know why .NET's font object is not recognizing the font when it has been installed and is available to other programs? It may be possible that I might have to add a registry value, but I know virtually nothing about doing that.

Any help is greatly, greatly appreciated.

drspock
Newbie Poster
13 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Do you have to register the font? I have seen code snippets before that use unregistered fonts.

// Be sure to dispose your PrivateFontCollection
// and Font to avoid an easy leak!
System.Drawing.Text.PrivateFontCollection privateFonts = new PrivateFontCollection();
privateFonts.AddFontFile("mycustomfont.ttf");
System.Drawing.Font font = new Font(privateFonts.Families[0], 12);
this.label1.Font = font;


Borrowed from here

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

Thank you so much! It works perfectly!

I can't believe the solution was so simple after I spent hours scouring the internet!

If only MSDN had mentioned this in their article about the Font class rather than saying that it is required that the font be installed on the computer.

drspock
Newbie Poster
13 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

You're welcome :)

Please mark this thread as solved if you have found a solution to your question and good luck!

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You