By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,423 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,423 IT Pros & Developers. It's quick & easy.

Printing a receipt

P: n/a
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
m_PrintDocument.PrintPage
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)
'MsgBox(text_size.Height)
'Dim layout_rect As RectangleF = New RectangleF
(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width,
e.MarginBounds.Height)
Dim layout_rect As RectangleF = New RectangleF
(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width,
text_size.Height)

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
assignment
Nov 12 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
textboxes/tables...)

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,
FileMode.Create)
If iPrintStream Mod 2 = 0 Then '--trying to eliminate printing empty
pages here
m_streams.Add(stream)
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>" + _
"</DeviceInfo>"

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
Next
End Sub

Private Sub PrintPage(ByVal sender As Object, ByVal ev As
PrintPageEventArgs)
Dim pageImage As New Metafile(m_streams
(m_currentPageIndex))
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
Return
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}"".",
strDefaultPrinter)
Return
End If
AddHandler printDoc.PrintPage, AddressOf PrintPage
printDoc.Print()
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})

Export(report)

m_currentPageIndex = 0
Print()

End Sub

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

End Class

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

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.