How to create, print barcodes in SAP Crystal Reports for .NET applications?

How to Generate Linear and 2D Barcodes in Crystal Report

Generate, Create, Print, & Draw Linear, 2D Bar Codes in Crystal Reports for .NET

  • Add robust barcodes generation capabilities into Crystal Reports for .NET
  • Print dynamic QR Code, Data Matrix, and PDF-417 in report with built-in features
  • Supports Code 39, Code 128, EAN-128, EAN-8, EAN-13, UPC-A, UPC-E
  • Provide detailed guide and complete code for VB and C# programmers
  • Capable of encoding barcode with JPEG, PNG, BMP, or GIF image file
  • Completely coded in C#.NET by strong named assemblies
  • Compatible with latest barcode symbology ISO Standards
  • Mature & reliable .NET Barcode Generating Components since 2004
Barcode Generator for Crystal Reports for .NET is a .NET component developed to generate 20+ linear & matrix barcode in Crystal Report. The generator is a control library with barcode generation capabilities. The control could be installed and used easily in Crystal Reports for .NET with Xcopy.
Barcode Generator for .NET Crystal Reports is specially designed for the development of Crystal Report for .NET. The integrated barcode generation capabilities could be used to draws high quality graphics object in your reports. The generated barcode is highly customizable.


How to create, print barcodes in Crystal Reports in Windows Forms application?

  1. Download Crystal Reports Barcode Generator library
  2. Install Crystal Reports Barcode Generator dll file into Visual Studio WinForms .NET project
  3. Start to Create










How to create, print barcodes in Crystal Reports in C# Windows applications?

Top

Prerequisites

  • Visual Studio 2022
  • .NET Framework 4.7.2 or higher (supporting .NET Standard 2.0)
  • Crystal Reports for Visual Studio 2022 Developer Edition (Service Pack 35 for x64)
  • Microsoft Visual C++ 2013 Redistributable (x64)




Create new Windows Forms project

Start Visual Studio 2022 and select "Create a new project".


Select Crystal Reports Application in the dialog and then press Next button.


Create a new project with name OnBarcodeCrystalReportsWinFormsDemo


Select Using the Report Wizard option in the Crystal Reports Gallery dialog, and choose Standard in the list box.


Press Finish to end the wizard. The data source would be chosen later.


Now, all auto-generated files could be found in the solution explorer.




Add library and NuGet packages

Right-click "References" in the Solution Explorer, and select "Manage NuGet Packages".


Select "Browse" and use the search control to find OnBarcode.Crystal.Reports from the package source “nuget.org”.


Choose the version 10.0.1 or later to install the package.


Check "References" in the Solution Explorer to ensure the installation is success.




Tutorial 1: How to create barcodes from database in Crystal report using C#?

Top

Add data source

Add a new item "DataSet1.xsd" to the project.




Insert a TableAdapter from Toolbox to "DataSet1.xsd" and configure it with "TableAdapter Configuration Wizard".


Press New Connection to add a data connection to the database.


Set Server name to (LocalDB)\MSSQLLocalDB and select the restored database AdventureWorksLT2019. (Try "Test Connection" to ensure the connection is successful.) Then, press OK to create the connection string.




Note:
Backup file of the Microsoft sample database AdventureWorks Lightweight (2019) can be downloaded from below website. https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver17&tabs=ssms

Check the checkbox to verify the connection string and press Next button to continue.


Save the connection as AdventureWorksLT2019ConnectionString.


Use "Query Builder" to specify a SQL statement to access the database.




Add table "Product (SalesLT)" in the database and select two fields "ProductID" and "Name"; and then, press OK to finish.




Verify the SQL statement and Finish the wizard.


Add a new column to the data table "Product" with column name "Barcode" and set its property Data Type to System.Byte[].




Finally, the data table "Product" contains three columns as below.




Design Crystal report template

Double click "CrystalReport1.rpt" in the Solution Explorer to open the report designer.


Right click the report to open the menu and choose "Database Expert".


Choose "Product" in ADO.NET DataSets.


Drag fields "ProductID", "Name" and "Barcode" to the report as below to display.




Adjust printed barcode image size

Open file CrystalReport1.rpt in Visual Studio

Select the barcode object in the report, and change the following properties.
  • Set XScaling to 5
  • Set YScaling to 2


