Please use the following points for generating tickets.
- Open Visual Studio and create a new Windows Form application.
- Add the following namespace.
- using System.Drawing.Printing;
- 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();
- 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.
- PaperSize ps = new PaperSize("", 420, 540);
- Now generate the following event: pd.PrintPage.
- pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
-
- void pd_PrintPage(object sender, PrintPageEventArgs e)
- {
- ‘’ write your code
- }
- Make an object of the graphics, the following is for drawing the graphics on the ticket.
- As in the preceding ticket image we need to start drawing by making a rectangle.
- g.DrawRectangle(Pens.Black,5,5,410,530);
- 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);
- 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);
- 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).
- 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)
- 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);
- 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();
- 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.
- 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 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.