Avatar of abbarnes
abbarnes

asked on 

Embedded font doesnt display properly in printdocument

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

 
Visual Basic.NET

Avatar of undefined
Last Comment
abbarnes
Avatar of x77
x77
Flag of Spain image

http://www.bobpowell.net/embedfonts.htm
Note.: on this article, you can select Vb code or Csharp code (default).
For Vb.net code, it uses Marshal.AllocCoTaskMem(fontStream.Length)
Avatar of abbarnes
abbarnes

ASKER

I tried this option earlier, the code posted works to write on a form, but, does not work in a printdocument/printpreview.  He also fails to call AddFontMemResourceEx which apparently registers the font with the system.  At this point, I am out of ideas, let me know if you have any thoughts.
Avatar of x77
x77
Flag of Spain image

I understand.
Print Preview uses metafiles, but d' nt support embebed fonts.
It knows the Font wken you use DrawString, but it d´nt include the font in metafile or Spool.

http://companionsoftware.com/Support/Resources/WindowsMetafileFAQ.aspx

What are some of the less-than-obvious limitations of Windows metafiles?

Here are a few limitations of metafiles (Windows metafiles (WMF) and/or enhanced metafiles (EMF), as noted) that you might not expect:

Metafiles don't support embedded fonts. This means that if you use a "special" font in a metafile that font won't be displayed on other systems if they don't have that font installed.

ASKER CERTIFIED SOLUTION
Avatar of x77
x77
Flag of Spain image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of abbarnes
abbarnes

ASKER

Wow, great info, thats what I was looking for.

I guess I will look at installing the fonts instead of embedding.  Thanks for your help!!!
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo