Saturday, 19 October 2019

Print barcode in asp on loacl machine

 


foreach (GridViewRow row in GridView1.Rows)
        {
            if ((row.FindControl("Checkbox") as CheckBox).Checked)
            {
                PrinterSettings printerName = new PrinterSettings();
                string defaultPrinter;
                string barcode = (row.FindControl("lblBarcode") as Label).Text;
                System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();

                string BarCodeU = Server.MapPath("~/Content/BarcodeFont/CODE39.TTF");//font in my server path
                //string BarCodeU = Server.MapPath("~/Content/BarcodeFont/CODE39.TTF");
                using (Bitmap bitMap = new Bitmap(580, 80))
                {
                    using (Graphics graphics = Graphics.FromImage(bitMap))
                    {
                        var myFonts = new System.Drawing.Text.PrivateFontCollection();
                        myFonts.AddFontFile(BarCodeU);
                        //var oFont = new System.Drawing.Font(myFonts.Families[0], 16);
                        var oFont = new System.Drawing.Font("CODE39.TTF", 16);//BarcodeFont In your Folder
                        PointF point = new PointF(2f, 2f);

                        SolidBrush blackBrush = new SolidBrush(Color.Black);
                        SolidBrush whiteBrush = new SolidBrush(Color.White);
                        graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
                        graphics.DrawString("*" + barcode + "*", oFont, blackBrush, point);
                    }
                    using (MemoryStream ms = new MemoryStream())
                    {
                        bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        byteImage = ms.ToArray();

                        Convert.ToBase64String(byteImage);
                        imgBarCode.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(byteImage);
                    }
                }

                SqlCommand cmd = new SqlCommand("Barcode", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Barcodes", barcode);//barcode);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable datatable = new DataTable();
                da.Fill(datatable); // getting value according to imageID and fill dataset

                DataTable dt1 = new DataTable();
                ReportDocument crystalReport = new ReportDocument();
                // creating object of crystal report
                foreach (DataRow dr in datatable.Rows)
                {

                    dr["Barcode"] = dr["Barcode"].ToString();
                }

                crystalReport.Load(Server.MapPath("../Reports/Barcode.rpt")); // path of report
                crystalReport.SetDataSource(datatable); // binding datatable

                defaultPrinter = printerName.PrinterName;
                crystalReport.PrintOptions.PrinterName = defaultPrinter;
                crystalReport.PrintToPrinter(1, false, 0, 0);
                crystalReport.ExportToHttpResponse
                                   (CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, "Barcode");

            }
            else
            {

            }
        }

No comments:

Post a Comment