
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.

bilal uddinPosted Apr 23, 2019, 5:57 PM
Sir How to Download this Ticket In pdf File
Alice NguyenPosted Jan 5, 2019, 9:57 PM
Nice great article, thank you so much
Jitendra BeheraPosted Dec 19, 2018, 9:51 AM
It is looking good by seeing code but Video demo is needed.
Sarthak MenparaPosted Jun 29, 2018, 12:41 AM
I just want to make simple application for practice purpose in which there is list of movies and user can see the available seat and book the movie on particular date and time.and admin can update and delete the movie. admin also can see the seat availability and add or delete seat. without login user can check only seat availability. to book seat anyone have to login first.and user also can check the order history. there is no any payment gateway or payment option.can you give me guidance,reference or any link ? i want to make this system in mvc 5 .net.
rahul kumarPosted Jun 8, 2018, 2:48 AM
Does it cut the paper automatically after printing all content or it will generate blank print too. plz clear it.
SubashPosted Jul 14, 2016, 6:54 AM
Nice
kalu singh raoPosted Jul 12, 2016, 2:26 AM
Nice...
Shobana JPosted Jul 7, 2016, 7:41 AM
Nice explanation
sushant bokdePosted Mar 16, 2016, 12:59 PM
its not working what should i do its showing error in dispose
Valentín ElizondoPosted Mar 3, 2016, 1:19 PM
Thank you. Really usefull :D
Praveen KumarPosted Jul 16, 2015, 11:11 PM
Very nice! Thank you for sharing!!
Rahul PrajapatPosted Jul 16, 2015, 11:00 PM
nice explanation sir
Pankaj Kumar ChoudharyPosted Jul 16, 2015, 6:51 PM
Nice Start..........
Santhakumar MunuswamyPosted Jul 16, 2015, 3:20 PM
Welcome
Santhakumar MunuswamyPosted Jul 16, 2015, 3:20 PM
Good Start
Sam HobbsPosted Jul 16, 2015, 1:15 PM
Thank you for the line-by-line explanation.
Narasimha Reddy ChennupalliPosted Jul 16, 2015, 8:48 AM
Good one.. thanks for sharing..
Nilesh JadavPosted Jul 16, 2015, 6:54 AM
Nice one sir
Debasis SahaPosted Jul 16, 2015, 6:11 AM
Good one..
Sibeesh VenuPosted Jul 16, 2015, 3:28 AM
Thanks for the information .
Vipul MalhotraPosted Jul 16, 2015, 3:18 AM
Nice
Vinodh NarayananPosted Jul 16, 2015, 3:01 AM
good one