Introduction
This is a simple ticket printing system I have developed for .NET programmers who want to see a printing demonstration. Using this system you will learn how to print any document with a custom paper size and how to operate in an object oriented environment.
Background
I was searching for a printing receipt solution all over the internet but couldn't find the exact solution. I was frustrated a little as I wouldn't be able to deliver the project on time to the client because of a lack of proper knowledge of the printing API of .NET. But fortunately I read a book where I found certain guidelines about how to print a document in a receipt like fashion so I worked a little while and found a solution.
Using the code
The articles includes a one main class Ticket
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing;
namespace TicketingSystem
{
public class Ticket
{
PrintDocument pdoc = null;
int ticketNo;
DateTime TicketDate;
String Source, Destination,DrawnBy;
float Amount;
public int TicketNo
{
set { this.ticketNo = value; }
get { return this.ticketNo; }
}
public DateTime ticketDate
{
set { this.TicketDate = value; }
get { return this.TicketDate; }
}
public String source
{
set { this.Source = value; }
get { return this.Source; }
}
public String destination
{
set { this.Destination = value; }
get { return this.Destination; }
}
public float amount
{
set { this.Amount = value; }
get { return this.Amount; }
}
public String drawnBy
{
set { this.DrawnBy = value; }
get { return this.DrawnBy; }
}
public Ticket()
{
}
public Ticket(int ticketNo, DateTime TicketDate, String Source,
String Destination, float Amount, String DrawnBy)
{
this.ticketNo = ticketNo;
this.TicketDate = TicketDate;
this.Source = Source;
this.Destination = Destination;
this.Amount = Amount;
this.DrawnBy = DrawnBy;
}
public void print()
{
PrintDialog pd = new PrintDialog();
pdoc = new PrintDocument();
PrinterSettings ps = new PrinterSettings();
Font font = new Font("Courier New", 15);
PaperSize psize = new PaperSize("Custom", 100, 200);
pd.Document = pdoc;
pd.Document.DefaultPageSettings.PaperSize = psize;
pdoc.DefaultPageSettings.PaperSize.Height = 820;
pdoc.DefaultPageSettings.PaperSize.Width = 520;
pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
DialogResult result = pd.ShowDialog();
if (result == DialogResult.OK)
{
PrintPreviewDialog pp = new PrintPreviewDialog();
pp.Document = pdoc;
result = pp.ShowDialog();
if (result == DialogResult.OK)
{
pdoc.Print();
}
}
}
void pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics graphics = e.Graphics;
Font font = new Font("Courier New", 10);
float fontHeight = font.GetHeight();
int startX = 50;
int startY = 55;
int Offset = 40;
graphics.DrawString("Welcome to MSST", new Font("Courier New", 14),
new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
graphics.DrawString("Ticket No:" + this.TicketNo,
new Font("Courier New", 14),
new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
graphics.DrawString("Ticket Date :" + this.ticketDate,
new Font("Courier New", 12),
new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
String underLine = "------------------------------------------";
graphics.DrawString(underLine, new Font("Courier New", 10),
new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
String Source= this.source;
graphics.DrawString("From "+Source+" To "+Destination, new Font("Courier New", 10),
new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
String Grosstotal = "Total Amount to Pay = " + this.amount;
Offset = Offset + 20;
underLine = "------------------------------------------";
graphics.DrawString(underLine, new Font("Courier New", 10),
new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
graphics.DrawString(Grosstotal , new Font("Courier New", 10),
new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
String DrawnBy = this.drawnBy;
graphics.DrawString("Conductor - "+DrawnBy, new Font("Courier New", 10),
new SolidBrush(Color.Black), startX, startY + Offset);
}
}
}
...
I found that this is quite an easy code for printing solutions in future.
https://www.buymeacoffee.com/tusharit25