Visual Basic Barcode Integration Guide
Integrate barcoding into VB 6 and Visual Basic .NET
IDAutomation provides several products and options for
implementing barcodes in VB with
flexible license
agreements to meet a variety of needs including single user licenses
and royalty-free developer licenses. The following chart may be used
to determine the best product needed:
Self-Checking Fonts in VB 6:
Self-Checking Barcode
Fonts (including
MICR and
OCR fonts) are
compatible with Visual Basic and may be used with ease. To use a self-checking
font such as Code
39, insert an asterisk "*" before and after the data encoded. Therefore,
to encode TEST3OF9 in a barcode, the text of *TEST3OF9*
would need to be present in a field with the Code 39 font selected.
When using the
Codabar Font for numbers only, use the 'A' character instead of
the asterisks.
The following is a VB 6 example of printing the
Code 39 barcode
font from Visual Basic, where variable is a string
to encode in the barcode:
Printer.FontName = "IDAutomationHC39M"
Printer.CurrentY = 3000
Printer.FontSize = 12
Printer.Print "*" & variable & "*"
Printer.FontSize = 10
Printer.FontName = "Helv" 'Switch to a text font
Printer.Print "The above barcode is code-39"
Printer.EndDoc 'This tells the printer to eject the page
Barcode may also be printed from a field in a database via the
data control, for example:
Printer.Print "*" & Data1.Recordset("field1")
& "*"
The start and stop characters may also be appended to the variable,
for example:
variable ="*" & DataString & "*"
Printer.Print variable
When printing with Crystal Reports, refer to the
Crystal Reports Integration
Guide for the best implementation method.
Data is easily formatted to the barcode font by appending asterisks
to the beginning and ending of the data being encoded; no other DLLs
or source code are needed. When using the
Codabar Barcode
Font for numbers only, use the 'A' character instead of asterisks.
The following source code is an example of printing a barcode from VB.NET
with the Code 39
Barcode Font, where VariableToPrintis a string that needs to be encoded:
Import the System.Drawing.Printing class and define a class level variable:
Imports System.Drawing.Printing
Private PrintFont As Font
'Class level variable.
private VariableToPrint as string
'class level variable representing the data to print
Place code in an object event to generate the page(s) to be printing:
'****Code within the object generating the print,
such as a toolbar button...
'Initialize the Print Document object that will be sent to the printer
Dim PrintDoc As New PrintDocument()
Dim VariableToPrint as string
'variable that we are printing
'Set the font of the font object and the data
to encode
PrintFont = New Font("IDAutomationHC39L", 12)
VariableToPrint = "*" & "128-39-200-128" & "*"
'Add an event handler that will be called for
each page that is generated
AddHandler PrintDoc.PrintPage, AddressOf Me.PrintDoPrintPage
'Initiate the printing of the document
PrintDoc.Print()
'****
The following code should be placed in the event handler, which
places the data on the page:
Dim linesPerPage As Single = 0
'Number of lines (bar codes) that you will be able to fit on the page
Dim yPos As Single = 0
'yPos on the page of the bar code
Dim leftMargin As Single = ev.MarginBounds.Left 'left
margin of bar code
Dim topMargin As Single = ev.MarginBounds.Top 'top margin
of bar code
Dim line As String = Nothing
'String that font will be applied to and printed
'Calculate the number of lines to print
per page that we can fit based on the
'height of the font
linesPerPage = ev.MarginBounds.Height / PrintFont.GetHeight(ev.Graphics)
'Set the y position of the font (how
far down on the page it prints
yPos = 100
'Set the text that we will print
line = VariableToPrint
'Send the string to the printer. An
empty string format variable is passed in. This
'variable can do any formatting on the string that is necessary
ev.Graphics.DrawString(line, PrintFont, Brushes.Black, leftMargin, yPos,
New StringFormat())
When printing with Crystal Reports, refer to the
Crystal Reports Integration
Guide for the best implementation method.
Visual Basic Barcode Font Module Integration
The
IDAutomation VB Font Module (or source code from the module) calculates
the start, stop and check digits for the barcode and returns a text
string, that when combined with the appropriate
IDAutomation
Barcode Font, creates a correct barcode. VB developers may add
this module to their Visual Basic applications and access the functions
directly. Using this module, there is nothing to distribute with the
application except the barcode font because the functions of the module
are compiled into the EXE file.
Note: Depending on the method used, a parameter may be required.
For example, Code128("123456",0,True).
|
VB Functions for Standard
Barcode Fonts (VB 6 & VB .NET) |
|
Barcode Type |
Method(s) & Notes |
Font to use |
|
Code-11 |
Code11
(DataToEncode) |
IDAutomationC11 |
Code-128
(Auto Mode) |
Code128 (DataToEncode,
C128 ReturnType,
ApplyTilde)
Human Readable text is enabled when ReturnType = 6
Example: Code128("123456789012", 6, 0) |
IDAutomationC128 |
Code-128
(Manual Mode) |
IDAutomation recommends using Auto
Mode for most situations.
Code128a (DataToEncode,
C128 ReturnType)
Code128b (DataToEncode,
C128 ReturnType)
Code128c (DataToEncode,
C128 ReturnType)
Example: Code128b("12345678", 6) |
IDAutomationC128 |
| Code-39 |
Code39
(DataToEncode)
Code39Mod43 (DataToEncode,
ReturnType)
Example: Code39Mod43("12345678", 0) |
IDAutomationC39 |
|
Code-93 |
Code93
(DataToEncode) |
IDAutomationC93 |
|
Codabar |
Codabar
(DataToEncode) |
IDAutomationCB |
| EAN-13 |
IDAEAN13
(DataToEncode) |
IDAutomationUPCEAN |
| EAN-8 |
IDAEAN8
(DataToEncode) |
IDAutomationUPCEAN |
|
GS1-128 |
Code128 (DataToEncode,
C128 ReturnType,
1)
GS1-128 is enabled in Code 128 Auto by setting
ApplyTilde to True. Human Readable
AIs may be created by setting the C128 ReturnType to 6.
Example: Code128("(12)3456789012", 6,
1) |
IDAutomationC128 |
| Interleaved
2 of 5 |
I2of5 (DataToEncode)
I2of5Mod10 (DataToEncode,
ReturnType)
Example: I2of5Mod10("123456789", 1) |
IDAutomationI25 |
| MSI
/ Plessey |
MSI
(DataToEncode,
ReturnType) |
IDAutomationMSI |
|
RM4SCC |
RM4SCC
(DataToEncode) |
IDAutomationRM |
| UPC-A |
UPCa
(DataToEncode) |
IDAutomationUPCEAN |
| UPC-E |
UPCe
(DataToEncode) |
IDAutomationUPCEAN |
|
USPS IntelligentMail |
IntelligentMail (DataToEncode)
** |
IDAutomationPOSTNET
or
IDAutomation_Uni |
|
USPS Postnet |
Postnet (DataToEncode,
ReturnType) |
IDAutomationPOSTNET |
|
USPS Planet |
Planet (DataToEncode,
ReturnType) |
IDAutomationPOSTNET |
- Install the desired
IDAutomation
Barcode Font package. If unsure of which is needed, IDAutomation
recommends the
Code 128 Font
Advantage Package due to its variety of fonts in differing heights
and automatic encoding of all letters and numbers.
-
Download the Visual Basic Barcode Font Module and extract the
IDAutomation.bas file into the VB project directory.
- Open Visual Basic; Choose Project - Add Module.
- Select the IDAutomation.bas file.
- Now that the module is part of the project, the methods of the
module used to format text to the barcode fonts may be called. For
example:
Printer.FontName = "IDAutomationC128L"
Printer.CurrentY = 3000
Printer.FontSize = 16
Printer.Print Code128(Data1.Recordset("field1"), 0)
Printer.FontSize = 10
Printer.FontName = "Helv" 'Switch back to a text font
Printer.Print "The above barcode was printed with the IDAutomation
Code 128 Font"
Printer.EndDoc 'This tells the printer to eject the page
- Install the desired
IDAutomation
Barcode Font package. If unsure of which is needed, IDAutomation
recommends the
Code 128 Font
Advantage Package due to its variety of fonts in differing heights
and automatic encoding of all letters and numbers.
-
Download the Visual Basic Barcode Font Module and extract the
IDAutomation.vb file into the VB project directory.
- In Visual Basic .NET, Choose Project - Add Existing
Item.

