Sending data/textbox value to thermal printer

JhunBB

Member
Joined
Jul 19, 2012
Messages
11
Programming Experience
Beginner
This is my first Printing of data experience in vb.net:

I need help on how to send DATA/TEXTBOX value to thermal printer.

I am sending the following to thermal printer with 2 font size.

-- Company logo
-- Queuing Number -- font size 55
-- Date and Time -- font size 8

I need an output that woul look like this
-----------------------------------------------
logo
001
Fri, 2 Oct 2015 10:00 AM
-----------------------------------------------


Only the logo and Queuing number is printing and the third row which is the date/time is printing over the logo image.

the script is this:
    Private Sub PrintQue_Print(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintQue.PrintPage


        Static currentChar As Integer
        Dim QFontNum As Font = New Font("Times New Roman", 55, FontStyle.Bold)
        Dim DFont As Font = New Font("Times New Roman", 8, FontStyle.Regular)


        Dim h, w As Integer
        Dim left, top As Integer
        With PrintQue.DefaultPageSettings
            h = 0
            w = 0
            left = 0
            top = 0.25
        End With


        'Queuing Number
        Dim TextToPrint As String = LblQueNum.Text
        Dim LineLen As String = TextToPrint.Length
        Dim spcLen As New String(" "c, Math.Round((30 - LineLen)))


        Dim lineLevel As Integer = CInt(Math.Round(h / 1))
        Dim b As New Rectangle(left, top, w, h)
        Dim format As StringFormat
        format = New StringFormat(StringFormatFlags.LineLimit)
        Dim line, chars As Integer


        Dim imgeLocation As Point = New Point(5, 5)
        Dim path As String = "D:\Database\Releasing\img\logo-bw2.jpg"
        Dim img As Image = Image.FromFile(path)
        'Draw the image.
        e.Graphics.DrawImage(img, imgeLocation) 


        e.Graphics.MeasureString(Mid(TextToPrint, currentChar + 1), QFontNum, New SizeF(w, h), format, chars, line)
        e.Graphics.DrawString(TextToPrint.Substring(currentChar, chars), QFontNum, Brushes.Black, b, format)


        currentChar = currentChar + chars
        If currentChar < TextToPrint.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
            currentChar = 0
        End If


         Dim curDate As Date = Environment.NewLine & DateTime.Now.ToString
         Dim StringToPrint As String = curDate.ToString("ddd, dd MMM yyyy") & Environment.NewLine & FormatDateTime(Now().ToString, vbLongTime)


        e.Graphics.MeasureString(Mid(StringToPrint, currentChar + 1), DFont, New SizeF(w, h), format, chars, line)
        e.Graphics.DrawString(StringToPrint.Substring(currentChar, chars), DFont, Brushes.Black, b, format)


        currentChar = currentChar + chars
        If currentChar < StringToPrint.Length Then
         e.HasMorePages = True
        Else
        e.HasMorePages = False
        currentChar = 0
        End If


    End Sub

---------------------------

Thank you for your help!
 
Last edited by a moderator:
Hi,

Since there seems to be so little to print here, it seems strange to me why you have focused so much effort on printing individual characters and forcing the printing of additional pages when it seems you do not need to. When it comes to Printing and Graphics of any kind the most important thing is to make sure you keep control of your X and Y Coordinate so that you know where Drawings will Start and End. Here is a small rewrite of how I would start to go about this. See if this helps to get you on track:-

'Set Start Margins
Dim leftMargin As Integer = 5
Dim topMargin As Integer = 5
Dim pageWidth As Integer = e.PageBounds.Width
Dim pageHeight As Integer = e.PageBounds.Height
 
'Get the Logo and Print It
Dim myImage As Image = Image.FromFile("c:\temp\accept_300x300.png")
Dim imageRect As New Rectangle(topMargin, leftMargin, myImage.Width, myImage.Height)
e.Graphics.DrawImage(myImage, imageRect)
 
'Get the Queuing Number
Dim largeFont As New Font("Times New Roman", 55, FontStyle.Bold)
Dim printString As String = "00001"
Dim currentXPos As Integer = leftMargin
Dim currentYPos As Integer = topMargin + myImage.Height + 5
Dim currentPrintRect As New Rectangle(currentXPos, currentYPos, pageWidth, pageHeight)
e.Graphics.DrawString(printString, largeFont, Brushes.Black, currentPrintRect)
 
'Finally Print the Date and Time
Dim smallFont As New Font("Times New Roman", 8, FontStyle.Regular)
printString = Now.ToString("ddd, dd MMM yyyy HH:mm:ss")
currentPrintRect.Offset(0, CInt(largeFont.GetHeight + 5))
e.Graphics.DrawString(printString, smallFont, Brushes.Black, currentPrintRect)


In this case I keep Control of my X and Y Coordinates by using Rectangles.

Hope that helps.

Cheers,

Ian
 
Thank you sir Ian for your help.

I will incorporate this script and will update you if it solve my problem.

Being new to vb.net programming is really difficult but i find it so interesting now.

Thanks again!
 
Sir Ian,

Good afternoon!

Your script works! its awesome!

I will study the other printing option especially in working with multiple data rows.

excited to move forward!

Thank you again sir ian!
 
Hi,

Glad to have helped. To move forward from this point have a look at the Properties exposed by the “e” variable which is of type PrintPageEventArgs. This actually provides a lot of starting information which you can use such as the MarginBounds Property which gives you an initial Rectangle which defines the Bounds of the Printable area of the Page based on the set Margins. Have a look here:-

PrintPageEventArgs Class

Another one to think about is Disposing of Objects once they have been used. This releases back to your system any resources used by your Objects once you have finished with them. Any Class which Implements IDisposable should be Disposed and Fonts and StringFormats are just two Classes that should be Disposed once you have finished using them. One of the best ways to achieve this is to Declare your objects as part of a Using Block. Have a look here:-

Using Statement (Visual Basic)

Hope that helps.

Cheers,

Ian
 
Yes! i will sir ian.

I am actually curious about the use of "e" variable. I will study and go deeper on this so i could relay the knowledge too to my co-programmer.

Thank you again for your continued guidance and support to all the newbies in vb.net programming especially me.
 
Back
Top