How to Print receipt in winforms in vb.net
Dim formatLeft As StringFormat = New StringFormat(StringFormatFlags.NoClip)
Dim formatCenter As StringFormat = New StringFormat(formatLeft)
Dim formatRight As StringFormat = New StringFormat(formatLeft)
Dim strMemo As String
Dim strShop As String
strMemo = FormatNumber(Me.TextBox2.Text, "00")
displayString = vbNewLine
strShop = "ShopName"
displayHeaderString = vbNewLine
displayHeaderString += "address" + vbNewLine
displayReceipt = vbNewLine
displayReceipt += "TAX INVOICE" + vbNewLine
displayReceipt1 = "1545451545" + vbNewLine
displayString += "-------------------------------------------"
displayString += vbNewLine
displayString += "Transaction #:" + Me.TextBox1.Text
displayString += vbNewLine
displayString += "Date: " + Date.Today()
displayString += vbNewLine
displayString += "Time: " + DateAndTime.Now().ToLongTimeString()
displayString += vbNewLine
displayString += vbNewLine
displayString += "Table No: " + strMemo
displayString += vbNewLine
displayString += "-------------------------------------------"
displayString += vbNewLine
displayString += "Qty".PadRight(4) + "Description".PadRight(18) + "Total"
displayString += vbNewLine
displayString += "-------------------------------------------"
displayString += vbNewLine
Dim dblGrossTot As Double
Dim dblDiscount As Double
Dim dblNetTotal As Double
Dim dblPaid As Double
Dim dblBalRetuned As Double
Dim Gst As Double
dblDiscount = dt.Rows(0)("Discount")
dblNetTotal = dt.Rows(0)("FPrice")
dblPaid = dt.Rows(0)("AmtPaid")
dblBalRetuned = dt.Rows(0)("Balance").ToString() 'dt.Rows(0)("")
Gst = dt.Rows(0)("Gst")
If dt.Rows.Count > 0 Then
Dim k As Integer
Dim n As Integer
k = dt.Rows.Count - 1
n = 1
Do While Not k < 0
Dim strItemName As String
Dim dblRate As String
Dim dblQty As String
Dim TPrice As String
Dim dblLineTotal As Double
strItemName = dt.Rows(k)("ItemName")
dblRate = FormatNumber(dt.Rows(k)("SRate"), 2)
TPrice = FormatNumber(dt.Rows(k)("TPrice"), 2)
dblQty = dt.Rows(k)("Qty")
dblLineTotal = dblQty * dblRate
dblGrossTot = (dblNetTotal - Gst)
Dim qntyValue As String = String.Empty
If dblQty.Length > 3 Then
'If Quantity length is greater then 20, then take substring of quantity 0 to 3.
dblQty = dblQty.Substring(0, 3)
Else
While dblQty.Length <= 3
'Adding " " in Quantity string until length 20.
qntyValue += ""
dblQty += " "
End While
End If
qntyValue += dblQty.Trim()
displayString += qntyValue.PadRight(4)
If strItemName.Length > 18 Then
'If Description length is greater then 20, then take substring of item 0 to 20.
strItemName = strItemName.Substring(0, 18)
Else
While strItemName.Length <= 18
'Adding " " in Description string until length 20.
strItemName += " "
End While
End If
displayString += strItemName.PadRight(5)
Dim PriceValue As String = String.Empty
If TPrice.Length > 5 Then
TPrice = TPrice.Substring(0, 5)
Else
While TPrice.Length <= 5
'Adding " " in Quantity string until length 20.
PriceValue += ""
TPrice += " "
End While
End If
PriceValue += TPrice.Trim()
displayString += FormatNumber(PriceValue.ToString, 2)
displayString += vbNewLine
k = k - 1
n = n + 1
Loop
displayString += "-------------------------------------------"
displayString += vbNewLine
displayString += "Gross Total :".PadRight(18) + FormatNumber(dblGrossTot.ToString, 2)
displayString += vbNewLine
displayString += "TAX %:".PadRight(18) + FormatNumber(Gst.ToString, 2)
displayString += vbNewLine
displayString += "Discount :".PadRight(18) + FormatNumber(-dblDiscount.ToString, 2)
displayString += vbNewLine
displayString += "Net Total :".PadRight(14) + FormatNumber(dblNetTotal.ToString, 2)
displayString += vbNewLine
displayString += vbNewLine
displayString += "-------------------------------------------"
displayString += vbNewLine
displayString += "Paid :".PadRight(18) + FormatNumber(dblPaid.ToString, 2)
displayString += vbNewLine
displayString += "Bal. Returned :".PadRight(18) + FormatNumber(dblBalRetuned.ToString, 2)
displayString += vbNewLine
displayString += "-------------------------------------------"
displayString += vbNewLine
displayString += vbNewLine
displayString += vbNewLine
displayString += "******************************************"
displayString += vbNewLine
displayString += " Thank You for visiting"
displayString += vbNewLine
displayString += vbNewLine
displayString += vbNewLine
displayString += vbNewLine
displayString += vbNewLine
displayString += vbNewLine
displayString += vbNewLine
displayString += vbNewLine
e.Graphics.DrawString(strShop, New Font("Consolas", 14, FontStyle.Bold), Brushes.Black, 40, 0)
e.Graphics.DrawString(displayReceipt, New Font("Consolas", 12, FontStyle.Regular), Brushes.Black, 38, 18)
e.Graphics.DrawString(displayReceipt1, New Font("Consolas", 10, FontStyle.Regular), Brushes.Black, 15, 55)
e.Graphics.DrawString(displayHeaderString, New Font("Consolas", 7, FontStyle.Regular), Brushes.Black, 10, 10)
e.Graphics.DrawString(displayString, New Font("Consolas", 8, FontStyle.Regular), Brushes.Black, 10, 55)