Click here to Skip to main content
13,198,068 members (49,951 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


25 bookmarked
Posted 4 Jul 2013

Generating Barcode for a Sale/Purchase System Using itextSharp

, 4 Jul 2013
Rate this:
Please Sign up or sign in to vote.
Generating barcodes for a sale/purchase system for the items to stick on.

Sample Image


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.


  • 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 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:

using iTextSharp.text;
using iTextSharp.text.pdf;

Add the following code in the button_Click event:

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

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

            PdfContentByte cb2 = writer.DirectContent;
            BaseFont bf1 = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            cb2.SetFontAndSize(bf1, 1.3f);
            cb2.SetTextMatrix(17.5f, 1.0f);

            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);

                   Environment.SpecialFolder.Desktop) + "/codes.pdf");
        //MessageBox.Show("Bar codes generated on desktop fileName=codes.pdf");

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.


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


About the Author

Zafar Safi
Software Developer (Senior)
Afghanistan Afghanistan
i am working in since 2007. My favorite is c#.
skype: zafarali5

You may also be interested in...


Comments and Discussions

QuestionMore price tags on one page Pin
Member 105557398-Mar-15 2:37
memberMember 105557398-Mar-15 2:37 
QuestionHow to increase the font Pin
Member 1021020416-Dec-13 9:08
memberMember 1021020416-Dec-13 9:08 
AnswerRe: How to increase the font Pin
Member 109880322-Aug-14 20:22
memberMember 109880322-Aug-14 20:22 
QuestionHow can I generate the codebar in an existing ItextSharp projet Pin
Grafid24-Sep-13 12:41
memberGrafid24-Sep-13 12:41 
AnswerRe: How can I generate the codebar in an existing ItextSharp projet Pin
Zafar Safi27-Sep-13 20:59
professionalZafar Safi27-Sep-13 20:59 
GeneralRe: How can I generate the codebar in an existing ItextSharp projet Pin
Grafid28-Sep-13 1:26
memberGrafid28-Sep-13 1:26 
GeneralRe: How can I generate the codebar in an existing ItextSharp projet Pin
Zafar Safi28-Sep-13 1:56
professionalZafar Safi28-Sep-13 1:56 
Questionis there free source library from itextSharp Pin
hongdida16-Jul-13 16:27
memberhongdida16-Jul-13 16:27 
AnswerRe: is there free source library from itextSharp Pin
Zafar Safi16-Jul-13 19:33
professionalZafar Safi16-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 4 Jul 2013
Article Copyright 2013 by Zafar Safi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid