0% found this document useful (0 votes)
317 views6 pages

POS Printing

This document describes a class that makes point-of-sale (POS) printing easier using the VB6 printer object. The class allows developers to add images, use different languages and text directions, and print previews. It controls font sizes and styles, prints images, and sections the page for receipt layout. Sample code shows creating a receipt using the class, resulting in the printed receipt shown. Areas for future improvement are noted, such as eliminating the print dialog for large prints.

Uploaded by

Romeo Balingao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
317 views6 pages

POS Printing

This document describes a class that makes point-of-sale (POS) printing easier using the VB6 printer object. The class allows developers to add images, use different languages and text directions, and print previews. It controls font sizes and styles, prints images, and sections the page for receipt layout. Sample code shows creating a receipt using the class, resulting in the printed receipt shown. Areas for future improvement are noted, such as eliminating the print dialog for large prints.

Uploaded by

Romeo Balingao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

POS Printing: The Easy Way

PeaceTiger, 16 Sep 2010 CPOL


4.71 (6 votes)
Rate this:
vote 1vote 2vote 3vote 4vote 5
VB6 Printer object supports printing to POS printers too. In this article, we will see Why and
How.

Download source - 14.08 KB

Introduction
This is a class that helps developers in designing POS printing forms the easy way. Developers
can add images (Logos, Ads, etc.); can use any language with any direction (RTL, LTR) easily;
and can use print preview to save paper printed for testing.

Background
POS printing using the EPS/POS standard has proved difficult, after spending days of designing
the printing class; I couldn't write Arabic text for unknown reasons. Printing images needed tons
of code, and print-previewing was almost impossible since you might need to build an engine to
parse those specific standard codes to display them on the screen.
A much easier solution was to use the classic VB printer object, those unfamiliar with the printer
object can read more about it here.
To use the good old printer class of VB 6.0 in your [Link] code, you will need to use the
PowerPack compatibility library, which can be downloaded here.

Using the Code


Start by adding [Link] to the references of the project.
Some imports will be needed throughout the code:
Collapse | Copy Code
Imports [Link]
Imports [Link].VB6

Some declarations and needed Enums:


Collapse | Copy Code
Private p As Printer
Private _Path As String
Private _Align As TextAlignment = [Link]
Private bIsDebug As Boolean = True
Public Enum TextAlignment As Byte
[Default] = 0
Left
Center
Right
End Enum

Now the class constructors takes the printer name as parameters, and also the Application path
for constructing the path of images to be used (if any), you can pass the
[Link] for that.
Collapse | Copy Code
#Region "Constructors"
Public Sub New(ByVal AppPath As String)
SetPrinterName("GP-80220II (USB2)", AppPath)
End Sub
Public Sub New(ByVal strPrinterName As String, ByVal AppPath As
String)
SetPrinterName(strPrinterName, AppPath)
End Sub
Private Sub SetPrinterName(ByVal PrinterName As String, ByVal AppPath
As String)
Dim prnPrinter As Printer

End Sub
#End Region

For Each prnPrinter In Printers


If [Link] = PrinterName Then
p = prnPrinter
Exit For
End If
Next
[Link] = "ERP System"
[Link] = AppPath
If bIsDebug Then
[Link] = [Link]
End If

I used the following font sizes which were appropriate for my case. Feel free to change that as
appropriate to yours:

Normal Font: 9.5

Large Font: 15

Small Font: 6

The following code shows properties to control fonts:


Collapse | Copy Code
#Region "Font"
Public Property Alignment() As TextAlignment
Get
Return _Align
End Get
Set(ByVal value As TextAlignment)
_Align = value
End Set
End Property
Public Sub AlignLeft()
_Align = [Link]
End Sub
Public Sub AlignCenter()
_Align = [Link]
End Sub
Public Sub AlignRight()
_Align = [Link]
End Sub
Public Property FontName() As String
Get
Return [Link]

End Get
Set(ByVal value As String)
[Link] = value
End Set
End Property
Public Property FontSize() As Single
Get
Return [Link]
End Get
Set(ByVal value As Single)
[Link] = value
End Set
End Property
Public Property Bold() As Boolean
Get
Return [Link]
End Get
Set(ByVal value As Boolean)
[Link] = value
End Set
End Property
Public Sub DrawLine()
[Link] = 2
[Link]([Link], [Link])
[Link] += 20 ' to move under the drawn line
End Sub
Public Sub NormalFont()
[Link] = 9.5F
End Sub
Public Sub BigFont()
[Link] = 15.0F
End Sub
Public Sub SmallFont()
[Link] = 6.0F
End Sub
Public Sub SetFont(Optional ByVal FontSize As Single = 9.5F, _
Optional ByVal FontName As String = "FontA1x1", _
Optional ByVal BoldType As Boolean = False)
[Link] = FontSize
[Link] = FontName
[Link] = BoldType
End Sub
#End Region

