Printing a receipt

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Glenn

    #1

    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(ByVa l sender As Object, ByVal
    e As System.Drawing. Printing.PrintP ageEventArgs) Handles
    m_PrintDocument .PrintPage
    Using string_format As New StringFormat
    string_format.A lignment = 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.Regul ar, GraphicsUnit.Po int)
    text_size = e.Graphics.Meas ureString(print ableData, the_font)
    'MsgBox(text_si ze.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.Heigh t)

    e.Graphics.Draw String(printabl eData, 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
  • Rich P

    #2
    Re: Printing a receipt

    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.Sql Client
    Imports System.Windows. Forms
    Imports Microsoft.Repor ting.WinForms

    Imports System
    Imports System.IO
    Imports System.Data
    Imports System.Text
    Imports System.Drawing. Imaging
    Imports System.Drawing. Printing
    Imports System.Collecti ons.Generic

    Public Class clsPrintComment
    Implements IDisposable

    Private m_currentPageIn dex As Integer
    Private m_streams As IList(Of Stream)
    Dim iPrintStream As Integer

    Private Function CreateStream(By Val name As String, ByVal
    fileNameExtensi on As String, _
    ByVal encoding As Encoding, ByVal mimeType
    As String, ByVal willSeek As Boolean) As Stream

    Dim stream As Stream = New FileStream(name + "." + fileNameExtensi on,
    FileMode.Create )
    If iPrintStream Mod 2 = 0 Then '--trying to eliminate printing empty
    pages here
    m_streams.Add(s tream)
    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 = _
    "<DeviceInf o>" + _
    " <OutputFormat>E MF</OutputFormat>" + _
    " <PageWidth>8.5i n</PageWidth>" + _
    " <PageHeight>11i n</PageHeight>" + _
    " <MarginTop>0.75 in</MarginTop>" + _
    " <MarginLeft>0.7 5in</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
    PrintPageEventA rgs)
    Dim pageImage As New Metafile(m_stre ams
    (m_currentPageI ndex))
    ev.Graphics.Dra wImage(pageImag e, ev.PageBounds)

    m_currentPageIn dex += 1

    ev.HasMorePages = (m_currentPageI ndex < 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 strDefaultPrint er As String = printDoc.Printe rSettings.Print erName
    '--this gets the default printer for the current workstation

    If Not printDoc.Printe rSettings.IsVal id Then
    Dim msg As String = String.Format(" Can't find printer ""{0}"".",
    strDefaultPrint er)
    Return
    End If
    AddHandler printDoc.PrintP age, 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.ReportPa th = Application.Sta rtupPath & "\Report1.r dlc"

    Dim s1 As String = frmCom.txtComme nt.Text
    Dim parm1 As ReportParameter
    parm1 = New ReportParameter ("prmComment ", s1)
    report.SetParam eters(New ReportParameter () {parm1})

    Export(report)

    m_currentPageIn dex = 0
    Print()

    End Sub

    Public Overloads Sub Dispose() Implements
    IDisposable.Dis pose
    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 ***

    Comment

    Working...