Free Barcode Generator for RDLC PDF report using C# and ASP .net and iTextSharp

Thanks to iTextSharp developer Team

Curtsy: http://www.jphellemons.nl/post/Make-a-code128-barcode-with-C-sharp-and-iTextSharp

——————————– My Code —————————-

private System.Drawing.Image CreateBarcode(string data)
{
// iTextShirp
//Bitmap barCode = new Bitmap(1, 1);
Barcode128 code128 = new Barcode128(); // barcode type
code128.Code = data;
System.Drawing.Image barCode = code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);
//barCode.Save(Server.MapPath(“~/barcode.gif”), System.Drawing.Imaging.ImageFormat.Gif); //save file
return barCode;
}

public static byte[] ImageToByte(System.Drawing.Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

// adding new column to datatable and assign value
System.Data.DataColumn newColumn = new System.Data.DataColumn(“barCode”, typeof(System.Byte[]));
newColumn.DefaultValue = ImageToByte(CreateBarcode(APPLICATION_SN));
dt.Columns.Add(newColumn);

————————————————————————-

I have looked for several options and libraries to generate a code128 barcode. It appears that there are three versions of code128

  • Code128A
  • Code128B
  • Code128C

128C is for digits only, 128A is for case insensitive barcodes and 128B is case sensitive. If you want to know more details, I recommend the Wikipedia page of code128.

Somehow other libraries I found did not clearly show the differences between the code128 subtypes. After some Google queries, I found out that the free open source library which I used for generating PDF’s also has capabilities for generating barcodes.

iText# (iTextSharp) is a port of the iText open source java library for PDF generation written entirely in C# for the .NET platform.

[more]So before I show you my code snippets for a generic Code128 barcode, here is the download link for the binary iText# DLL.

http-handlerAfter you have downloaded the DLL and added a reference in your project, I used a HTTP handler for handling the barcodes because I wanted to display the barcode as an image on a webpage.

I have chosen GIF over JPG for displaying barcodes online. It appears to be sharper.

So here is my implementation of the auto generated ProcessRequest method of the HTTP handler:

 

1
2
3
4
5
6
7
8
9
10
11
12
string prodCode = context.Request.QueryString.Get("code");
context.Response.ContentType = "image/gif";
if (prodCode.Length > 0)
{
    Barcode128 code128          = new Barcode128();
    code128.CodeType            = Barcode.CODE128;
    code128.ChecksumText        = true;
    code128.GenerateChecksum    = true;
    code128.Code                = prodCode;
    System.Drawing.Bitmap bm    = new System.Drawing.Bitmap(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White));
    bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);           
}

 

Please do not forget to add iTextSharp and iTextSharp.text.pdf to your usings!

Good luck with your Code128 Barcodes!

code128

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s