Generating Qr Barcode in C#.net
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 iTextSharp.text; using iTextSharp.text.pdf; private void button1_Click(object sender, EventArgs e) { Document doc = new Document(new iTextSharp.text.Rectangle(4.5f, 5.5f), 0.5f, 0.5f, 0, 0); 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 < 8; i++) { DataRow row = dt.NewRow(); row["ID"] = "ZS00000000000000" + i.ToString(); row["Price"] = "100," + i.ToString(); dt.Rows.Add(row); } for (int i = 0; i < dt.Rows.Count; i++) { if (i != 0) doc.NewPage(); iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent; iTextSharp.text.pdf.BarcodeQRCode Qr = new BarcodeQRCode(dt.Rows[i]["ID"].ToString(),60,6,null); iTextSharp.text.Image img = Qr.GetImage(); cb.SetTextMatrix(-2.0f, 0.0f); img.ScaleToFit(60, 5); img.SetAbsolutePosition(-2.8f, 0.5f); cb.AddImage(img); PdfContentByte cb1 = writer.DirectContent; BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_BOLDITALIC, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb1.SetFontAndSize(bf, 0.5f); cb1.BeginText(); cb1.SetTextMatrix(0.2f, 5.1f); cb1.ShowText("Zafar Garments"); cb1.EndText(); PdfContentByte id = writer.DirectContent; BaseFont bf1 = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); id.SetFontAndSize(bf1, 0.4f); id.BeginText(); id.SetTextMatrix(0.2f, 0.6f); id.ShowText(dt.Rows[i]["ID"].ToString()); id.EndText(); } // if you want to print it un comment the following two line //PdfAction act = new PdfAction(PdfAction.PRINTDIALOG); //writer.SetOpenAction(act); doc.Close(); System.Diagnostics.Process.Start(Environment.GetFolderPath( Environment.SpecialFolder.Desktop) + "/codes.pdf"); } catch { } finally { doc.Close(); } }
7 Comments
Nice Sir, thanks for sharing your experiences
ReplyDeleteThank you. I will share more
DeleteThank you. I will share more
Deletesir i have error in writer, any advice iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
Deletepost complete error/exception message.
DeleteVery good !I like this generate barcode tips and tricks.
ReplyDeleteThanks for sharing.
ReplyDeletePost a Comment