Click here to Skip to main content
14,738,075 members
Articles » Desktop Development » Printing » General
Posted 16 Sep 2010

Tagged as


25 bookmarked

POS Printing: The Easy Way

Rate me:
Please Sign up or sign in to vote.
4.89/5 (14 votes)
16 Sep 2010CPOL
VB6 Printer object supports printing to POS printers too. In this article, we will see Why and How.


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.


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 VB.NET code, you will need to use the PowerPack compatibility library, which can be downloaded here.

Using the Code

Start by adding Microsoft.VisualBasic.PowerPacks to the references of the project.

Some imports will be needed throughout the code:

Imports System.Drawing
Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6 

Some declarations and needed Enums:

Private p As Printer
Private _Path As String
Private _Align As TextAlignment = TextAlignment.Default
Private bIsDebug As Boolean = True
Public Enum TextAlignment As Byte
	[Default] = 0
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 Application.StartupPath for that.

 #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

		For Each prnPrinter In Printers
			If prnPrinter.DeviceName = PrinterName Then
				p = prnPrinter
				Exit For
			End If
		p.DocumentName = "ERP System"
		Me.Path = AppPath
		If bIsDebug Then
			p.PrintAction = Printing.PrintAction.PrintToPreview
		End If
	End Sub
#End Region

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:

#Region "Font"
	Public Property Alignment() As TextAlignment
			Return _Align
		End Get
		Set(ByVal value As TextAlignment)
			_Align = value
		End Set
	End Property
	Public Sub AlignLeft()
		_Align = TextAlignment.Left
	End Sub
	Public Sub AlignCenter()
		_Align = TextAlignment.Center
	End Sub
	Public Sub AlignRight()
		_Align = TextAlignment.Right
	End Sub
	Public Property FontName() As String
			Return p.FontName
		End Get
		Set(ByVal value As String)
			p.FontName = value
		End Set
	End Property

	Public Property FontSize() As Single
			Return p.FontSize
		End Get
		Set(ByVal value As Single)
			p.FontSize = value
		End Set
	End Property
	Public Property Bold() As Boolean
			Return p.FontBold
		End Get
		Set(ByVal value As Boolean)
			p.FontBold = value
		End Set
	End Property
	Public Sub DrawLine()
		p.DrawWidth = 2
		p.Line(p.Width, p.CurrentY)
		p.CurrentY += 20 ' to move under the drawn line
	End Sub
	Public Sub NormalFont()
		Me.FontSize = 9.5F
	End Sub
	Public Sub BigFont()
		Me.FontSize = 15.0F
	End Sub
	Public Sub SmallFont()
		Me.FontSize = 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)
		Me.FontSize = FontSize
		Me.FontName = FontName
		Me.Bold = BoldType
	End Sub
#End Region 

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

 #Region "Images"
 Public Property Path() As String
			Return _Path
		End Get
		Set(ByVal value As String)
			_Path = value
		End Set
	End Property
	Public Sub PrintLogo()
		Me.PrintImage(_Path & "\Logo.bmp")
	End Sub
	Private Sub PrintImage(ByVal FileName As String)
		Dim pic As Image

		pic = pic.FromFile(FileName)

		p.PaintPicture(pic, p.CurrentX, p.CurrentY)
		p.CurrentY = p.CurrentY + pic.Height
	End Sub
#End Region 

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.

  #Region "Control"
	Public Sub NewPage()
	End Sub
	Public Property RTL() As Boolean
			Return p.RightToLeft
		End Get
		Set(ByVal value As Boolean)
			p.RightToLeft = value
		End Set
	End Property
	Public Sub FeedPaper(Optional ByVal nlines As Integer = 3)
		For i As Integer = 1 To nlines
	End Sub

	Public Sub GotoCol(Optional ByVal ColNumber As Integer = 0)
		Dim ColWidth As Single = p.Width / 48
		p.CurrentX = ColWidth * ColNumber
	End Sub
	Public Sub GotoSixth(Optional ByVal nSixth As Integer = 1)
		Dim OneSixth As Single = p.Width / 6
		p.CurrentX = OneSixth * (nSixth - 1)
	End Sub

	Public Sub UnderlineOn()
		p.FontUnderline = True
	End Sub
	Public Sub UnderlineOff()
		p.FontUnderline = False
	End Sub
	Public Sub EndDoc()
	End Sub
	Public Sub EndJob()
	End Sub
	Public Sub WriteLine(ByVal Text As String)
		Dim sTextWidth As Single = p.TextWidth(Text)
		Select Case _Align
			Case TextAlignment.Default
				'do nothing
			Case TextAlignment.Left
				p.CurrentX = 0
			Case TextAlignment.Center
				p.CurrentX = (p.Width - sTextWidth) / 2
			Case TextAlignment.Right
				p.CurrentX = (p.Width - sTextWidth)
		End Select
	End Sub
	Public Sub WriteChars(ByVal Text As String)
	End Sub
	Public Sub CutPaper()
	End Sub

