(Print this page)

Embedding a font into an application
Published date: Tuesday, October 30, 2007
On: Moer and Éric Moreau's web site

Have you ever needed to distribute a font with one of your application and got blocked to install it into the Fonts folder? Or do you have a private font that you want to use in your application without having to other to be able to use it elsewhere?

The solution: embed it into your application. Sounds easy? It is easy if you have the right set of instructions. Read on.

I had to do it a long time ago (in the VB6 era) for a barcode font and had many problems. If only I knew that trick!


I first need to give full credits to the author of the complex part of the code required to package the font and to load it.

The author is Michael S. Kaplan, a Technical Lead in the International Fundamentals team at Microsoft, and the code to create the class available here is available from his blog.

I have transformed the sample he created into a class library so that I can use it from within any application. His code is in C# and I didn’t rewrite it to VB. Personally, I do prefer VB but I have nothing against C#. Don’t tell anybody but I even have some projects in C#!

I have contacted Michael to ensure I could re-use his class and he required me to warn you about font license:

Warning: do not violate the license for any font file from Microsoft or any other source. You can use the licensing information in the Font Properties Extension to find out if you are allowed to do it!

The process

The steps we will do here for the demonstration are:

  • Install the font
  • Add the project that handles the binary file
  • Create something in the application to package the font as a binary file
  • Uninstall the font
  • Create something in the application to load the font from the binary file

Installing a special font

There are a lot of free fonts available on the web. I agree that using fancy fonts in an accounting application is probably not the best idea but sometime a more “serious” font is required. In the introduction, I already talked about a barcode font.

For the demo purpose, I will be using a free barcode font. This font is the “Free 3 of 9”, a very standard barcode font available from the Square Gear Productions website.

Now that you have downloaded the font, you need to install it. Some fonts come with an installer. The one I have provided you the link does not. The simplest way to install it is to unzip the font and to copy it into the Fonts folder found in your Windows folder or from the Control Panel.

Creating our solution

To be able to test this scenario, I have created a VB Windows application. To that solution, I have added the project containing the class library from Michael Kaplan.

Figure 1: The solution Explorer

Finally, in my Windows application project, I have added a reference to the class library to be able to use it like shown in figure 2.

Figure 2: Adding the reference to the other project

Packaging a font into a binary file (.bin)

Thanks to Michael, the hardest part is done! The next thing we need to do now is to package the font into a binary file.

If you look at the btnCreateFontFile_Click event handler, only 3 lines of code are required as you can see here (only 1 if you don’t want to count the declaration and the disposition):

Dim objEF As New FontEmbedding.cFont
objEF.CreateFontFile(lblSelectedFont.Text, "MyEmbeddedFont.bin")
objEF = Nothing
If you download the demo application, you will discover that I am using the standard font dialog box to let the user pick a font from the list of installed fonts. The name of the selected font is then sent to the Text property of a Label control (lblSelectedFont).

If you need more details on the standard dialogs, you can return to an article I have written in June 2003 .

The CreateFontFile method loads the font in memory, stream it and save it into a file with the name you have provided in the second argument.

Uninstalling the font

To be really sure that our solution is working, you should uninstall the font. Reopen the font folder, select the font you just installed and delete it.

Loading the font from the binary file

It is now time to test loading the font from the binary file.

Once again, it is really not complex. Have a look at the btnLoadFont_Click event handler from the demo application. A couple lines of code and you are done:

Dim objEF As New FontEmbedding.cFont
txtTest.Text = "12345"
txtTest.Font = objEF.CreateFontFromFile(txtTest.Handle, _
                                        "Fake name!!!", _
objEF = Nothing
If you followed the steps correctly (or downloaded the demo application), you now see the textbox at the bottom of the form displaying 12345 using a barcode font.

Figure 3: The demo running


Thanks again to Michael Kaplan for having published the blog and the C# code. With this new class/assembly into your toolkit, you can now skip the installation of a font in your deployment.

I hope you appreciated the topic and see you next month.

(Print this page)