Visual Studio Developer Center > Visual Basic Forums > Visual Basic General > VB.net Printing with both texts and images.

Answered VB.net Printing with both texts and images.

  • Friday, December 07, 2007 1:30 AM
     
     
    Hi! I'm new to VB.net programming on printing a page to a thermal printer (Epson T88IV). I really had a hard time figuring out how to code the printing of a bitmap image and some text, while sending commands (through font control) to the printer.
    I tried sending the escape characters with Control font, but the Graphics.DrawString of PrintPageEventArgs do not allow it.. so I used the RawPrinterHelper class I found on Microsoft KB. It worked out but, I cannot print a bitmap image with it.
    Anybody knows how to do that? I will be very grateful and appreciate it if you can show me. Thanks.

     

Answers

  • Tuesday, December 11, 2007 10:52 AM
     
     Answered

    Hi e.d,

     

    Please check this code sample.

    Clicking the Button1 will print a .bmp file and a .txt file on the same page.

    Code Block

    Public Class Form1

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog

            Dim PrintDocument1 As Printing.PrintDocument = New Printing.PrintDocument

            PrintPreviewDialog1.Document = PrintDocument1

            PrintPreviewDialog1.ShowDialog()

            AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintDocument1_PrintPage

            PrintDocument1.Print()

        End Sub

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            'RichTextBox1.LoadFile("D:\Text.txt", RichTextBoxStreamType.PlainText)

            'PictureBox1.Image = Image.FromFile("E:\VBproject\Gap.bmp")

        End Sub

     

        Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

           

          Dim newImage As Image = Image.FromFile("E:\VBproject\Gap.bmp")

          e.Graphics.DrawImage(newImage, 100, 100)

          ' You also can reference an image to PictureBox1.Image.

     

            Dim txtReader As System.IO.StreamReader = New System.IO.StreamReader("D:\Text.txt")

            Dim textOfFile As String = txtReader.ReadToEnd

            txtReader.Close()

            e.Graphics.DrawString(textOfFile, New Font("Arial", 16), Brushes.Black, 100, 500)

            ' You also can reference some text to RichTextBox1.Text, etc.

     

        End Sub

    End Class

    Trackback: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2372449&SiteID=1

     

     

    Regards,

    Martin

  • Friday, February 01, 2008 3:34 AM
     
     Answered
    im already done with this problem, and found the workaround a few days after my last post above.
    i just remembered now to post the workaround so that those who might have encountered the same problem would know they can try this, too.

    on the printer preferences of the epson's thermal printers (i dont know yet with their other printers), select the "printer settings" tab to find "true type font substitution", then select "replace" radio button, click advanced settings button. from the list of fonts, select a font and replace it with a device font from the dropdown box. click ok twice.
    then on your code, so you can use the device font and printer commands, create a font object with the true type font's name that you have substituted to a device font.
    for barcode fonts to work, you need to set the correct font size of the chosen barcode.

    that's it. thanks.

