Generating Barcodes in ASP.NET (C#)
This tutorial is a very simple way of generating barcodes in ASP.NET in four lines of code! My code is in C# but im sure converting to VB will take only a few moments. Using iTextSharp to generate barcodes as images.

Generating Barcodes in ASP.NET

This tutorial is a very simple way of generating barcodes in ASP.NET in four lines of code!  My code is in C# but im sure converting to VB will take only a few moments.  Using iTextSharp to generate barcodes as images.

I was recently working on a project that required developing a way to output and print out barcodes.  It is a web application written in c#.  There are loads of commercial barcode generators many of which work fine, however one in particular (iTextSharp) stood out and was very easy to get up and running.  So in this tutorial ill show you how to generate barcodes very easily with just a few lines of code.  

What you’ll need

The library that has the barcode functions built in is actually one that generates on the fly PDF files, iTextSharp which is a c# implementation of a Java based PDF generator iText.  The first thing you need to do is download the files and copy them into your /bin directory within your application.  You can download the files from http://sourceforge.net/projects/itextsharp/

Step1.  Install the Library

Copy Your iTextSharp.dll into your applications /bin folder

Step 2. Add a reference to the library in your code behind

using iTextSharp;

using iTextSharp.text;

using iTextSharp.text.pdf;

Step 3. Choose your barcode type and initialise the object

Barcode128 code128 = new Barcode128(); // barcode type

Step 4.  Attach some data to the barcode

code128.Code = "Barcode Test";

Step 5. Save Your barcode as an image

        System.Drawing.Image bc = code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);

       bc.Save(Server.MapPath("~/barcode.gif"), System.Drawing.Imaging.ImageFormat.Gif); //save file

Thats it!  You can choose various types of barcodes, and adjust the settings such as the height of the barcode that gets produced and line widths.  In this example, I simply save the image as a file.  If you want to change the format e.g. to a PNG of JPEG you can simply changed the ImageFormat and your done.

10/17/2011 2:03:48 PM | By Chris
thanks for this code I found it very useful as I needed to create bar codes for my project
Add Comment
Your Name
Your Comment
Your Email Address
Your Website Address
Add Comment
Sponsors