Friday 12 April 2013

Show Message Box In ASP .Net with C# From Server Side

ClientScript.RegisterStartupScript(Page.GetType(), "alert", "<script language='javascript'>alert('HAI MUKESH')</script>");

Friday 5 April 2013

Generate Barcode in C# ASP.Net

How to Generate Barcode , Save It Into a folder and Display Generated Barcode

              1. Initially You have to download the open source DLL iTextSharp(For Create Pdf)
              2. Add the iTextSharp  DLL To the project.
              3. Create a folder with name barcodes to store barcode inside the  pdf.
              4. Add iTextSharp namespace to the Project.
                       using iTextSharp.text;
                       using iTextSharp.text.pdf;
                       using iTextSharp.text.html.simpleparser;

              5. Store the value you want to Generate Barcode in to the Variable Code 
              6. Then you can generate barcodes.
              7.Go and see the barcodes Folder In the Project to see the generated Barcode inside Pdf
 
Name Spaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.IO;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Text;
using System.IO.Ports;
using System.Net;



Barcode Generating Code

        string id = Guid.NewGuid().ToString();

        MemoryStream MS = new MemoryStream();
        Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);
        PdfWriter.GetInstance(pdfDoc, MS);

        pdfDoc.Open();

            // string code = dtExcelData.Rows[j].ItemArray[0].ToString();
            string sWorkPath = "";

            sWorkPath = this.Context.Server.MapPath("");

            //  string Code = code;
            string Code ="12345";
            // Multiply the lenght of the code by 40 (just to have enough width)
            int w = Code.Length * 40;


            // int w = Code.Length * 25;


            // Create a bitmap object of the width that we calculated and height of 100
            Bitmap oBitmap = new Bitmap(w, 100);


            // then create a Graphic object for the bitmap we just created.
            Graphics oGraphics = Graphics.FromImage(oBitmap);


            PrivateFontCollection fnts = new PrivateFontCollection();
            fnts.AddFontFile(sWorkPath + @"\IDAutomationHC39M.ttf");
            FontFamily fntfam = new FontFamily("IDAutomationHC39M", fnts);
            System.Drawing.Font oFont = new System.Drawing.Font(fntfam, 18);

            PointF oPoint = new PointF(2f, 2f);
            SolidBrush oBrushWrite = new SolidBrush(Color.Black);
            SolidBrush oBrush = new SolidBrush(Color.White);



            oGraphics.FillRectangle(oBrush, 0, 0, w, 100);



            oGraphics.DrawString("*" + Code + "*", oFont, oBrushWrite, oPoint);

            System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
            using (MemoryStream ms = new MemoryStream())
            {
                oBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] byteImage = ms.ToArray();

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

            iTextSharp.text.Image i = iTextSharp.text.Image.GetInstance(oBitmap, System.Drawing.Imaging.ImageFormat.Bmp);


            pdfDoc.Add(i);

       


        pdfDoc.Close();


        Response.Buffer = true;

        byte[] content = MS.ToArray();


        using (FileStream fs = File.Create(Server.MapPath("~/barcodes/" + id + ".pdf")))
        {
            fs.Write(content, 0, (int)content.Length);

        }


        System.Diagnostics.Process.Start(Server.MapPath("~/barcodes/" + id + ".pdf"));




Output


IE7 Issues Text Indent

                 Unfortunately IE 7 is still widespread among the users hence while theming we have to give special importance to the grea...