473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
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
1 13574
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: PW | last post by:
I have to setup a point-of-sale system for a friend. He has a PC, barcode scanner, receipt printer and cashdrawer. The receipt printer connects to the serial port. The cashdrawer connects to...
0
by: Samuel Lee | last post by:
Could anyone convert the following vb 6.0 codes into vb.net version? '************************************************************************ '** Button to print a sales receipt - spool print...
0
by: Michelle Keys | last post by:
I am trying to call a print function to print a string from a database using javascript. Which is RC_DATA of Varchar2(2500). This is a javascript is not being used. I have a thing that needs to...
1
by: jimmy | last post by:
Hi, I'm supposed to come up with receipt printing program similar tothose receipt printing you typically see in a supermarket. Whena new item is added, one more line is added to the printoutuntil...
1
by: hasanainf | last post by:
Hi all and thanking you all in advance for your help My client wants to use a receipt printer. Since I have never used one before I have some questions. 1. Unlike other printers which are set...
2
by: Ravi | last post by:
Hi People, I am facing an issue with printing where I have to print receipts of a custom size. Each receipt is about 8 inch by 4inch. I am using window.print(). When a receipt is printed another...
9
by: Gurbinder Kaur | last post by:
Hi, i have made a project for printing fee receipt. Using Windows-XP, VBasic 6.0 & Ms-Access-97. i have designed a form for prinring receipts. m paper-size is 5x10. Two copies of bills in this...
7
by: ARC | last post by:
Hello all, What's the proper paper size setting if you want to do a receipt printer report, that's a continuous form? I don't really see an option for a continuous paper size. Thanks! Andy
0
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.