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

Pin on pinterest Plus on Googleplus Post on LinkedIn

Comments

Comment by Bas

Thank you for this. I was using the GenCode128 library on CodeProject, but that one didn't support CODE 128C. Since I have to print the barcodes to small badges, printing space was limited. You helped me out a lot.

Bas
Comment by JP Hellemons

No problem, glad to hear that this post helped you out :)

Comment by Hemant

I am unable to get Barcode text in the footer or bottom of actual barcode generated by itextsharp 5.0.

I need to show barcode text in the bottom of barcode.

say for example:

code128.Code = "4300050393"
after barcode image I want "4300050393" to be printed just like in the above example we have "Code 128"

This is bit urgernt and thanks in advance!

Hemant
Comment by JP Hellemons

@Hemant, I do not think that it's possible by default. I think that you have to manually make a PdfTable and have two rows and set it to horizontal center

Font fontHeader = new Font(1, 12, 1, Color.BLACK);
Table tp = new Table(1);
// your cell with the barcode image
Cell codeBelow = new Cell(new Phrase("4300050393", fontHeader));
codeBelow.VerticalAlignment = Element.ALIGN_CENTER;
codeBelow.HorizontalAlignment = Element.ALIGN_CENTER;
tp.AddCell(codeBelow);

Comment by tuan

Thank you for this.But I want to draw with lager barcode ( barweight = 2, 3 ...),so how can i do this.
Thank you!

tuan
Comment by JP Hellemons

Hi Tuan,

As Chris points out here stackoverflow.com/.../how-to-resize-barcode
you can resize the image, because it is a vector.

good luck!

ps. the X and Size property of the barcode128 object do not work.

JP Hellemons
Comment by JP Hellemons

@James, you are right, but that costs at least $ 750,-. The way described in this post uses open source.

JP Hellemons
Comment by mark029

Code 128a, Code 128b, and Code 128c are used for representing all 128 ASCII characters. i used a C# Code128 generator from the from page, which has the capability of generating code 128a,code128b, and code128c.
www.keepdynamic.com/.../code-128.shtml

mark029
Comment by john

Hi JP
just wondering if i want code 128A barcode what else i need to change in the code code ? sorry its not clear from the post or iText website. thanks for the advise.

john
Comment by JP Hellemons

Hi John,

On line 6 of the code above, you can change Barcode.CODE128 to Barcode.CODE128_RAW or Barcode.CODE128_UCC but both do not seem to support 128a. itextsharp does not support that, it seems. I am afraid that you might have to move to a commercial library.

good luck!
JP

JP Hellemons
Comment by Fred

Hey JP,

I went to the stackoverflow page for the same question, but turns out there is no solutions yet.

I really love how you have the "Code 128" BarCode, Can you let me know how did you re-size the pic and here is my code:

------------------------------------------------------------------------------------------------------------
[i]code128.Code = _barCodeId;
code128.StartStopText = true;
code128.ChecksumText = true;
code128.GenerateChecksum = true;
code128.Extended = true;
code128.CodeType = Barcode128.CODE128;

// Create a blank image
System.Drawing.Bitmap bmpimg = new Bitmap(120, 35); // provide width and height based on the barcode image to be generated.

Graphics bmpgraphics = Graphics.FromImage(bmpimg);
bmpgraphics.Clear(Color.White); // Provide this, else the background will be black by default

// Generate the code128 barcode
bmpgraphics.DrawImage(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White), new Point(0, 0));

// Generate the text below the barcode image. If you want the placement to be dynamic, calculate the point based on size of the image
bmpgraphics.DrawString(_barCodeId, new System.Drawing.Font("Vernada", 7, FontStyle.Regular), SystemBrushes.WindowText, new Point(15, 24));

// Save the output stream as gif. You can also save it to external file
bmpimg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);[/i]
------------------------------------------------------------------------------------------------------------

And from the code above, I can only have a really tiny BarCode (The above code can produce the text below if someone need this functionality) I understand that we are able to change the height of the BarCode but is there anyway that we can make BarCode just like this one?
[b][u][i]www.jphellemons.nl/image.axd [/i][/u][/b]

Fred
Comment by Fred

Hey JP,

I went to the stackoverflow page for the same question, but turns out there is no solutions yet.

I really love how you have the "Code 128" BarCode, Can you let me know how did you re-size the pic and here is my code:

------------------------------------------------------------------------------------------------------------
[i]code128.Code = _barCodeId;
code128.StartStopText = true;
code128.ChecksumText = true;
code128.GenerateChecksum = true;
code128.Extended = true;
code128.CodeType = Barcode128.CODE128;

// Create a blank image
System.Drawing.Bitmap bmpimg = new Bitmap(120, 35); // provide width and height based on the barcode image to be generated.

Graphics bmpgraphics = Graphics.FromImage(bmpimg);
bmpgraphics.Clear(Color.White); // Provide this, else the background will be black by default

// Generate the code128 barcode
bmpgraphics.DrawImage(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White), new Point(0, 0));

// Generate the text below the barcode image. If you want the placement to be dynamic, calculate the point based on size of the image
bmpgraphics.DrawString(_barCodeId, new System.Drawing.Font("Vernada", 7, FontStyle.Regular), SystemBrushes.WindowText, new Point(15, 24));

// Save the output stream as gif. You can also save it to external file
bmpimg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);[/i]
------------------------------------------------------------------------------------------------------------

And from the code above, I can only have a really tiny BarCode (The above code can produce the text below if someone need this functionality) I understand that we are able to change the height of the BarCode but is there anyway that we can make BarCode just like this one?
[b][u][i]www.jphellemons.nl/image.axd [/i][/u][/b]

Fred