Breaking News
Loading...
Thursday 11 July 2013

Ticket Generation Using C#.net


              
               This article show and learns about how to generate the ticket using c#.net. This describe the how actually ticket (bus ticket) is generate what it will use any how it work
             This article presents a solution to solving ticket generation problem using C# and Visual Studio 2010.This application is multipurpose application. This article is use full for many purpose for example city bus ticket generation, store retail ticket generation, in fair for rider tickets etc. This application will solve problem of how to print ticket, how to set margin of text for ticketetc.

Follow the Steps:

Step -1

In this tutorial first you have to add namespace
using System.Drawing.Printing;

now start with fist make object of the print document  ‘pd’.this will  make one document on which you have to draw or write content
PrintDocument pd = new PrintDocument();

Then you have to make object of the paper size this will take arguments as name, width, height this define the size of your ticket
PaperSize ps = new PaperSize("", 420, 540);

The you have to generate ‘pageprinteventhandler’  this event is use for the add text content and other  in to the ticket . You have to write in this event

pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
         ‘’ write your code
}

 Step - 2
Now write code for what you have to add in ticket
      a)      First of all make object of the graphics this is for draw the graphics in ticket .
Graphics g = e.Graphics;

      b)      Now we have to draw the rectangle so we write code soothing like. here ‘DrawRectangle’ take four arguments (color of rectangle, x-axis, y-axis, width, height)
g.DrawRectangle(Pens.Black,5,5,410,530);

      c)       now we have to  add ticket title in to ticket, here we take  image for the title so first we have to copy our image into project folder and then give path of this image to our function ‘Drawimage’ function with parameter as(image _path, x-axis, y-axis)

   string title = Application.StartupPath+"\\CBT_Title.png";
   g.DrawImage(Image.FromFile(title),50,7);

    d)   now we want to add text to the ticket so fist we make object of the font with parameter    (Font_name, Font_size, Font_Style)
and also add we make object of the solid brush this is for the color of the font


   Font fBody = new Font("Lucida Console", 15, FontStyle.Bold);
   SolidBrush sb = new SolidBrushColor.Black);


        e)      now  we add text to the ticket so we use ‘DrawString()’  function to add string in ticket so we now add “dotted line” to the ticket so code is like and it take arguments (“Your_Text”,font_object,  Solid_Object, x-asix, y-axis)
   g.DrawString("------------------------------",fBody1,sb, 10, 120);
    
f)       now we add date, time by sing same as above function ‘DrawString’
g.DrawString("Date :", fBody,sb, 10, SPACE);     g.DrawString(DateTime.Now.ToShortDateString(), fBody1, sb, 90, SPACE);        g.DrawString("Time :", fBody, sb, 10, SPACE+30);      g.DrawString(DateTime.Now.ToShortTimeString(), fBody1, sb, 90, SPACE +30)


     now we add ticket number which is generate by random number function and add to the   ticket and also we have to generate the barcode for the this ticket number
     so we have to first add one DLL to our project which is used to generate the barcode code :

Random RandomNumber = new Random();
int no = RandomNumber.Next(1000, 9999);


belove code generate the barcode for the given number with parameter (barcode_type, Barcode_number, barcode_color,color,width,height)

Image imgBarcode = BarcodeLib.Barcode.DoEncode(BarcodeLib.TYPE.CODE128, no.ToString(),true,Color.Black,Color.White,200,60);
g.DrawImage(imgBarcode, 10, SPACE + 240);

Now code for draw the ticket and ticket number

g.DrawString("TicketNo.:", fBody, sb, 10, SPACE+60);
g.DrawString(no.ToString(), fBody1, sb, 150, SPACE + 60);


   now we have to draw bus number ,bus route, number of passenger, prize of ticket, total rupee, helpline number, notice



            g.DrawString("BusNo.:", fBody, sb, 10, SPACE+90 );
            g.DrawString(txtBusNo.Text, fBody1, sb, 100, SPACE + 90);

           

            g.DrawString("Route:", fBody, sb, 10, SPACE+120);
            g.DrawString(cbRoute.SelectedItem.ToString(), fBody1, sb, 100, SPACE + 120);

            int price = Convert.ToInt32(txtMember.Text) * Convert.ToInt32(txtPrice.Text);
            string price1=txtMember.Text +" X "+ txtPrice.Text +" = "+price.ToString();

            g.DrawString("Full:", fBody, sb, 10, SPACE+150);
            g.DrawString(price1, fBody1, sb, 80, SPACE + 150);

            g.DrawString("Rs."+price.ToString()+".00", rs, sb, 10, SPACE + 180);
            g.DrawString(TType,fTType, sb, 230, 120);
            g.DrawString("HelplineNo.: +91 9999999999", fBody2,sb, 15, 465);
            g.DrawString("* NOT TRANSFERABLE", fBody2, sb, 15, 485);
            g.Dispose();