Right click barcode object, and choose Format Object. Go to tab Picture.

Make sure the size settings are correct.





Add control and behind C# code to the Form

Double click "Form1.cs" to open the designer and add a ProductTableAdapter control with default name "productTableAdapter1" to the form.


Then, add a DataSet1 with default name "dataSet11".


Select "View Code" from the popup menu to open the Form1.cs window.


Replace ALL contents in the Form1.cs file by following codes.

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Windows.Forms;
using OnBarcode.Barcode.CrystalReports;

namespace OnBarcodeCrystalReportsWinFormsDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.productTableAdapter1.Fill(this.dataSet11.Product);
            Linear linear = new Linear();
            linear.Type = BarcodeType.CODE128;
            linear.UOM = UnitOfMeasure.INCH;
            linear.BarcodeWidth = 5;
            linear.BarcodeHeight = 2;
            linear.AutoResizePaddingTransparent = false;
            foreach (DataSet1.ProductRow row in this.dataSet11.Product.Rows)
            {
                linear.Data = row.ProductID.ToString();
                linear.OutputFileFormat = FileFormat.PNG;
                row.Barcode = linear.drawBarcodeAsBytes();
            }
            // Create Report
            CrystalReport1 rpt = new CrystalReport1();
            // Binding DataTable
            rpt.SetDataSource(this.dataSet11);
            // Binding Report
            this.crystalReportViewer1.ReportSource = rpt;
        }
    }
}


Select the Form and switch to Event tab in the Properties window, and then add Load event "Form1_Load".




Run the Crystal Reports report in Windows Forms application

It is done. Now press Ctrl+F5 to run the project.


Screenshot for the Crystal Reports report demo with barcodes printed in .NET Windows Forms application.






Tutorial 2: How to create barcodes from DataTable in Crystal report using C#?

Top

Add data source

Add a new item "DataSet1.xsd" to the project.




Add a new data table to "DataSet1.xsd". By default, the table name is "DataTable1"


Add a new column to the data table "DataTable1" with column name "ID" and set its property Data Type to System.String.




Add another column to the table with name "Barcode" and set its property Data Type to System.Byte[].


Finally, the data table "DataTable1" contains two columns as below.




Design report layout

Double click "CrystalReport1.rpt" in the Solution Explorer to open the report designer.


Right click the report to open the menu and choose "Database Expert".


Choose "DataTable1" in ADO.NET DataSets.


Drag fields "ID" and "Barcode" to the report as below to display.




Adjust printed barcode image size

Open file CrystalReport1.rpt in Visual Studio

Select the barcode object in the report, and change the following properties.
  • Set XScaling to 5
  • Set YScaling to 2


Right click barcode object, and choose Format Object. Go to tab Picture.

Make sure the size settings are correct.





Add Form's event

Add source code for Form1's Load event

Form1.cs
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Windows.Forms;
using OnBarcode.Barcode.CrystalReports;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OnBarcodeCrystalReportsWinFormsDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //  Prepare report data.
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(String));
            dt.Columns.Add("Barcode", typeof(byte[]));

            String id1 = "000001";
            String id2 = "000002";

            //  Create data for the 1st row in the table.
            Linear linear = new Linear();
            linear.Type = BarcodeType.CODE128;
            linear.UOM = UnitOfMeasure.INCH;
            linear.BarcodeWidth = 5;
            linear.BarcodeHeight = 2;
            linear.AutoResizePaddingTransparent = false;
            linear.OutputFileFormat = FileFormat.PNG;

            linear.Data = id1;
            byte[] imgData = linear.drawBarcodeAsBytes();
            dt.Rows.Add(id1, imgData);

            //  Create data for the 2nd row in the table.
            linear.Data = id2;
            imgData = linear.drawBarcodeAsBytes();
            dt.Rows.Add(id2, imgData);

            //  Create Report
            CrystalReport1 rpt = new CrystalReport1();
            //  Binding DataTable
            rpt.SetDataSource(dt);
            //  Binding Report
            this.crystalReportViewer1.ReportSource = rpt;
        }
    }
}


Select the Form and switch to Event tab in the Properties window, and then add Load event "Form1_Load".




Run the Crystal report with barcode in Windows Forms application

It is done. Now press Ctrl+F5 to run the project.


Screenshot for the Crystal Reports report demo with barcodes printed in .NET Windows Forms application.




































Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.