Export (0) Print
Expand All

How to: Create a Private Font Collection

The PrivateFontCollection class inherits from the FontCollection abstract base class. You can use a PrivateFontCollection object to maintain a set of fonts specifically for your application. A private font collection can include installed system fonts as well as fonts that have not been installed on the computer. To add a font file to a private font collection, call the AddFontFile method of a PrivateFontCollection object.

The Families property of a PrivateFontCollection object contains an array of FontFamily objects.

The number of font families in a private font collection is not necessarily the same as the number of font files that have been added to the collection. For example, suppose you add the files ArialBd.tff, Times.tff, and TimesBd.tff to a collection. There will be three files but only two families in the collection because Times.tff and TimesBd.tff belong to the same family.

The following example adds the following three font files to a PrivateFontCollection object:

  • C:\systemroot\Fonts\Arial.tff (Arial, regular)

  • C:\systemroot\Fonts\CourBI.tff (Courier New, bold italic)

  • C:\systemroot\Fonts\TimesBd.tff (Times New Roman, bold)

The code retrieves an array of FontFamily objects from the Families property of the PrivateFontCollection object.

For each FontFamily object in the collection, the code calls the IsStyleAvailable method to determine whether various styles (regular, bold, italic, bold italic, underline, and strikeout) are available. The arguments passed to the IsStyleAvailable method are members of the FontStyle enumeration.

If a given family/style combination is available, a Font object is constructed using that family and style. The first argument passed to the Font constructor is the font family name (not a FontFamily object as is the case for other variations of the Font constructor). After the Font object is constructed, it is passed to the DrawString method of the Graphics class to display the family name along with the name of the style.

The output of the following code is similar to the output shown in the following illustration.

Fonts Text

Arial.tff (which was added to the private font collection in the following code example) is the font file for the Arial regular style. Note, however, that the program output shows several available styles other than regular for the Arial font family. That is because GDI+ can simulate the bold, italic, and bold italic styles from the regular style. GDI+ can also produce underlines and strikeouts from the regular style.

Similarly, GDI+ can simulate the bold italic style from either the bold style or the italic style. The program output shows that the bold italic style is available for the Times family even though TimesBd.tff (Times New Roman, bold) is the only Times file in the collection.

PointF pointF = new PointF(10, 0);
SolidBrush solidBrush = new SolidBrush(Color.Black);

int count = 0;
string familyName = "";
string familyNameAndStyle;
FontFamily[] fontFamilies;
PrivateFontCollection privateFontCollection = new PrivateFontCollection();

// Add three font files to the private collection.
privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\Arial.ttf");
privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\CourBI.ttf");
privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\TimesBD.ttf");

// Get the array of FontFamily objects.
fontFamilies = privateFontCollection.Families;

// How many objects in the fontFamilies array?
count = fontFamilies.Length;

// Display the name of each font family in the private collection 
// along with the available styles for that font family. 
for (int j = 0; j < count; ++j)
{
    // Get the font family name.
    familyName = fontFamilies[j].Name;

    // Is the regular style available? 
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Regular))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Regular";

        Font regFont = new Font(
           familyName,
           16,
           FontStyle.Regular,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           regFont,
           solidBrush,
           pointF);

        pointF.Y += regFont.Height;
    }

    // Is the bold style available? 
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Bold))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Bold";

        Font boldFont = new Font(
           familyName,
           16,
           FontStyle.Bold,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(familyNameAndStyle, boldFont, solidBrush, pointF);

        pointF.Y += boldFont.Height;
    }
    // Is the italic style available? 
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Italic";

        Font italicFont = new Font(
           familyName,
           16,
           FontStyle.Italic,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           italicFont,
           solidBrush,
           pointF);

        pointF.Y += italicFont.Height;
    }

    // Is the bold italic style available? 
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic) &&
    fontFamilies[j].IsStyleAvailable(FontStyle.Bold))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + "BoldItalic";

        Font italicFont = new Font(
           familyName,
           16,
           FontStyle.Italic | FontStyle.Bold,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           italicFont,
           solidBrush,
           pointF);

        pointF.Y += italicFont.Height;
    }
    // Is the underline style available? 
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Underline))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Underline";

        Font underlineFont = new Font(
           familyName,
           16,
           FontStyle.Underline,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           underlineFont,
           solidBrush,
           pointF);

        pointF.Y += underlineFont.Height;
    }

    // Is the strikeout style available? 
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Strikeout))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Strikeout";

        Font strikeFont = new Font(
           familyName,
           16,
           FontStyle.Strikeout,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           strikeFont,
           solidBrush,
           pointF);

        pointF.Y += strikeFont.Height;
    }

    // Separate the families with white space.
    pointF.Y += 10;

} // for

The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of PaintEventHandler.

Show:
© 2014 Microsoft