-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOSPrinter.vb
163 lines (126 loc) · 4.8 KB
/
POSPrinter.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'*********************************************************************
' LIBRARY NAME: Simple .NET POSPrinter
' DESCRIPTION: This is a VB.NET library that can be used to easily manage a thermal printer
' without using ESC/POS commands.
' This library uses System.Drawing.Printer and it is not needed to use virtual
' COM port.
' AUTHOR: Timothy Franceschi
' COPYRIGHT: © 2023 Timothy Franceschi
' LICENSE: MIT License
' VERSION: 2.0.0
'*********************************************************************
Imports System.Drawing.Printing
Imports System.Net
Imports System.Runtime.CompilerServices
Imports Newtonsoft.Json.Linq
Imports ZXing
Imports ZXing.Common
Imports ZXing.Windows.Compatibility
Public Class POSPrinter
Dim myFont As Font
Dim myBrush As SolidBrush
Dim lineHeight As Single
Dim myPoint As New PointF
Dim e As PrintPageEventArgs
Public Sub New(setE As PrintPageEventArgs)
myFont = New Font("Arial", 15, FontStyle.Bold)
myBrush = New SolidBrush(Color.Black)
myPoint = New PointF(0, 0)
lineHeight = myFont.Height
e = setE
End Sub
Public Property Font() As Font
Set(ByVal newFont As Font)
myFont = newFont
lineHeight = myFont.GetHeight * 1.1
End Set
Get
Return myFont
End Get
End Property
Public Property Brush() As SolidBrush
Set(ByVal newBrush As SolidBrush)
myBrush = newBrush
End Set
Get
Return myBrush
End Get
End Property
Public Property Point() As PointF
Get
Return myPoint
End Get
Set(newPoint As PointF)
myPoint = newPoint
End Set
End Property
Public Sub EmptyLine()
myPoint.Y += lineHeight / 1.1
myPoint.X = 0
End Sub
Public Sub NewLine()
myPoint.Y += lineHeight
myPoint.X = 0
End Sub
Public Sub PrintHLine()
Dim myPen As New Pen(Color.Black)
Dim startPoint As New PointF(0, myPoint.Y)
Dim endPoint As New PointF(e.MarginBounds.Right, myPoint.Y)
e.Graphics.DrawLine(myPen, startPoint, endPoint)
myPoint = New PointF(0, myPoint.Y + (lineHeight * 0.8))
End Sub
Public Sub PrintBarcode(ByVal testo As String, ByVal options As EncodingOptions, ByVal Optional format As BarcodeFormat = BarcodeFormat.CODE_128)
Dim writer As New BarcodeWriter() With {
.Format = format,
.Options = options
}
' Genera immagine codice a barre
Dim barcodeImg As Bitmap = writer.Write(testo)
' Stampa immagine
e.Graphics.DrawImage(barcodeImg, myPoint)
End Sub
Public Sub PrintText(ByVal testo As String, Optional align As String = "left")
Dim textPos As PointF
Select Case align
Case "center"
textPos = CenterPosition(testo)
Case "right"
textPos = RightPosition(testo)
Case Else
textPos = myPoint
End Select
e.Graphics.DrawString(testo, myFont, myBrush, textPos)
Dim textSize As SizeF = e.Graphics.MeasureString(testo, myFont)
myPoint.X += textSize.Width
End Sub
Public Sub PrintTextLn(testo As String, Optional align As String = "left")
Dim textPos As PointF
Select Case align
Case "center"
textPos = CenterPosition(testo)
Case "right"
textPos = RightPosition(testo)
Case Else
textPos = myPoint
End Select
e.Graphics.DrawString(testo, myFont, myBrush, textPos)
myPoint.Y += lineHeight / 1.1
myPoint.X = 0
End Sub
Private Function CenterPosition(testo As String) As PointF
'Calcola dimensioni testo
Dim textSize As SizeF = e.Graphics.MeasureString(testo, myFont)
Dim freeSpace As Single = e.PageSettings.PaperSize.Width - textSize.Width
'Calcola coordinata X del testo
Dim centerX As Single = ((e.PageSettings.PaperSize.Width - textSize.Width) / 2) - (e.PageSettings.Margins.Left / 2)
Return New PointF(centerX, myPoint.Y)
End Function
Private Function RightPosition(testo As String) As PointF
'Calcola dimensioni testo
Dim textSize As SizeF = e.Graphics.MeasureString(testo, myFont)
Dim freeSpace As Single = e.PageSettings.PaperSize.Width - textSize.Width
'Calcola coordinata X del testo
Dim centerX As Single = ((e.PageSettings.PaperSize.Width - textSize.Width) / 2)
Return New PointF(centerX, myPoint.Y)
End Function
End Class