How to Create Barcodes in .NET RDLC Local Reports using Visual C#
Create barcode in RDLC report in .NET application
We have built two step-by-step tutorial guides on how to create barcode in RDLC reports in .NET application.
Please follow one of the above tutorial and build a demo .NET application with barcode printed in the RDLC report. Here we will further demonstrate how to customize the printed barcode in RDLC reports, including:
- Encode various text formats in barcode
- Print barcode with specified image size
- Styling QR Code and barcodes
- Apply image options for printing
How to encode text string in barcode in RDLC report using C#?
Encode ASCII characters
Some barcode formats support full ASCII 128 characters encoding. Here we will show how to create barcode with ASCII characters encoded in RDLC reports.
List of the barcode formats supporting ASCII characters encoding:
- Code 128
- Code 39 extension
- Data Matrix
- PDF417
- QR Code
To create barcode with ASCII characters encoded, you can enter the encoding ASCII chars directly to property
Data in RDLC report using C#.
barcode.Data = "text string";
Encode ASCII control characters
There are few control characters in ASCII character set, which are non-printable. You cannot enter the control characters directly through keyboard. To create barcode with those non-printable chars using C#, you need pass these control character's ASCII integer value (in three digits format) to the C# barcode generator library.
- Enable property
ProcessTilde to true
- Convert ASCII control character to the following format: ~ddd. Here "ddd" is the ASCII integer value in 3 digits.
Set the converted text and other text string to property
Data
barcode.ProcessTilde = true;
barcode.Data = "Code~013128";
Encode Unicode text
Using BarcodeLib RDLC Reports Barcode Generator library, you can directly enter the encoding Unicode text to the barcode property
Data.
List of the barcode formats supporting Unicode encoding:
- QR Code
- Data Matrix
- PDF417
The C# sample code below, we will create three QR Code with Unicode text encoded in RDLC report in ASP.NET web application.
Go to method
Page_Load in the
Default.aspx.cs file, we have added three columns with different
hello in Chinese, Japanese, Thai to the data table. And we will create three QR Code images with Unicode text encoded in the rdlc report.
- In the created
QRCodeRDLC object, enable property IsUnicodeData to true.
The RDLC barcode library will automatically print QR Code using binary data mode with Unicode text encoded in the report.
DataTable dt = new DataTable();
dt.Columns.Add("Website", typeof(String));
dt.Columns.Add("Barcode", typeof(byte[]));
String id1 = "你好 hello in Chinese";
String id2 = "こんにちは hello in Japanese";
String id3 = "สวัสดีค่ะ hello in Thai";
QRCodeRDLC barcode = new QRCodeRDLC();
barcode.ResizeImage = true;
barcode.UOM = UnitOfMeasure.INCH;
barcode.ImageWidth = 2;
barcode.ImageHeight = 2;
barcode.Resolution = 300;
barcode.IsUnicodeData = true;
barcode.ImageFormat = BLImageFormat.PNG;
barcode.Data = id1;
byte[] imgData = barcode.drawBarcodeAsBytes();
dt.Rows.Add(id1, imgData);
// Create data for the 2nd row in the table.
barcode.Data = id2;
imgData = barcode.drawBarcodeAsBytes();
dt.Rows.Add(id2, imgData);
// Create data for the 3rd row in the table.
barcode.Data = id3;
imgData = barcode.drawBarcodeAsBytes();
dt.Rows.Add(id3, imgData);
// Configure Report
ReportDataSource rds = new ReportDataSource("DataSet1", dt);
this.ReportViewer1.LocalReport.DataSources.Clear();
this.ReportViewer1.LocalReport.DataSources.Add(rds);
this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report1.rdlc");
this.ReportViewer1.LocalReport.Refresh();
Run the ASP.NET web application using Visual Studio, and you can view the QR Codes printed in RDLC report in web browser.
How to print barcode with specified image size in RDLC report?
Step 1: Set image item display size option
- Open report file
Report1.rdlc in Design mode
- Right click barcode image item, choose
Image Properties... in the popup menu list.
- Go to Size section, select
Fit to size. Click button OK.
Step 2: Set barcode image item with specified size in RDLC report designer
- Select the barcode image item in the report designer.
- In the image properties window, input the specified image width and height.
Step 3: Customize the barcode generation code with required image size
Go to method
Page_Load in the
Default.aspx.cs file.
- Set the property
UOM with INCH, which is the same value in the RDLC report designer.
- Set the property
ImageWidth and ImageHeight to the same values in the image item size in the designer.
- Set the property
Resolution to a high value for good printing quality
QRCodeRDLC barcode = new QRCodeRDLC();
barcode.UOM = UnitOfMeasure.INCH;
barcode.ImageWidth = 2;
barcode.ImageHeight = 2;
barcode.Resolution = 300;
Preview the printed QR Code image in PDF reader software
We have run the ASP.NET RDLC report application, and export the printed report to PDF file.
You can find that the printed QR Code with 2-inch width and height in the Adobe Acrobat application.
Styling QR Code with colors and logo image in RDLC report
You can easily create and customize QR Code with logo image printed in the RDLC reports.
- Use property
ECL to increate QR Code error correction level to QRCodeErrorCorrectionLevel.Q. Now you have more space to print logo image on the QRCode.
- Set QR Code module color and background color to match the logo style.
- Set property
LogoImage to the logo image object
- Set property
LogoRenderSize with the logo image rendering area size
QRCodeRDLC barcode = new QRCodeRDLC();
barcode.ResizeImage = true;
barcode.UOM = UnitOfMeasure.INCH;
barcode.ImageWidth = 2;
barcode.ImageHeight = 2;
barcode.Resolution = 300;
barcode.ECL = QRCodeErrorCorrectionLevel.Q;
barcode.ModuleColor = Color.LightBlue;
barcode.BackgroundColor = Color.White;
barcode.LogoImage = new System.Drawing.Bitmap("C://Input//facebook.png");
barcode.LogoRenderSize = new System.Drawing.SizeF(150, 150);
Print high quality barcode images in RDLC reports
To create high quality barcode images in reports for printing, you need generate high resolution barcode images.
You can set the barcode image resolution (dpi) through property
Resolution. Make sure the barcode image resolution is higher than your printer's.
barcode.Resolution = 900;
More Online Tutorial for RDLC Local Report Barcoding:
Other .NET Barcode Generator Components