All Replies

  • Tuesday, December 11, 2007 10:52 AM
     
     Answered

    Hi e.d,

     

    Please check this code sample.

    Clicking the Button1 will print a .bmp file and a .txt file on the same page.

    Code Block

    Public Class Form1

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog

            Dim PrintDocument1 As Printing.PrintDocument = New Printing.PrintDocument

            PrintPreviewDialog1.Document = PrintDocument1

            PrintPreviewDialog1.ShowDialog()

            AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintDocument1_PrintPage

            PrintDocument1.Print()

        End Sub

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            'RichTextBox1.LoadFile("D:\Text.txt", RichTextBoxStreamType.PlainText)

            'PictureBox1.Image = Image.FromFile("E:\VBproject\Gap.bmp")

        End Sub

     

        Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

           

          Dim newImage As Image = Image.FromFile("E:\VBproject\Gap.bmp")

          e.Graphics.DrawImage(newImage, 100, 100)

          ' You also can reference an image to PictureBox1.Image.

     

            Dim txtReader As System.IO.StreamReader = New System.IO.StreamReader("D:\Text.txt")

            Dim textOfFile As String = txtReader.ReadToEnd

            txtReader.Close()

            e.Graphics.DrawString(textOfFile, New Font("Arial", 16), Brushes.Black, 100, 500)

            ' You also can reference some text to RichTextBox1.Text, etc.

     

        End Sub

    End Class

    Trackback: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2372449&SiteID=1

     

     

    Regards,

    Martin

  • Wednesday, December 12, 2007 1:35 AM
     
     
    Hi Martin,

    Thanks for your effort.
    I learned new things from your reply. Although I managed to find a way to print images and texts already before your post, thank you still for replying.

    dotNet's printing is somehow difficult, I say. Especially that it won't support device fonts (Control, Barcodes fonts) anymore. That is what's bothering me now. I hope there will be somebody in here who can help me or share their gotchas to us about sending/printing device fonts on a .net code.


    Thank you,
    e.d
  • Thursday, December 13, 2007 1:33 PM
     
     

    Hi e.d,

     

    Generally, to add a font, just put the font file into the folder "C:\Windows\Font". Therefore Windows can access the font.

     

    However, Barcodes are not created by a font. The encoding of the information in a barcode as well as the many different standards doesn’t allow this. You need a control.

     

    Here is a Free .NET Barcode Forms Control DLL 6.03 for you to try:

    http://cn.mysoftoday.com/free_net_barcode_forms_p35262.html

    It creates bar-codes in Visual Studio and the .NET Framework. Includes Visual Basic .NET example and a manual with code for VB and C# for printing or saving images to a file. Supports Code 39 and Extended Code 39.

    Here is the screenshot:

      

    References:

    1.    How to add a FONT?
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=844348&SiteID=1

     

    2.    Print Barcodes with the Active X print control
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1315912&SiteID=1

     

     

    Best wishes,

    Martin

  • Friday, December 14, 2007 1:44 AM
     
     
    Hi Martin,

    That's how easy I thought it could be.. but this is really different. Epson's thermal printers (some other printers, too, i think) have specific fonts that we can use to send commands to say, cut the paper, and barcode fonts to print barcodes.

    This can be easily done using VB 6. In .Net, it became difficult since .Net now only supports TrueType and OpenType fonts. So in the case of device fonts (like Control, ControlA, Barcodes), .Net will just replace these with a default font like "Microsoft Sans Serif" when you try to use them in your code with a PrintDocument.

    I found on the internet just few discussions on this matter, and they only provide vague ideas.

    To give you code snippets, here's how we can do it in VB 6:

    1. For sending commands to the printer to feed a rollpaper and then cut it:

        Printer.FontSize = 10
        Printer.Font = "Control"
        Printer.Print = "P"

    2. For printing a barcode:

        Printer.FontSize = 54.5        'The size of the barcode
        Printer.Font = "Barcode5"    'In Epson TM printers, this is an "ITF" barcode
        Printer.Font "12345"        'Sample data for barcode


    To send printer commands in a .Net code, we need to use the RawPrinterHelper class from the Microsoft KB (http://support.microsoft.com/kb/322090). Then, ESC or "escape commands" are used instead of their character counterparts. Just like the "P" in the code above, its equivalent is "1BJ01DV0".

    To use the printer's Barcode fonts in .Net, that's what I don't know how. With Epson's TM printers, it has Barcode1 to Barcode8 fonts. Using a free barcode control is not an option for me. Epson's manuals also give codes in VB6 only.

    Hope there will be somebody out there who will share a light..


    Martin, man, thanks for the help. I really appreciate your effort.
  • Friday, February 01, 2008 3:34 AM
     
     Answered
    im already done with this problem, and found the workaround a few days after my last post above.
    i just remembered now to post the workaround so that those who might have encountered the same problem would know they can try this, too.

    on the printer preferences of the epson's thermal printers (i dont know yet with their other printers), select the "printer settings" tab to find "true type font substitution", then select "replace" radio button, click advanced settings button. from the list of fonts, select a font and replace it with a device font from the dropdown box. click ok twice.
    then on your code, so you can use the device font and printer commands, create a font object with the true type font's name that you have substituted to a device font.
    for barcode fonts to work, you need to set the correct font size of the chosen barcode.

    that's it. thanks.
  • Friday, February 01, 2008 4:27 AM
     
     

    Hi e.d,

     

    I’m glad to hear that you have solved the issue by yourself. Cheers!

     

    Thank you for sharing your solution here. It will be very beneficial to other community members having similar questions.

     

     

    Best regards,

    Martin

  • Saturday, May 17, 2008 3:21 PM
     
     

    I am also having this trouble, but under the printer settings i dont see any option for font subsitution.

     

    I have to convert a vb6 pos application and the only thing that i hit a snag on is using the control font for epson tm88 thermal printer.

     

    Using a truetype is fine with the exception that the printer prints 10 times slower.

     

    Please help me figure out a way to use a device font in vb.net

  • Thursday, December 03, 2009 6:35 PM
     
      Has Code
    @e.d.
    This is good solution with limited use. I would be troublesome to change printer setting for all the printers that are using the application. We need to have some means to change those setting programmatically or write to the printer with default printer fonts without substitution in printer settings.
    I have following problem:
    I am trying to write to Epson TM-T88III receipt printer using vb.net 2005.  I am using PrintDocument class. Using system fonts is too slow to print so I wanted to use printer native fonts. But my application replaces the printer fonts with system fonts when I try to print.
    Here is the code:
    'to create font
    f_header = New Font("FontA42", 12) ', FontStyle.Bold)
    'to print
     g.DrawString("A D M I T   O N E", f_header, Brushes.Black, 23, y)

    Can any one tell me how to accomplish this printing with printer fonts -- "FontA42" is my printer font?
    Looks like I need to use Win32API. Can I create the instance of printer font with Win32API and then use g.DrawString and use that font to print?
    Please help.....

    Rup


  • Thursday, December 03, 2009 9:10 PM
     
     
    I think that the trick is to tell the font mapper that you want the device font, and no other font will do. The place to tell it that is in the LOGFONT structure. So, construct your font using a LOGFONT that specifies OUT_DEVICE_PRECIS. (That should do it for you.)

    http://msdn.microsoft.com/en-us/library/dd145037(VS.85).aspx

    You can go ahead and use what you have to create a Font object, then get that font's LOGFONT structure (Using ToLogFont), change the lpOutPrecision to include OUT_DEVICE_PRECIS and create the font you want using the LOGFONT and the Font class method FromLogFont.