VB.NET RAW Receipt Reports Source Code

In any programming include Visual Basic VB.NET, reporting become one of the most important aspect, cause with reports we will have something (data) that can used to what must to do the next.

In here I’ve source code how to make/generate RAW Reporting that fit to used for mini market, and small business store elsewhere.

To create RAW Reports you just create one project and add one button, and create class, then copy paste below code.

Class “Print.vb

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

This code for event “Form1.vb

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim strPrint As String
        strPrint = "Nosware Store & Coffe Shop" & vbCrLf
        strPrint = strPrint & "------------------------------" & vbCrLf
        strPrint = strPrint & "No     : TN1254389" & vbCrLf
        strPrint = strPrint & "Cashier: Soni" & vbCrLf
        strPrint = strPrint & " " & vbCrLf
        strPrint = strPrint & "Nama   Qty. Costs SubTotal" & vbCrLf
        strPrint = strPrint & "------------------------------" & vbCrLf
        strPrint = strPrint & "Sauce    2   5000    10000" & vbCrLf
        strPrint = strPrint & "Coffe    3   1000     3000" & vbCrLf
        strPrint = strPrint & "Sugar    1   8000     3000" & vbCrLf
        strPrint = strPrint & "------------------------------" & vbCrLf
        strPrint = strPrint & "Total                13000" & vbCrLf
        Printer.Print(strPrint)
    End Sub
End Class

If code error replace &amp with only &.

The result will look like this:

Receipt Report with VB.NET

Source Code, can be downloaded here: https://nosware.com/go/receiptreportvb

Leave a Comment