ByteScout BarCode Generator SDK - VB6 - Printing barcodes - ByteScout
Announcement
Our ByteScout SDK products are sunsetting as we focus on expanding new solutions.
Learn More Open modal
Close modal
Announcement Important Update
ByteScout SDK Sunsetting Notice
Our ByteScout SDK products are sunsetting as we focus on our new & improved solutions. Thank you for being part of our journey, and we look forward to supporting you in this next chapter!

ByteScout BarCode Generator SDK – VB6 – Printing barcodes

  • Home
  • /
  • Articles
  • /
  • ByteScout BarCode Generator SDK – VB6 – Printing barcodes

ByteScout BarCode Generator SDK – VB6 – Printing barcodes

Form1.frm

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
VERSION 5.00
Begin VB.Form Form1
   AutoRedraw      =   -1  'True
   Caption         =   "Form1"
   ClientHeight    =   2175
   ClientLeft      =   45
   ClientTop       =   435
   ClientWidth     =   7770
   LinkTopic       =   "Form1"
   ScaleHeight     =   2175
   ScaleWidth      =   7770
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command1
      Caption         =   "Print Barcode to the selected printer with 300 DPI quality and physycal size (4x1 inches)"
      Height          =   855
      Left            =   120
      TabIndex        =   1
      Top             =   840
      Width           =   6975
   End
   Begin VB.ComboBox cboPrinterList
      Height          =   315
      Left            =   120
      TabIndex        =   0
      Top             =   360
      Width           =   6975
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
 
Private Sub Command1_Click()
  ' set printer to the selected printer
  Call SelectPrinter(cboPrinterList.Text)
  ' call function to print barcode to Printer object
 Call PrintBarCode
End Sub
 
  
' populate list of printers on form load
Private Sub Form_Load()
 
Dim dev As Printer, Index As Integer, CurrentPrinter As Integer
CurrentPrinter = -1
Index = 0
For Each dev In Printers
cboPrinterList.AddItem dev.DeviceName
If Printer.DeviceName = dev.DeviceName Then
CurrentPrinter = Index
End If
       Index = Index + 1
Next
If CurrentPrinter <> -1 Then
 cboPrinterList.ListIndex = CurrentPrinter
End If
 
End Sub
  
'===========================
'Function to select printer
'===========================
 
Function SelectPrinter(ByVal printer_name As String) As Boolean
    Dim i As Integer
 
    SelectPrinter = False
    For i = 0 To Printers.Count - 1
        ' if the specified printer is found, select it and return True
        If Printers(i).DeviceName = printer_name Then
            Set Printer = Printers(i)
            SelectPrinter = True
            Exit For
        End If
    Next i
End Function
 
 
' print barcode to the Printer object
 
Private Sub PrintBarCode()
  
' this method prints a barcode via temporary EMF metafile allowing to set target physical size and DPI resolution quality
 
Const DPIQuality = 300 ' 300 DPI as printing resolution (DPI) quality
Const WidthInInches = 4 ' 4 inches as width for the barcode generated
Const HeightInInches = 1 ' 1 inches as height for the barcode generated
 
 ' create barcode object as ActiveX
 Dim BC
 Set BC = CreateObject("Bytescout.BarCode.Barcode")
 
 Printer.PrintQuality = DPIQuality ' set the printer quality to 300 dpi
 
 ' IMPORTANT: call Printer.Print Space(1) to initialize the Printer
 ' this is how Microsoft advises to do in their KB article (search for Printer.Print string): http://support.microsoft.com/kb/146022
 Printer.Print Space(1) ' initialize hDC of Printer object
  
 ' set the same DPI resolution (300 DPI) to generate barcode image
 BC.ResolutionX = DPIQuality
 BC.ResolutionY = DPIQuality
  
 ' set barcode symbology type
 BC.Symbology = 0 ' Code 39 = 0 (see the documentation for other barcode types)
  
 ' set barcode value
 BC.Value = "1234567890"
  
  
 ' use .FitInto_3 method to set the size in inches (you can also set in mm, cm, pixels - see the documentation for more)
 BC.FitInto_3 WidthInInches, HeightInInches, 2 ' fit into 3.5x0.875 inches (2 = inch)
   
' save barcode into temprorary EMF image
 
' create FileSystemObject to work with files
Dim FSO As Object
Set FSO = CreateObject("Scripting.filesystemobject")
 
' get temporary filename to save a temporary .EMF file
Dim tempEMFFileName
tempEMFFileName = FSO.GetTempName() & ".EMF"
 
' save EMF metafile into the temporary file
BC.SaveImage tempEMFFileName
 
' now we paint EMF file to Printer object
' default units for Printer in VB6 is Twips, which is 1440 per Inch
' so we calculate 3.5 inches as 3.5 * TwipsPerInch
Const TwipsPerInch = 1440
' now printing the picture at 0,0 with 3.5 inches X 0.875 inches
Printer.PaintPicture LoadPicture(tempEMFFileName), 0, 200, 3.5 * TwipsPerInch, 0.875 * TwipsPerInch
 
' now remove temporary EMF file
FSO.deletefile tempEMFFileName, True
Set FSO = Nothing
  
 ' finally send command to print the page
Printer.EndDoc
  
Set BC = Nothing
 
 
End Sub

  Click here to get your Free Trial version of the SDK

Tutorials:

prev
next