finally we have to dispose the graphics object


The Compelete Code

void pd_PrintPage(object sender, PrintPageEventArgs e)
{
            int SPACE = 145;
            string title = Application.StartupPath+"\\CBT_Title.png";
            string barcode = Application.StartupPath + "\\code128bar.jpg";
            Graphics g = e.Graphics;
            g.DrawRectangle(Pens.Black,5,5,410,530);

            string TType = "S";

            if (rbReturn.Checked)
            {
                TType = "R";
            }
          


            g.DrawImage(Image.FromFile(title),50,7);
            Font fBody = new Font("Lucida Console", 15, FontStyle.Bold);
            Font fBody1 = new Font("Lucida Console", 15, FontStyle.Regular);
            Font fBody2 = new Font("Lucida Console", 9, FontStyle.Regular);
            Font rs = new Font("Stencil", 25, FontStyle.Bold);
            Font fTType = new Font("", 150, FontStyle.Bold);
            SolidBrush sb = new SolidBrush(Color.Black);



            g.DrawString("------------------------------",fBody1,sb, 10, 120);

            g.DrawString("Date :", fBody,sb, 10, SPACE);
            g.DrawString(DateTime.Now.ToShortDateString(), fBody1, sb, 90, SPACE);

            g.DrawString("Time :", fBody, sb, 10, SPACE+30);
            g.DrawString(DateTime.Now.ToShortTimeString(), fBody1, sb, 90, SPACE + 30);


            Random RandomNumber = new Random();
            int no = RandomNumber.Next(1000, 9999);

            //System.IO.File.Delete(Application.StartupPath + "\\code128bar.jpg");

            Image imgBarcode = BarcodeLib.Barcode.DoEncode(BarcodeLib.TYPE.CODE128, no.ToString(),true,Color.Black,Color.White,200,60);
           // imgBarcode.Save(Application.StartupPath + "\\code128bar.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            g.DrawString("TicketNo.:", fBody, sb, 10, SPACE+60);
            g.DrawString(no.ToString(), fBody1, sb, 150, SPACE + 60);

            g.DrawString("BusNo.:", fBody, sb, 10, SPACE+90 );
            g.DrawString(txtBusNo.Text, fBody1, sb, 100, SPACE + 90);

            //g.DrawString("DriverName:", fBody, sb, 10, SPACE+120);
            //g.DrawString(txtDriveName.Text, fBody1, sb, 153, SPACE + 120);

            g.DrawString("Route:", fBody, sb, 10, SPACE+120);
            g.DrawString(cbRoute.SelectedItem.ToString(), fBody1, sb, 100, SPACE + 120);

            // price Calculation
            int price = Convert.ToInt32(txtMember.Text) * Convert.ToInt32(txtPrice.Text);
            string price1=txtMember.Text +" X "+ txtPrice.Text +" = "+price.ToString();

            g.DrawString("Full:", fBody, sb, 10, SPACE+150);
            g.DrawString(price1, fBody1, sb, 80, SPACE + 150);

            g.DrawString("Rs."+price.ToString()+".00", rs, sb, 10, SPACE + 180);

            g.DrawString(TType,fTType, sb, 230, 120);

           
            //g.DrawImage(Image.FromFile(barcode), 10, SPACE+240);
            g.DrawImage(imgBarcode, 10, SPACE + 240);
            g.DrawString("HelplineNo.: +91 9999999999", fBody2,sb, 15, 465);

            g.DrawString("* NOT TRANSFERABLE", fBody2, sb, 15, 485);


            g.Dispose();
        }
Step – 3
Now we have to set the margine of the page  and then apply the page size to the  our document object,then apply print command
            pd.PrintController = new StandardPrintController();
            pd.DefaultPageSettings.Margins.Left = 0;
            pd.DefaultPageSettings.Margins.Right = 0;
            pd.DefaultPageSettings.Margins.Top = 0;
            pd.DefaultPageSettings.Margins.Bottom = 0;

            pd.DefaultPageSettings.PaperSize = ps;
            pd.Print();
 


2 comments:

  1. i want to print multiple tickets on same page, how to do this?

    ReplyDelete

Thanks for comment

 
Toggle Footer