PrivateFontCollections and PrintPreview

January 11th, 2006 by Bryan Kinkel

I am having a heck of a time getting a Printing.PrintDocument and a print preview control to use fonts in a private font collection. I’ve got a simple form that contains a textbox (for the TTF file path), a label and a Print Preview control.

On load, the form adds the TrueType file to the Private Font Collection. A font, scoped to the form class is created from the PFC and assigned to the label. The label, as expected, redraws itself using the new font. Finally, a PrintDocument is assigned to the printpreview control. The PrintPage event renders some text using the new font. (screenshot is below).

privatefontcollection.gif

However, what is displayed in the print preview control looks like MS Sans Serif text. I’ve tried a variety of standard TrueType fonts, stored in a c:\dev\fonts folder, and I can’t get any of them to display in the print preview control.

Any thoughts?

My code generally looks like this:


Public Class Form1Private WithEvents oDoc As New System.Drawing.Printing.PrintDocument
Private oPFC As New PrivateFontCollection
Private oFontToDraw As Font
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'full path to the font to install
    'c:\dev\fonts\cookiehollow.ttf
   
    Dim sFontFile As String = Me.txtFile.Text
   
   
'add the font file to the collection
    oPFC.AddFontFile(sFontFile)
   
    'create the font to draw from the first entry in the private font collection
    oFontToDraw = New Font(oPFC.Families(0), 36, FontStyle.Regular)
    
   
   
'assign the font to a label - the label should draw itself in the new font
    Me.Label1.Font = oFontToDraw
    'assign the print document to the print preview control
    Me.printpreview.Document = Me.oDoc
End Sub
Private Sub oDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles oDoc.PrintPage
    Dim sSampleString As String = "The quick brown fox jumped over the lazy dogs."
    e.Graphics.DrawString(sSampleString, oFontToDraw, System.Drawing.Brushes.Black, 0, 0)
End Sub
End Class

Posted in .NET Framework 2.0, Programming | No Comments »

New development blog

January 11th, 2006 by Bryan Kinkel

This is a new blog. The purpose is to document various projects at Lose Your Mind Development.

Posted in General | 1 Comment »