VB.NET: Direct Printing


How do we send printing command directly to printer without additional reporting application?
I usually use this kind of printing on my Point of Sales (POS) Application.

When I used Visual Basic 6 (VB6) we can simply use this syntax:
Printer.Print(text)

We don't have it on VB.NET, but thanks to cicatrix for the class which he built.
http://www.vbforums.com/showthread.php?608727-Printer-class-for-VB-NET

This class allow us to send plain text directly to printer. Let's see how I use it.

First, add new class into project.


Name it as Printer.


Then I write the code with some adjustment, because I want my own font type.

Public Class Printer
    Private Shared Lines As New Queue(Of String)
    Private Shared _myfont As Font
    Private Shared prn As Printing.PrintDocument

    Shared Sub New()
        _myfont = New Font("Courier New", _
                  8, FontStyle.Regular, GraphicsUnit.Point)
        prn =
New Printing.PrintDocument
        AddHandler prn.PrintPage, AddressOf PrintPageHandler
    End Sub

    Public Shared Sub Print(ByVal text As String)
        Dim linesarray() = text.Split(New String() _
            {Environment.NewLine}, StringSplitOptions.None)

        For Each line As String In linesarray
            Lines.Enqueue(line)
        Next
        prn.Print()
    End Sub

    Private Shared Sub PrintPageHandler(ByVal sender As Object, _
        ByVal e As Printing.PrintPageEventArgs)

       
Dim sf As New StringFormat()
       
Dim vpos As Single = e.PageSettings.HardMarginY

        Do While Lines.Count > 0
            Dim line As String = Lines.Dequeue
            Dim sz As SizeF = e.Graphics.MeasureString( _
                line, _myfont, e.PageSettings.Bounds.Size, sf)

           
Dim rct As New RectangleF( _
                e.PageSettings.HardMarginX, vpos, _
                e.PageBounds.Width - e.PageSettings.HardMarginX * 2, _
                e.PageBounds.Height - e.PageSettings.HardMarginY * 2)

            e.Graphics.DrawString(line, _myfont, Brushes.Black, rct)
            vpos += sz.Height
            If vpos > e.PageSettings.Bounds.Height - _
                e.PageSettings.HardMarginY * 2 Then
                e.HasMorePages = True
                Exit Sub
            End If
        Loop
    End Sub
End Class


Create a simpe UI, only form with button.


Here is the code:

Public Class frmDirectPrint

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        Dim strPrint As String
        strPrint = "Toko Jualan Saya" & vbCrLf
        strPrint = strPrint & "------------------------------" & vbCrLf
        strPrint = strPrint & "No   : TN1254389" & vbCrLf
        strPrint = strPrint & "Kasir: Lana" & vbCrLf
        strPrint = strPrint & " " & vbCrLf
        strPrint = strPrint & "Nama   Qty. Harga SubTotal" & vbCrLf
        strPrint = strPrint &
"------------------------------" & vbCrLf
        strPrint = strPrint & "Ciki     2   5000    10000" & vbCrLf
        strPrint = strPrint & "Akua     3   1000     3000" & vbCrLf
        strPrint = strPrint &
"------------------------------" & vbCrLf
        strPrint = strPrint & "Total                13000" & vbCrLf

        Printer.Print(strPrint)
    End Sub

End Class


Sorry that I used some Indonesian text on the printing value. Hopefully you still can get the purpose. This process will send print to default printer that installed to our PC/Laptop. My PC is using PDF printer (DoPDF). So, let me show you how it works:


This is the result.


Any question? Just comment below.
Happy Coding ^_^


Post a Comment

3 Comments

Muchlis said…
Apakah ini bisa print ke printer dot matrik semacam tmu 220
rani said…
@Muchlis: iya, saya bikin code ini buat print ke printer kecil sekelas TMU 220
Arya Isfandiari said…
Kalau saya print dengan menggunakan ASP.Net apakah sama cara nya? karena contoh ini menggunakan Windows Form.

Mohon bantuannya bu, karena saya baru ini mengerjakan proyek tersebut, proyek saya menggunakan ASP.Net WebForms dan kebetulan bahasa nya sama VB.NET