Click here to Skip to main content
Click here to Skip to main content

Tagged as

Generating Barcode for a Sale/Purchase System Using itextSharp

, 4 Jul 2013 CPOL
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

Comments and Discussions

QuestionMore price tags on one page PinmemberMember 105557398-Mar-15 3:37 
QuestionHow to increase the font PinmemberMember 1021020416-Dec-13 10:08 
AnswerRe: How to increase the font PinmemberMember 109880322-Aug-14 21:22 
QuestionHow can I generate the codebar in an existing ItextSharp projet PinmemberGrafid24-Sep-13 13:41 
AnswerRe: How can I generate the codebar in an existing ItextSharp projet PinprofessionalZafar Safi27-Sep-13 21:59 
GeneralRe: How can I generate the codebar in an existing ItextSharp projet PinmemberGrafid28-Sep-13 2:26 
GeneralRe: How can I generate the codebar in an existing ItextSharp projet PinprofessionalZafar Safi28-Sep-13 2:56 
Questionis there free source library from itextSharp Pinmemberhongdida16-Jul-13 17:27 
AnswerRe: is there free source library from itextSharp PinprofessionalZafar Safi16-Jul-13 20:33 

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

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

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