Generate barcode in c# using itextsharp
Text is a PDF library that allows you to CREATE, ADAPT, INSPECT and MAINTAIN documents in the Portable Document Format (PDF):
Text is a PDF library that allows you to CREATE, ADAPT, INSPECT and MAINTAIN documents in the Portable Document Format (PDF):
- Generate documents and reports based on data from an XML file or a database
- Create maps and books, exploiting numerous interactive features available in PDF
Steps for creating barcodes.
- creating new project window application
- download itextsharp from Here
- add reference of itextSharp.dll to your project
- Add a button to form
- add the following code on button click
- use the following namespaces
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
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("Zafar Garments");
cb1.EndText();
PdfContentByte cb2 = writer.DirectContent;
BaseFont bf1 = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
// Change the font size here
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");
//MessageBox.Show("Bar codes generated on desktop fileName=codes.pdf");
}
catch
{
}
finally
{
doc.Close();
}
}
3 Comments
I've found a Javascript Barcode Generator on the net that can be integrated into web pages.
ReplyDeletejavascript barcode only works when you connected to the internet. if not connected it will not work. And in the article you can use it offline. and no query string is required
ReplyDeleteThe pdf barcode generate step is very complicated and needs to be understood slowly.
ReplyDeletePost a Comment