#End Region 

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

Dim P As New PrinterClass(Application.StartupPath)
With P
	'Printing Logo
	.RTL = False

	'Printing Title
	.Bold = True
	.WriteLine("Sales Receipt")

	'Printing Date

	'Printing Header

	'Printing Items
	Dim i As Integer
	For i = 1 To 6
		.WriteChars("Item# " & (Rnd() * 100) \ 1)
		.WriteChars(Rnd() * 10 \ 1)
		.WriteChars((Rnd() * 50 \ 1) & " JD(s)")

	'Printing Totals
	.WriteChars((Rnd() * 300 \ 1) & " JD(s)")
	.CutPaper()	' Can be used with real printer to cut the paper.

	'Ending the session
End With 


Code 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.


  • 16th September, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Comments and Discussions

Questionlong item names Pin
Cool Smith7-Jan-20 23:43
MemberCool Smith7-Jan-20 23:43 
QuestionObject reference not set to an instance of an object. ERROR Pin
besaridrizi6-Sep-19 11:34
Memberbesaridrizi6-Sep-19 11:34 
QuestionThe whole application closes after printing Pin
Member 1174530314-Apr-19 6:37
MemberMember 1174530314-Apr-19 6:37 
AnswerRe: The whole application closes after printing Pin
Member 1174530314-Apr-19 6:53
MemberMember 1174530314-Apr-19 6:53 
QuestionVB6 version of class Pin
Member 1375396429-Mar-18 15:47
MemberMember 1375396429-Mar-18 15:47 
QuestionPrint a table using this class? Pin
Alvin Seliang12-Feb-18 0:47
MemberAlvin Seliang12-Feb-18 0:47 
QuestionIs there any way.... Pin
Member 1361352512-Jan-18 22:58
MemberMember 1361352512-Jan-18 22:58 
QuestionCopies Pin
Glen Dias6-Jun-17 3:52
MemberGlen Dias6-Jun-17 3:52 
QuestionNumber Alignment Right Pin
gvcs4-Apr-17 10:20
Membergvcs4-Apr-17 10:20 
AnswerRe: Number Alignment Right Pin
BIBASWAN4-Aug-17 6:18
MemberBIBASWAN4-Aug-17 6:18 
Questionhow to set paper size? Pin
Member 126112394-Feb-17 19:00
MemberMember 126112394-Feb-17 19:00 
QuestionHide Printing page x of y Pin
Member 1282262919-Dec-16 19:49
MemberMember 1282262919-Dec-16 19:49 
AnswerRe: Hide Printing page x of y Pin
Member 128226296-Jan-17 23:35
MemberMember 128226296-Jan-17 23:35 
Questioncan i do this from vb6 only? Pin
Member 128386978-Nov-16 0:40
MemberMember 128386978-Nov-16 0:40 
QuestionHow do I right align amout column while printig in a POS Pin
BIBASWAN21-Jun-16 5:25
MemberBIBASWAN21-Jun-16 5:25 
AnswerRe: How do I right align amout column while printig in a POS Pin
Member 1364626525-Jul-19 17:37
MemberMember 1364626525-Jul-19 17:37 
subashcj6-Jan-16 18:18
Membersubashcj6-Jan-16 18:18 
BIBASWAN23-Jun-16 6:42
MemberBIBASWAN23-Jun-16 6:42 
QuestionText Allignment Pin
Praful I Hampannavar3-Jun-15 19:53
MemberPraful I Hampannavar3-Jun-15 19:53 
QuestionMargins at the top and bottom of paper Pin
Member 1160119117-Apr-15 3:33
MemberMember 1160119117-Apr-15 3:33 
QuestionUnicode Pin
Member 1139617521-Mar-15 9:30
MemberMember 1139617521-Mar-15 9:30 
Question"Printer Error" Pin
Ryan Saputra29-Jul-14 22:26
MemberRyan Saputra29-Jul-14 22:26 
AnswerRe: "Printer Error" Pin
BIBASWAN23-Jun-16 6:44
MemberBIBASWAN23-Jun-16 6:44 
Questioncode interference problem Pin
ZiZoU3278-Feb-14 0:16
MemberZiZoU3278-Feb-14 0:16 
AnswerRe: code interference problem Pin
Member 1131773218-Dec-14 2:07
MemberMember 1131773218-Dec-14 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.