
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:
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);
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;
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");
}
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.