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.
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:
Update!!!
Rani bikin juga class supaya printing langsung bisa buat cetak gambar, mengatur font, dan membuat tabel lebih rapi. Semoga bisa membantu temen-temen yang perlu contoh code untuk cetak struk transaksi Point of Sales (POS) a.k.a software kasir. Cek di link di bawah ya!
https://rani-irsan.blogspot.com/2019/12/vbnet-mencetak-dengan-pos-printer.html
14 Comments
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..
Tks
terus simpen di variable string data2 yang mau di print nya
https://rani-irsan.blogspot.com/2019/12/vbnet-print-gambar-dengan-printdocument.html
kalau ini saya mau pakai kertas roll
bagaimana cara menghentikan printer feed begitu selesai print
jadi kertas tidka menggulung terus sampai habis
klo tipe printer kita dot matrik kayak epson lx itu jadi PR tersendiri, biasa nya harus setting PaperSize.
https://rani-irsan.blogspot.com/2019/12/vbnet-mencetak-dengan-pos-printer.html
Disitu sudah lebih dilengkapi lagi dan ada sample source yang bisa di download.