Click here to Skip to main content
15,892,804 members
Articles / Programming Languages / C#
Tip/Trick

Generating Barcode for a Sale/Purchase System Using itextSharp

Rate me:
Please Sign up or sign in to vote.
4.69/5 (9 votes)
4 Jul 2013CPOL1 min read 54.5K   7.7K   26   10
Generating barcodes for a sale/purchase system for the items to stick on.

Sample Image

Introduction

Recently I was developing a sale/purchase system in which the requirement was to stick a barcode on each garment that contains different information about the item, purchase price, sale price, etc. I searched the internet and finally found code for generating a barcode with itexsharp. In today's business environment, staying competitive is critical to your success. Bar code data-collection technology is an effective way to improve the bottom line and meet the competitive challenges your organization faces every day.

Background

  • Integrating different information or ID of product/employee in the barcode.
  • Readable by barcode data-capture technology.
  • I found it very useful in sale/purchase and employee card generation system.

Using the code

Download the open source library from sourceforge.net or the source code of the article contains the library. Create the Windows Forms application in Visual Studio. Add a button to your Windows form. Add a reference of itextSharp.dll in reference.

Add the following code to your form.cs:

Import the following namespaces:

C#
using iTextSharp.text;
using iTextSharp.text.pdf;

Add the following code in the button_Click event:

C#
private void button1_Click(object sender, EventArgs e)
{
    Document doc = new Document(new iTextSharp.text.Rectangle(24, 12),5, 5, 1, 1);
  
    try
    {

        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(
          Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/codes.pdf", FileMode.Create));   
        doc.Open();
        
        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("Price");
        for (int i = 0; i < 20; i++)
        {
            DataRow row = dt.NewRow();
            row["ID"] = "ZS00000000000000" + i.ToString();
            row["Price"] = "100," + i.ToString();
            dt.Rows.Add(row);
        }
        System.Drawing.Image img1 = null;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (i != 0)
                doc.NewPage();
            PdfContentByte cb1 = writer.DirectContent;
            BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_BOLDITALIC , BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            
            cb1.SetFontAndSize(bf, 2.0f);
            cb1.BeginText();
            cb1.SetTextMatrix(1.2f, 9.5f);
            cb1.ShowText("Safi Garments");
            cb1.EndText();

            PdfContentByte cb2 = writer.DirectContent;
            BaseFont bf1 = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            cb2.SetFontAndSize(bf1, 1.3f);
            cb2.BeginText();
            cb2.SetTextMatrix(17.5f, 1.0f);
            cb2.ShowText(dt.Rows[i]["Price"].ToString());
            cb2.EndText();

            iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
            iTextSharp.text.pdf.Barcode128 bc = new Barcode128();
            bc.TextAlignment = Element.ALIGN_LEFT;
            bc.Code = dt.Rows[i]["ID"].ToString();
            bc.StartStopText = false;
            bc.CodeType = iTextSharp.text.pdf.Barcode128.EAN13;
            bc.Extended = true;
           
            //System.Drawing.Image bimg = 
            //  bc.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);
            //img1 = bimg;

            iTextSharp.text.Image img = bc.CreateImageWithBarcode(cb, 
              iTextSharp.text.BaseColor.BLACK, iTextSharp.text.BaseColor.BLACK);
                   
            cb.SetTextMatrix(1.5f, 3.0f);
            img.ScaleToFit(60, 5);
            img.SetAbsolutePosition(1.5f, 1);
            cb.AddImage(img);
        }

        ////////////////////***********************************//////////////////////
              
        doc.Close();
        System.Diagnostics.Process.Start(Environment.GetFolderPath(
                   Environment.SpecialFolder.Desktop) + "/codes.pdf");
        //MessageBox.Show("Bar codes generated on desktop fileName=codes.pdf");
    }
    catch
    {
    }
    finally
    {
        doc.Close();
    }
}

Points of Interest

The barcode generated is automatically readable by the barcode data-capture device, there is no need to do extra code for reading the barcode. Just click inside the textbox in which you want to display the barcode value and point the barcode data-capture device over the barcode paper.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Pakistan Pakistan
i am working in asp.net since 2007. My favorite is c#.
Email: zafar.kust@gmail.com
skype: zafarali5

Comments and Discussions

 
QuestionRequired Multiple bar code on one page Pin
ArvindTomar11-Feb-19 21:28
ArvindTomar11-Feb-19 21:28 
QuestionMore price tags on one page Pin
Member 105557398-Mar-15 2:37
Member 105557398-Mar-15 2:37 
QuestionHow to increase the font Pin
Member 1021020416-Dec-13 9:08
Member 1021020416-Dec-13 9:08 
AnswerRe: How to increase the font Pin
Member 109880322-Aug-14 20:22
Member 109880322-Aug-14 20:22 
QuestionHow can I generate the codebar in an existing ItextSharp projet Pin
Grafid24-Sep-13 12:41
Grafid24-Sep-13 12:41 
AnswerRe: How can I generate the codebar in an existing ItextSharp projet Pin
Zafar A khan 27-Sep-13 20:59
professionalZafar A khan 27-Sep-13 20:59 
GeneralRe: How can I generate the codebar in an existing ItextSharp projet Pin
Grafid28-Sep-13 1:26
Grafid28-Sep-13 1:26 
GeneralRe: How can I generate the codebar in an existing ItextSharp projet Pin
Zafar A khan 28-Sep-13 1:56
professionalZafar A khan 28-Sep-13 1:56 
AnswerRe: is there free source library from itextSharp Pin
Zafar A khan 16-Jul-13 19:33
professionalZafar A khan 16-Jul-13 19:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.