Printing a receipt



I am writing a program for field work that will use a receipt
printer. I need to be able to adjust the page settings prior to
printing depending on how much needs to be printed. I have been able
to print to this printer but cannot figure out how to overwrite the
page settings.

Below is the code so far:
Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal
e As System.Drawing.Printing.PrintPageEventArgs) Handles
Using string_format As New StringFormat
string_format.Alignment = StringAlignment.Near
Dim text_size As SizeF
Dim font_size As Integer = 12
Dim font_name As String = "Times New Roman"

Using the_font As New Font(font_name, font_size,
FontStyle.Regular, GraphicsUnit.Point)
text_size = e.Graphics.MeasureString(printableData, the_font)
'Dim layout_rect As RectangleF = New RectangleF
(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width,
Dim layout_rect As RectangleF = New RectangleF
(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width,

e.Graphics.DrawString(printableData, the_font, Brushes.Black,
layout_rect, string_format)
End Using
End Using
e.HasMorePages = False
End Sub

printableData above is a function that returns the formated text.

I think that somehow I need to either change the e.pagesettings,
e.pagebounds, or something else, but everything I have tried has not
worked. When trying to change e.pagesettings or e.pagebounds, I get
the following error message:
expression is a value and therefore cannot be the target of an

Rich P

Use a report object to print and format - much more reliable/easier that
writing printing code from scratch. Create a .rdlc file (drag a
ReportViewer control onto a form and click on create report -
automatically generates a .rdlc file where you place you

You have some options now for printing - you can print from the
ReportViewer control - but have to set print settings each time - or you
can print from code where you just click a button. The settings are set
in code. Here is a sample of the code (I think the VS help files also
have a print sample under ReportViewer)


Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports Microsoft.Reporting.WinForms

Imports System
Imports System.IO
Imports System.Data
Imports System.Text
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.Collections.Generic

Public Class clsPrintComment
Implements IDisposable

Private m_currentPageIndex As Integer
Private m_streams As IList(Of Stream)
Dim iPrintStream As Integer

Private Function CreateStream(ByVal name As String, ByVal
fileNameExtension As String, _
ByVal encoding As Encoding, ByVal mimeType
As String, ByVal willSeek As Boolean) As Stream

Dim stream As Stream = New FileStream(name + "." + fileNameExtension,
If iPrintStream Mod 2 = 0 Then '--trying to eliminate printing empty
pages here
End If
iPrintStream += 1
Return stream
End Function

Private Sub Export(ByVal report As LocalReport)

'--this is regular layout for printing on 8x11 paper
Dim deviceInfo As String = _
"<DeviceInfo>" + _
" <OutputFormat>EMF</OutputFormat>" + _
" <PageWidth>8.5in</PageWidth>" + _
" <PageHeight>11in</PageHeight>" + _
" <MarginTop>0.75in</MarginTop>" + _
" <MarginLeft>0.75in</MarginLeft>" + _
" <MarginRight>0.75in</MarginRight>" + _
" <MarginBottom>0.75in</MarginBottom>" + _

Dim warnings() As Warning = Nothing
m_streams = New List(Of Stream)()

report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)

Dim stream As Stream
For Each stream In m_streams
stream.Position = 0
End Sub

Private Sub PrintPage(ByVal sender As Object, ByVal ev As
Dim pageImage As New Metafile(m_streams
ev.Graphics.DrawImage(pageImage, ev.PageBounds)

m_currentPageIndex += 1

ev.HasMorePages = (m_currentPageIndex < m_streams.Count)

'--ev.HasMorePages = False '--set this to false if only want to print
one page

End Sub

Private Sub Print()
If m_streams Is Nothing Or m_streams.Count = 0 Then
End If

Dim printDoc As New PrintDocument()
Dim strDefaultPrinter As String = printDoc.PrinterSettings.PrinterName
'--this gets the default printer for the current workstation

If Not printDoc.PrinterSettings.IsValid Then
Dim msg As String = String.Format("Can't find printer ""{0}"".",
End If
AddHandler printDoc.PrintPage, AddressOf PrintPage
End Sub

Public Sub Run()
iPrintStream = 0 '--iPrintStream is for skipping print empty pages

Dim report As LocalReport = New LocalReport()

report.ReportPath = Application.StartupPath & "\Report1.rdlc"

Dim s1 As String = frmCom.txtComment.Text
Dim parm1 As ReportParameter
parm1 = New ReportParameter("prmComment", s1)
report.SetParameters(New ReportParameter() {parm1})


m_currentPageIndex = 0

End Sub

Public Overloads Sub Dispose() Implements
If Not (m_streams Is Nothing) Then
Dim stream As Stream
For Each stream In m_streams
m_streams = Nothing
End If
End Sub

End Class

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question