For image printing, I used a PrintLogo sub, but you can use a general method (like
PrintImage below):
Collapse | Copy Code

#Region "Images"
Public Property Path() As String
Get
Return _Path
End Get
Set(ByVal value As String)
_Path = value
End Set
End Property
Public Sub PrintLogo()
[Link](_Path & "\[Link]")
End Sub
Private Sub PrintImage(ByVal FileName As String)
Dim pic As Image
pic = [Link](FileName)
[Link](pic, [Link], [Link])

End Sub
#End Region

[Link] = [Link] + [Link]

Now, for my case, I sectioned the paper into 6 sections (sixths) for easier control. This might be
appropriate for your case, but if not; you can easily change that.
Also notice that my printer prints 48 characters in normal font, that is why I also sectioned the
paper into 48 columns.
Collapse | Copy Code

#Region "Control"
Public Sub NewPage()
[Link]()
End Sub
Public Property RTL() As Boolean
Get
Return [Link]
End Get
Set(ByVal value As Boolean)
[Link] = value
End Set
End Property
Public Sub FeedPaper(Optional ByVal nlines As Integer = 3)
For i As Integer = 1 To nlines
[Link]("")
Next
End Sub
Public Sub GotoCol(Optional ByVal ColNumber As Integer = 0)
Dim ColWidth As Single = [Link] / 48
[Link] = ColWidth * ColNumber
End Sub
Public Sub GotoSixth(Optional ByVal nSixth As Integer = 1)
Dim OneSixth As Single = [Link] / 6
[Link] = OneSixth * (nSixth - 1)
End Sub
Public Sub UnderlineOn()
[Link] = True
End Sub
Public Sub UnderlineOff()
[Link] = False
End Sub
Public Sub EndDoc()
[Link]()
End Sub
Public Sub EndJob()
[Link]()
End Sub
Public Sub WriteLine(ByVal Text As String)
Dim sTextWidth As Single = [Link](Text)
Select Case _Align
Case [Link]
'do nothing
Case [Link]
[Link] = 0
Case [Link]
[Link] = ([Link] - sTextWidth) / 2
Case [Link]
[Link] = ([Link] - sTextWidth)
End Select
[Link](Text)
End Sub
Public Sub WriteChars(ByVal Text As String)
[Link](Text)
End Sub

Public Sub CutPaper()


[Link]()
End Sub
#End Region

For using the class, a sample receipt is being created, see code and resulted print.
Collapse | Copy Code

Dim P As New PrinterClass([Link])


With P
'Printing Logo
.RTL = False
.PrintLogo()
'Printing Title
.FeedPaper(4)
.AlignCenter()
.BigFont()
.Bold = True
.WriteLine("Sales Receipt")
'Printing Date
.GotoSixth(1)
.NormalFont()
.WriteChars("Date:")
.WriteLine([Link])
.DrawLine()
.FeedPaper(2)
'Printing Header
.GotoSixth(1)
.WriteChars("#")
.GotoSixth(2)
.WriteChars("Description")
.GotoSixth(5)
.WriteChars("Count")
.GotoSixth(6)
.WriteChars("Total")
.WriteLine("")
.DrawLine()
'.FeedPaper(1)
'Printing Items
.SmallFont()
Dim i As Integer
For i = 1 To 6
.GotoSixth(1)
.WriteChars(i)
.GotoSixth(2)
.WriteChars("Item# " & (Rnd() * 100) \ 1)
.GotoSixth(5)
.WriteChars(Rnd() * 10 \ 1)
.GotoSixth(6)
.WriteChars((Rnd() * 50 \ 1) & " JD(s)")
.WriteLine("")
Next
'Printing Totals
.NormalFont()
.DrawLine()
.GotoSixth(1)
.UnderlineOn()
.WriteChars("Total")
.UnderlineOff()
.GotoSixth(5)
.WriteChars((Rnd() * 300 \ 1) & " JD(s)")
.CutPaper()
' Can be used with real printer to cut the paper.

'Ending the session


.EndDoc()
End With

Results

Things to Notice

Printer name is hard coded, but it can easily be set to read from a configuration file.

The printer should already be defined (driver installed) to the system, especially when
using the "Print Preview" (bIsDebug set to true), so that the preview window reflects
the actual properties of the printer (width, font, etc.).

Points of Interest
The following points can be seen as problems in the approach and can be used for future
development of the article:

While printing, a "print dialogue" shows, in my case (of printing small POS receipts) this
happens fast enough before it disappears, but it can still prevent application input till it
disappears for bigger prints.

You might also like