22 April 2015

VB.NET: Print Langsung Ke Printer


Ngeprint langsung dari VB.NET ke printer tanpa crystal report, SQL reporting, ataupun aplikasi reporting lainya gimana ya?
Klo di VB6 yang tinggal pake sintaks:
Printer.Print(text)

Ternyata direct print di VB.NET ga semudah itu ͼ(ݓ_ݓ)ͽ
But thanks to cicatrix for the class which he built.
http://www.vbforums.com/showthread.php?608727-Printer-class-for-VB-NET

Kita jadi bisa kirim teks polos langsung ke printer. Saya pun meninggalkan jejak di blog ini saat mencobanya.
Pertama tambahkan file class.

Beri nama Class Printer.

Kemudian tulis code berikut (Rani sesuaikan code nya, karena ingin jenis font tertentu)
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

Kemudian kita membuat UI untuk proses print. Sederhana aja cuman form sama Button.
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


Proses ini akan mencetak teks menggunakan printer default. PC Rani menggunakan DoPDF sebagai printer default, jadi saat running hasilnya seperti ini:



Click here if you like this article.


5 comments:

LUT-V D' ANGEL said...

kalau mau ukuran huruf di sebagian baris itu gimana caranya?

rani irsan said...

class yang ini sih kayak nya dibikin simple dengan font seragam.
klo pengen beda2 berarti mesti bongkar class printer nya...
clue nya pas print cetak string itu kan code nya:
e.Graphics.DrawString(line, _myfont, Brushes.Black, rct)

code diatas ngeprint sebaris, klo mau diubah2 berarti _myfont nya yang diganti2..

Yakub elfazrie said...

untuk X,Y klo kita tentuin sendiri gimana?

misjan nurbudiman said...

Kalo mau di hubungkan dengan database gimana ya bu?
Tks

rani irsan said...

ya tinggal ambil dulu data nya dari database (tergantung database nya apa?)
terus simpen di variable string data2 yang mau di print nya