Ticket Generation Using C# in .NET

city bus ticket

Please use the following points for generating tickets.

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

Step 2. Add the following namespace.

using System.Drawing.Printing;  

Step 3. Now create an object of the print document "pd".

This will make one document on which you need to draw or write content.

PrintDocument pd = new PrintDocument();  

Step 4. Then you need to make an object of the paper size. This will take arguments as name, width, and height.

You can define the size of your ticket here.

PaperSize ps = new PaperSize("", 420, 540);  

Step 5. Now generate the following event: pd.PrintPage.

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

 Step 6. Make an object of the graphics, and the following is for drawing the graphics on the ticket.

Graphics g = e.Graphics;  

Step 7. As in the preceding ticket image, we need to start drawing by making a rectangle.

g.DrawRectangle(Pens.Black,5,5,410,530);  

Step 8. Now we will add an image file in the ticket for the LOGO.

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

Step 9. We will define the font style with font size, font name, font color, and other things.

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

Step 10. Now we will write a test to the ticket (here, we will draw a line and then print the date and time as in the preceding displayed image format).

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)  

Step 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.

Random RandomNumber = new Random();  
int no = RandomNumber.Next(1000, 9999);   
Image imgBarcode = BarcodeLib.Barcode.DoEncode(BarcodeLib.TYPE.CODE128, no.ToString(),true,Color.Black,Color.White,200,60);  
g.DrawImage(imgBarcode, 10, SPACE + 240);  

Step 12. Now add the code for drawing the ticket number and other information.

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("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();  

Step 13. Now we need to set the margin of the page and then apply the page size to our document object, and then apply the 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();  

Note.  Here for printing such types 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 also, I have attached the source code with this, which will help more.


Recommended Free Ebook
Similar Articles