12 Comments
  •   Posted in: 
  • C#

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:

 

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


kick it on DotNetKicks.com Shout it

Post on LinkedIn