
Print on Receipt printer in VB.NET
Hello,
I have the following code and it does not work for
Receipt printers I used Generic Text Driver for that
printer. What is the solution to print on receipt printer?
Thanks for your help.
david
--------------------
Public Class TextFilePrintDocument
Inherits PrintDocument
Private printFont As Font
Private streamToPrint As StreamReader
Public Sub New(ByVal streamToPrint As
StreamReader)
MyBase.New()
Me.streamToPrint = streamToPrint
End Sub
'Override OnBeginPrint to set up the font we are
going to use
Protected Overrides Sub OnBeginPrint(ByVal ev As
PrintEventArgs)
MyBase.OnBeginPrint(ev)
printFont = New Font("Arial", 10)
End Sub
'Override the OnPrintPage to provide the printing
logic for the document
Protected Overrides Sub OnPrintPage(ByVal ev As
PrintPageEventArgs)
MyBase.OnPrintPage(ev)
Dim lpp As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single =
ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String
'Work out the number of lines per page
'Use the MarginBounds on the event to do this
lpp = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics)
'Now iterate over the file printing out each
line
'NOTE WELL: This assumes that a single line
is not wider than the page width
'Check count first so that we don't read line
that we won't print
line = streamToPrint.ReadLine()
While (count < lpp And line <> Nothing)
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics))
ev.Graphics.DrawString(line, printFont,
Brushes.Black, leftMargin, _
yPos, New
StringFormat())
count = count + 1
If (count < lpp) Then
line = streamToPrint.ReadLine()
End If
End While
'If we have more lines then print another page
If (line <> Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
End Class
Private Sub Button22_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button22.Click
'test for print
Try
Dim streamToPrint As StreamReader = New
StreamReader("unpaidck.dat")
Try
'Assumes the default printer
Dim pd As TextFilePrintDocument = New
TextFilePrintDocument(streamToPrint)
Dim dlg As New PrintDialog()
dlg.Document = pd
Dim result As DialogResult =
dlg.ShowDialog()
If (result =
System.Windows.Forms.DialogResult.OK) Then
pd.Print()
End If
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show("An error occurred
printing the file - " + ex.Message)
End Try
End Sub