- Select the IDAutomation.vb file. After adding
the module, it should show up in the IDE.

- Now that the module is part of the project, the functions of
the module may be used to format text to
IDAutomation
Barcode Fonts. For example:
TextVariable = Code128("Code 128 Font Test",
0) returns a character string, that when combined with
the bar code font, creates a correct barcode.
- The following code example prints a bar code in VB.NET:
Import the System.Drawing.Printing class and define a class level
variable:
Imports System.Drawing.Printing
Private PrintFont As
Font
'Class level variable.
private VariableToPrint as string
'Class level variable representing the data to print
Place code in an object event to generate the page(s) to
be printed:
'****Code within the object generating the
print, such as a toolbar button...
'Initialize the Print Document
object that will be sent to the printer
Dim PrintDoc As New PrintDocument()
Dim VariableToPrint as string
'variable that we are printing
'Set the font of the font object and the
text to encode
PrintFont = New Font("IDAutomationHC39M", 12)
VariableToPrint = "*" & "12827384736388" & "*"
'Add an event handler that will be called
for each page that is generated
AddHandler PrintDoc.PrintPage, AddressOf Me.PrintDoPrintPage
'Initiate the printing of the document
PrintDoc.Print()
'****
The following code should be placed in the event handler,
which places the data on the page:
Dim linesPerPage As Single = 0
'Number of lines (bar codes) that you will be able to fit on the
page
Dim yPos As Single = 0
'yPos on the page of the bar code
Dim leftMargin As Single = ev.MarginBounds.Left
'left margin of bar code
Dim topMargin As Single = ev.MarginBounds.Top 'top margin
of bar code
Dim line As String = Nothing
'String that font will be applied to and printed
'Calculate the number of lines to
print per page that we can fit based on the
'height of the font
linesPerPage = ev.MarginBounds.Height / PrintFont.GetHeight(ev.Graphics)
'Set the y position of the font
(how far down on the page it prints
yPos = 100
'Set the text that will print
line = VariableToPrint
'Send the string to the printer.
An empty string format variable is passed in. This
'variable can do any formatting on the string that is necessary
ev.Graphics.DrawString(line, PrintFont, Brushes.Black, leftMargin,
yPos, New StringFormat())
ActiveX Control Integration with VB.NET
The IDAutomation.com
ActiveX barcode controls allow generation of Windows-Enhanced Metafile
images and printing from Visual Studio.NET. IDAutomation recommends
using .NET Forms
Barcode Controls instead of ActiveX if possible.
- Purchase
or download
and install the ActiveX Control.
- Open a VB .NET Project and choose Project | Add Reference.

- Select the appropriate IDAutomation Barcode Component from the
COM components tab of the reference list. Click Select. Click OK.

- The following code examples use the IDAutomation.com Linear
Barcode ActiveX control in the object named axBarCode1 to print
to the default printer:
Private Sub cmdPrintToPrinter_Click(ByVal
sender As System.Object, ByVal e As System.EventArgs) Handles
cmdPrintToPrinter.Click
Dim pd As New
PrintDocument()
'Add an Event Handler for printing
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
'Fire off
the print page event.
pd.Print()
End Sub
'Event handler for the PrintDocument
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
'Left Margin of Page
Dim topMargin As Single = ev.MarginBounds.Top
'Top Margin of Page.
Dim line As String = Nothing
'Data that we will write to the printer
' Calculate the number of lines per page based on the height
of the font that was sent.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
' Print each line of the file.
line = "IDAutomation.com, Inc. VB Dot Net Example"
'Set the y co-ordinate of where we are going to print the Line
yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
'Send the string to the printer
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin,
yPos, New StringFormat())
'Increase the line count
count += 1
'Set the y position of the next line
yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
'Set the second string to be printed. We have to account for
the actual printing position of the
'bar code when displaying yPos in this line. yPos set above is
actually the location that this line
'will be printed on.
line = "Printing the Enhanced Metafile version of the barcode
at X=" & leftMargin & ", Y=" & yPos + (topMargin + ((count + 1)
* printFont.GetHeight(ev.Graphics)))
'Print the string
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin,
yPos, New StringFormat())
'Increase the line count
count += 1
'Increase the ypos
yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
'Print the enhanced metafile image of the bar code
ev.Graphics.DrawImage(AxBarCode1.GetEnhWMF, leftMargin, yPos)
End Sub
An image may also be saved the a local hard drive with the
following code:
Private Sub cmdSaveToFile_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles cmdSaveToFile.Click
AxBarCode1.SaveBarCode("C:\SavedBarCode.wmf")
End Sub
Copying the image to the clipboard is only supported with IDAutomation's
.NET Forms
Controls. When printing with Crystal Reports, refer to the
Crystal Reports Integration
Guide for the best implementation method.
Back to Top
|