We help IT Professionals succeed at work.
Get Started

Embedded font doesnt display properly in printdocument

abbarnes
abbarnes asked
on
1,332 Views
Last Modified: 2012-05-11
I have some custom fonts that I have embedded in my app.  These work great across all controls, but, my problem arises when I try to use the custom fonts in a printdocument.  The printdocument ends up using the default font, which wont work for what I need.

Here is my sample code, that loads the custom fonts, puts em in a shared font collection to use throughout the app.


            Dim FontNames(3) As String
            FontNames(0) = "LTYPE.TTF"
            FontNames(1) = "LTYPEB.TTF"
            FontNames(2) = "LTYPEBO.TTF"
            FontNames(3) = "LTYPEO.TTF"
            prvFC = Me.GetFont(FontNames)
            Cur.LucidaFonts = prvFC

 Public Function GetFont(ByVal FontResource() As String) As Drawing.Text.PrivateFontCollection
        Try
            'Get the namespace of the application  '  
            Dim NameSpc As String = Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()
            Dim FntStrm As System.IO.Stream
            Dim FntFC As New Drawing.Text.PrivateFontCollection()
            Dim i As Integer
            For i = 0 To FontResource.GetUpperBound(0)
                'Get the resource stream area where the font is located '
                FntStrm = Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(NameSpc + "." + FontResource(i))
                'Load the font off the stream into a byte array '
                Dim ByteStrm(CType(FntStrm.Length, Integer)) As Byte
                FntStrm.Read(ByteStrm, 0, Int(CType(FntStrm.Length, Integer)))
                'Allocate some memory on the global heap '
                Dim FntPtr As IntPtr = Runtime.InteropServices.Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(GetType(Byte)) * ByteStrm.Length)
                'Copy the byte array holding the font into the allocated memory. '
                Runtime.InteropServices.Marshal.Copy(ByteStrm, 0, FntPtr, ByteStrm.Length)
                'Add the font to the PrivateFontCollection '
                FntFC.AddMemoryFont(FntPtr, ByteStrm.Length)
                Dim pcFonts As Int32
                pcFonts = 1
                AddFontMemResourceEx(FntPtr, ByteStrm.Length, 0, pcFonts)
                'Free the memory '
                Runtime.InteropServices.Marshal.FreeHGlobal(FntPtr)
            Next
            Return FntFC
        Catch ex As Exception
            Dim xx As String = ex.Message
            Return Nothing
        End Try
    End Function


 Private Sub PD1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PD1.PrintPage
        Dim Hfnt As New Font(Cur.LucidaFonts.Families(0), 10, FontStyle.Bold)
        e.Graphics.DrawString("Test Font Print", Hfnt, Brushes.Black, x, y)
End Sub

Open in new window

 
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE