C# Barcode Generator > Crystal Report Barcode > Print Barcode in C# > How to print barcode in Crystal Reports in ASP.NET and WinForms application?

How to Generate Barcode in Crystal Report using C#

Crystal Reports Barcode Generator Library

QR Code, Data Matrix, Code 128, GS1, EAN/UPC. Unicode text encoding in C# Crystal Reports ASP.NET, Windows application.

Download Now
NuGet Ready
.NET Framework 4.x, 3.x, 2.x


Install, setup barcode library in .NET projects

We have prepared detailed step-by-step instructions how to create, print barcodes in Crystal Reports report in ASP.NET and Windows Forms application.



How to Print Barcodes in Crystal Reports in .NET application?

The above ASP.NET and WinForms demo application will create and print Code 128 barcodes in the report. To generate QR Code, Data Matrix, EAN/UPC barcodes in the report, you need do the following code modification.
    • For ASP.NET web application, open file Default.aspx.cs in Visual Studio, go to method Page_Load
    • For WinForms application, open file Form1.cs in Visual Studio, go to method Form1_Load
    • For 1D (EAN/UPC, GS1-128, Code 39, Intelligent Mail) barcode generation:
      Change property Type with the generating barcode format. For example, the following C# code will print EAN-13 barcodes in the report.
      barcode.Type = BarcodeType.EAN13;
      

    • For QR Code, Data Matrix, PDF417 2D barcode generation:
      Create new barcode object from class QRCodeCrystal, DataMatrixCrystal, and PDF417Crystal.
  1. Set barcode image size in Crystal Reports report designer.
    • Open report file CrystalReport1.rpt in Visual Studio. Click the Barcode item, change the properties:
    • Property XScaling value to the same value in barcode property ImageWidth
    • Property YScaling value to the same value in barcode property ImageHeight
Note: In Crystal Reports the default unit of measure is inch. During barcode generation, we recommend you to set property UOM (barcod image unit of measure) to value UnitOfMeasure.INCH also.
Here is a sample report with EAN-13 barcodes printed in web browser.



How to print barcodes with various text encoding?

In this section, we will explain how to print barcode with various text data encoded. It covers ASCII characters, Unicode text, GS1 text message and binary data barcode encoding in Crystal Reports.


Encode barcode with ASCII characters

All 2d barcodes (QR Code, Data Matrix, PDF417), Code 128 and Code 39 extension mode support full ASCII table 128 characters. Some of them are control characters, you cannot input them directly to property Data.

Here is the sample code to print barcode with ASCII control chars in report.
  • Enable property ProcessTilde to true. The barcode library will process and convert the 3-digit integer behide the tilde char (~) to ASCII char.
  • Pass the ASCII chars to property Data. Each ASCII control chars will use it's ASCII integer value in 3-digits and add a tilde char (~) in beginning.
For example, ASCII control char CR's ASCII table integer value is 13. To encode the char in barcode, you need convert it to string ~013.

barcode.ProcessTilde = true;
barcode.Data = "Code~013128";



Encode 2D barcode with Unicode text

You can create, print 2d barcodes (QR Code, Data Matrix, PDF417) with unicode text encoded in the report.

To encode the unicode text in the 2d barcodes, apply the following barcode properties:
  • Set property IsUnicodeData to true to enable unicode text encoding in the 2d barcodes.
  • Set property UnicodeMode to unicode text binary encoding mode.
    UTF8 (default) is the most common encoding mode, and supported by the most mobile barcode scanners.
    Available options are: UTF8, UTF7, UTF32, Unicode, UnicodeBE.
  • Set property Data with the encoding unicode text string.
QRCodeCrystal barcode = new QRCodeCrystal();
barcode.IsUnicodeData = true;
barcode.UnicodeMode = DataUnicodeMode.UTF8;
barcode.Data = "unicode text here";


Encode GS1 barcode with GS1 text message

To generate GS1 2d barcodes, you need apply the following settings for GS1 QR Code and GS1 Data Matrix.
  • Set property FNC1Mode with value FirstPosition
QRCodeCrystal barcode = new QRCodeCrystal();
barcode.FNC1Mode = QRCodeFNC1Mode.FirstPosition;

DataMatrixCrystal bar = new DataMatrixCrystal();
bar.FNC1Mode = DataMatrixFNC1Mode.FirstPosition;


The valid GS1 text formats for GS1-128, GS1 QR Code, and GS1 Data Matrix
  • In the Data property, include AI codes in parentheses followed by its data.
barcode.Data = "(17)050101(10)ABC123";

The image below shows two GS1-128 barcode images printed in the Crystal Reports report in web browser.



Encode 2D barcode with binary data

All 2d barcodes (QR Code, Data Matrix, PDF417) support binary data encoding. To create 2d barcodes with binary data encoded, apply the following settings:
  • Set barcode encoding mode as byte. For QR Code, the value is QRCodeEncoding.Byte.
  • Set property ProcessTilde to true to encode binary data.
  • Call the method BarcodeUtils.ByteArrayToString() to convert your raw byte array into a formatted string.
  • Assign the converted string value to the Data property.
QRCode barcode = new QRCode();
barcode.Encoding = QRCodeEncoding.Byte;
barcode.ProcessTilde = true;

String message = "https://www.barcodelib.com";
byte[] bytes = Encoding.UTF8.GetBytes(message);
barcode.Data = BarcodeUtils.ByteArrayToString(bytes);


Adjust Barcode Size in Crystal Reports

Using BarcodeLib Crystal Reports Barcode Generator library, you can easily create and print barcode with specified image width and height in .NET projects.

Here we will create and customize barcodes with 4-inch width and 1.5-inch height in the Crystal Reports report in .NET project.
  1. Specify and customize the barcode image width and height in barcode property settings.
    barcode.UOM = UnitOfMeasure.INCH;
    barcode.ImageWidth = 4;
    barcode.ImageHeight = 1.5f;
    
  2. Click the barcode item in the report designer, set the properties
    • Set XScaling to 4
    • Set YScaling to 1.5
  3. Verify the image size setting in report.
    • Right-click the barcode item in the report designer, choose Format Object
    • Click tab Picture.
    • You can see the modified barcode image width and height.
Note: Crystal Reports supports the following units of measurement: pixels, inches, centimeters. When setting up barcodes, it is recommended to use the same units of measurement as those used in the report. In the barcode UOM parameter settings, use these parameter values to correspond: PIXEL, INCH, CM.


Advanced Barcode Customizations in Report

BarcodeLib Crystal Reports Barcode library supports lots of QR Code, and barcode customizations, including:
  • Linear barcode text label
  • Barcode bar/space modules, text, colors
  • Add-on symbol
  • Barcode rotations
  • Image settings


1D barcode text label

You can quickly customize the linear barcode (Code 128, GS1-128, EAN/UPC) text label using Crystal Reports barcode library.
  • Set property ResizeText to false to manually set the 1d barcode text label font size.
  • Set property TextFont with the new Font data. Make sure the font file is installed on the system.
  • Set property TextMargin to control the space between bars and the text label.
barcode.ResizeText = false;
barcode.TextFont = new Font("OCR-B", 38, FontStyle.Regular);
barcode.TextMargin = 0;



Barcode colors

You can easily print, customize barcode bar, space module colors and text label color using property BarColor, BackgroundColor, and TextFontColor.
barcode.BarColor = Color.Red;
barcode.BackgroundColor = Color.White;
barcode.TextFontColor = Color.Red;



Barcode rotation

    • Set barcode property Rotate to apply the barcode rotation. There are four options to choose:
      BottomFacingDown, BottomFacingRight, BottomFacingUp, BottomFacingLeft.
    barcode.UOM = UnitOfMeasure.INCH;
    barcode.ImageWidth = 4;
    barcode.ImageHeight = 1.5f;
    barcode.Rotate = RotateOrientation.BottomFacingRight;
    
  1. Modify the barcode XScaling, YScaling values in report designer, if the barcode is vertical printed.
Run the ASP.NET web application, you will view the rotated barcodes in the report in browser.



Image settings

Using Crystal Reports barcode library, you can print high quality barcode images (high resolution dpi) in report and barcode image with transparnt background.
  • Set property Resolution to specify the printed barcode dpi. To print hight quality barcode images, the resolution value should be the same or higher than the printer's.
  • Set property BackgroundColor with the value Color.Transparent to create barcode image with transparent background.
barcode.Resolution = 900;
barcode.BackgroundColor = Color.Transparent;




Other Related .NET Barcode Generator SDKs