How to Print Barcodes in Crystal Reports with VB.NET
Adding barcodes to Crystal Reports can enhance your business processes, making them faster and more accurate, from tracking inventory to generating detailed reports. If you’ve ever tried using font-based barcodes in VB.NET, you know how tricky it can be with the complex formulas, limited formats, and printer issues that can make it frustrating.
Luckily, modern barcode generator SDKs, such as IronBarcode, make creating and printing barcodes straightforward. In this guide, we’ll walk you through everything: setting up IronBarcode in Visual Studio, generating barcodes, and displaying them directly in your Crystal Reports. By the end, you’ll be able to produce professional, reliable barcodes with minimal hassle.
What Are the Methods for Printing Barcodes in Crystal Reports?
Two primary approaches exist for adding barcodes to Crystal Reports: font-based methods and SDK solutions.
Font-based methods require installing special barcode fonts (True Type fonts) in your fonts folder and using formulas or a User Function Library (UFL) to encode data. While this approach works, it often involves complex encoding formulas, limited barcode format support (such as Code 39 or data matrix), and potential font rendering issues across different printers and systems, as discussed in Microsoft's documentation on font rendering.
SDK-based solutions provide a more robust alternative. These libraries generate barcode images programmatically, offering better reliability, extensive format support, and easier implementation. IronBarcode exemplifies this modern approach, allowing developers to generate barcodes as images that integrate seamlessly with Crystal Reports through standard database fields or DataTable objects. This method ensures consistent rendering across all platforms and eliminates font dependency issues when printing a barcode in a Crystal Report using VB.NET.
How Do I Set Up IronBarcode for VB.NET?
Setting up IronBarcode in your VB.NET project requires minimal configuration. First, install the library through NuGet Package Manager by running:
Install-Package BarCode
Alternatively, use the Package Manager UI in Visual Studio to search for and install "IronBarcode". Once installed, import the namespace in your VB.NET code:
Imports IronBarCode
Imports IronBarCode
Ensure your project targets .NET Framework 4.0 or higher, or any version of .NET Core/.NET 5+. IronBarcode works with all Crystal Reports versions that support image fields, making it broadly compatible with existing projects. For detailed setup instructions, refer to the IronBarcode documentation.
Download IronBarcode today to start creating professional barcodes in your Crystal Reports.
How Can I Generate Barcodes and Store them in a Database?
Creating barcodes with IronBarcode and storing them for Crystal Reports involves generating the barcode image and saving it to your database using a new OleDbConnection or SqlConnection. Here's a complete example:
Option 1: Using a Database
Imports IronBarCode
Imports System.Data.SqlClient
Module BarcodeGenerator
Sub CreateAndStoreBarcode()
' Generate a Code128 barcode
Dim myBarcode = BarcodeWriter.CreateBarcode("PROD-12345", BarcodeWriterEncoding.Code128)
' Add readable text below the barcode
myBarcode.AddBarcodeValueTextBelowBarcode()
' Resize to appropriate dimensions and adjust font size
myBarcode.ResizeTo(400, 150)
' Connect to your database (example uses connection string)
Using connection As New SqlConnection("YourConnectionString")
' SQL command to post barcode data
Dim cmd As New SqlCommand("INSERT INTO Products (ProductCode, BarcodeImage) VALUES (@Code, @Image)", connection)
' Add parameters
cmd.Parameters.AddWithValue("@Code", "CUSTOMER-12345")
cmd.Parameters.AddWithValue("@Image", myBarcode.BinaryStream)
' Execute the command
connection.Open()
cmd.ExecuteNonQuery()
End Using
End Sub
End Module
Imports IronBarCode
Imports System.Data.SqlClient
Module BarcodeGenerator
Sub CreateAndStoreBarcode()
' Generate a Code128 barcode
Dim myBarcode = BarcodeWriter.CreateBarcode("PROD-12345", BarcodeWriterEncoding.Code128)
' Add readable text below the barcode
myBarcode.AddBarcodeValueTextBelowBarcode()
' Resize to appropriate dimensions and adjust font size
myBarcode.ResizeTo(400, 150)
' Connect to your database (example uses connection string)
Using connection As New SqlConnection("YourConnectionString")
' SQL command to post barcode data
Dim cmd As New SqlCommand("INSERT INTO Products (ProductCode, BarcodeImage) VALUES (@Code, @Image)", connection)
' Add parameters
cmd.Parameters.AddWithValue("@Code", "CUSTOMER-12345")
cmd.Parameters.AddWithValue("@Image", myBarcode.BinaryStream)
' Execute the command
connection.Open()
cmd.ExecuteNonQuery()
End Using
End Sub
End Module
Connect to your database by creating a new connection using SqlConnection (or new OleDbConnection) and post your barcode data.
Option 2: Using a DataTable
Imports IronBarCode
Imports System.Data
Function CreateBarcodeDataTable() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("ProductCode", GetType(String))
dt.Columns.Add("Barcode", GetType(Byte()))
' Add table row for each product
Dim row As DataRow = dt.NewRow()
row("ProductCode") = "PROD-12345"
' Create barcode on-the-fly and convert to binary
Dim barcode = BarcodeWriter.CreateBarcode("CUSTOMER-12345", BarcodeWriterEncoding.Code128)
barcode.AddBarcodeValueTextBelowBarcode()
barcode.ResizeTo(400, 150)
row("Barcode") = barcode.BinaryStream
dt.Rows.Add(row)
Return dt
End Function
Imports IronBarCode
Imports System.Data
Function CreateBarcodeDataTable() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("ProductCode", GetType(String))
dt.Columns.Add("Barcode", GetType(Byte()))
' Add table row for each product
Dim row As DataRow = dt.NewRow()
row("ProductCode") = "PROD-12345"
' Create barcode on-the-fly and convert to binary
Dim barcode = BarcodeWriter.CreateBarcode("CUSTOMER-12345", BarcodeWriterEncoding.Code128)
barcode.AddBarcodeValueTextBelowBarcode()
barcode.ResizeTo(400, 150)
row("Barcode") = barcode.BinaryStream
dt.Rows.Add(row)
Return dt
End Function
This code generates a Code 128 barcode with the value "PROD-12345". The AddBarcodeValueTextBelowBarcode() method adds human-readable text beneath the barcode. The ResizeTo() method adjusts dimensions to fit your report layout. For more barcode formats, explore the supported barcode types.
How Do I Display Barcodes in Crystal Reports?
Once barcodes are stored in your database, displaying them in Crystal Reports follows standard image field procedures:
- Open Crystal Reports and create a new report or open an existing one
In Field Explorer, right-click "Database Fields" and select "Database Expert"
- Connect to your database and select the table containing barcode images
- Drag the barcode image field onto your report design surface
- Resize and position the field as needed
Your report designer should look similar to this:
For dynamic barcode generation during report runtime, modify your DataTable or database query to generate barcodes on-the-fly:
Function GetReportData() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("ProductCode", GetType(String))
dt.Columns.Add("Barcode", GetType(Byte()))
' Add table row
Dim row As DataRow = dt.NewRow()
row("ProductCode") = "CUSTOMER-12345"
' Create barcode on-the-fly and convert to binary
Dim barcode = BarcodeWriter.CreateBarcode("CUSTOMER-12345", BarcodeWriterEncoding.Code128)
row("Barcode") = barcode.BinaryStream
dt.Rows.Add(row)
Return dt
End Function
Function GetReportData() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("ProductCode", GetType(String))
dt.Columns.Add("Barcode", GetType(Byte()))
' Add table row
Dim row As DataRow = dt.NewRow()
row("ProductCode") = "CUSTOMER-12345"
' Create barcode on-the-fly and convert to binary
Dim barcode = BarcodeWriter.CreateBarcode("CUSTOMER-12345", BarcodeWriterEncoding.Code128)
row("Barcode") = barcode.BinaryStream
dt.Rows.Add(row)
Return dt
End Function
This approach generates barcodes dynamically when the report loads, eliminating the need to pre-store images in the database. You can also convert reports to PDF using Crystal Reports export features. For more information about Crystal Reports data sources, see SAP's Crystal Reports documentation.
Output
Your Crystal Report should display the barcode with readable text and proper font size. Adjust the font, field size, or margins as needed for scanners, printers, or labels. The barcode can now be included in PDF, printed forms, or templates directly from your VB program/project.
What Are Common Issues and Solutions?
Several issues may arise when implementing barcodes in Crystal Reports using VB.NET:
- Blurry barcodes: Enhance resolution with myBarcode.ResizeTo() with larger dimensions.
- Scanning problems: Ensure adequate white space around barcodes or include asterisk markers for Code 39.
- Database connection issues: Check your new OleDbConnection or new OleDbDataAdapter parameters. Ensure proper dataset binding.
- Data type issues: Always use image or varbinary(max) fields for storing barcode binary data.
- Barcodes not displaying: Verify that the Crystal Reports viewer has proper permissions to access the database, DataTable, or file resources.
- System issues: Ensure all required DLLs are installed, and your project references the correct IronBarcode DLL.
- Font or printer problems: Confirm True Type fonts are installed in the fonts folder and configured for your printers.
For additional troubleshooting support, visit the IronBarcode troubleshooting guide.
Conclusion
Implementing barcodes in Crystal Reports using VB.NET and IronBarcode offers a reliable and straightforward solution for report enhancement. This SDK-based approach eliminates font dependencies while offering extensive customization options. Whether using a database or a DataTable, you can implement professional barcodes, including data matrix, Code 39, and other formats in your Crystal Reports application.
Start implementing barcodes in your project today by downloading IronBarcode's free trial to explore its full capabilities for printing barcodes in Crystal Reports using VB.NET.