Ticket Generation Using C# in .NET

city bus ticket

Please use the following points for generating tickets.

  1. Open Visual Studio and create a new Windows Form application.

  2. Add the following namespace.
    1. using System.Drawing.Printing;  
  3. Now create an object of the print document "pd".

    This will make one document on which you need to draw or write content.
    1. PrintDocument pd = new PrintDocument();  
  4. Then you need to make an object of the paper size, this will take arguments as name, width, height.

    You can define the size of your ticket here.
    1. PaperSize ps = new PaperSize("", 420, 540);  
  5. Now generate the following event: pd.PrintPage.
    1. pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);  
    2.   
    3. void pd_PrintPage(object sender, PrintPageEventArgs e)  
    4. {  
    5.    ‘’ write your code  
    6. }  
  6. Make an object of the graphics, the following is for drawing the graphics on the ticket.
    1. Graphics g = e.Graphics;  
  7. As in the preceding ticket image we need to start drawing by making a rectangle.
    1. g.DrawRectangle(Pens.Black,5,5,410,530);  
  8. Now we will add an image file in the ticket for the LOGO.
    1. string title = Application.StartupPath+"\\CBT_Title.png";  
    2. g.DrawImage(Image.FromFile(title),50,7);  
  9. We will define the font style with font size, font name, font color and other things.
    1. Font fBody = new Font("Lucida Console", 15, FontStyle.Bold);  
    2. SolidBrush sb = new SolidBrushColor.Black);  
  10. Now we will write a test to the ticket (here we will draw a line then print the date and time as in the preceding displayed image format).
    1. g.DrawString("------------------------------",fBody1,sb, 10, 120);  
    2. g.DrawString("Date :", fBody,sb, 10, SPACE);   
    3. g.DrawString(DateTime.Now.ToShortDateString(), fBody1, sb, 90, SPACE);   
    4. g.DrawString("Time :", fBody, sb, 10, SPACE+30);   
    5. g.DrawString(DateTime.Now.ToShortTimeString(), fBody1, sb, 90, SPACE +30)  
  11. Now we add the ticket number that is randomly number generated and add it to the ticket and also we need to generate the barcode for this ticket number.

    So we need to first add one DLL to our project that is used to generate the Barcode code:
    1. Random RandomNumber = new Random();  
    2. int no = RandomNumber.Next(1000, 9999);   
    3. Image imgBarcode = BarcodeLib.Barcode.DoEncode(BarcodeLib.TYPE.CODE128, no.ToString(),true,Color.Black,Color.White,200,60);  
    4. g.DrawImage(imgBarcode, 10, SPACE + 240);  
  12. Now add the code for drawing the ticket number and other information.
    1. g.DrawString("TicketNo.:", fBody, sb, 10, SPACE+60);  
    2. g.DrawString(no.ToString(), fBody1, sb, 150, SPACE + 60);  
    3. g.DrawString("BusNo.:", fBody, sb, 10, SPACE+90 );  
    4.   
    5. g.DrawString(txtBusNo.Text, fBody1, sb, 100, SPACE + 90);  
    6. g.DrawString("Route:", fBody, sb, 10, SPACE+120);  
    7. g.DrawString(cbRoute.SelectedItem.ToString(), fBody1, sb, 100, SPACE + 120);  
    8. int price = Convert.ToInt32(txtMember.Text) * Convert.ToInt32(txtPrice.Text);  
    9. string price1=txtMember.Text +" X "+ txtPrice.Text +" = "+price.ToString();  
    10. g.DrawString("Full:", fBody, sb, 10, SPACE+150);  
    11. g.DrawString(price1, fBody1, sb, 80, SPACE + 150);  
    12. g.DrawString("Rs."+price.ToString()+".00", rs, sb, 10, SPACE + 180);  
    13. g.DrawString(TType,fTType, sb, 230, 120);  
    14. g.DrawString("HelplineNo.: +91 9999999999", fBody2,sb, 15, 465);  
    15. g.DrawString("* NOT TRANSFERABLE", fBody2, sb, 15, 485);  
    16. g.Dispose();  
  13. Now we need to set the margin of the page and then apply the page size to the our document object, then apply print command.
    1. pd.PrintController = new StandardPrintController();  
    2.   
    3. pd.DefaultPageSettings.Margins.Left = 0;  
    4. pd.DefaultPageSettings.Margins.Right = 0;  
    5. pd.DefaultPageSettings.Margins.Top = 0;  
    6. pd.DefaultPageSettings.Margins.Bottom = 0;  
    7. pd.DefaultPageSettings.PaperSize = ps;  
    8. pd.Print();  

Note: Here for printing such type of tickets you can use any thermal print like the Epson Thermal Printer and so on.
Here if you have any query then you can provide it in the comment section and also I have attached the source code with this, that will help more.

My personal blog.