Click here to Skip to main content
12,444,360 members (49,561 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

145.5K views
11.2K downloads
29 bookmarked
Posted

Simple Receipt Like Printing Using the C# Printing API

, 5 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This is a simple ticket printing system that I have prepared for programmers who want to learn how to do receipt like printing.

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 the person name
            set { this.ticketNo = value; }
            //get the person name 
            get { return this.ticketNo; }
        }
        public DateTime ticketDate
        {
            //set the person name
            set { this.TicketDate = value; }
            //get the person name 
            get { return this.TicketDate; }
        }

        public String source
        {
            //set the person name
            set { this.Source = value; }
            //get the person name 
            get { return this.Source; }
        }
        public String destination
        {
            //set the person name
            set { this.Destination = value; }
            //get the person name 
            get { return this.Destination; }
        }
        public float amount
        {
            //set the person name
            set { this.Amount = value; }
            //get the person name 
            get { return this.Amount; }
        }
        public String drawnBy
        {
            //set the person name
            set { this.DrawnBy = value; }
            //get the person name 
            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);
            //ps.DefaultPageSettings.PaperSize = psize;

            pd.Document = pdoc;
            pd.Document.DefaultPageSettings.PaperSize = psize;
            //pdoc.DefaultPageSettings.PaperSize.Height =320;
            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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Tushar K. Skype(tushar.kshirsagar24)
Founder Oleander Software Solution Pvt Ltd
India India
I am Mr Tushar Kshirsagar. I Used to work at Oleander Software solutions Pvt.Ltd.
I have founded this organization 2009 putting motive behind to provide software
solution with high easiness. We are automating various business out there in market
with our high end software solution.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questionneed help on printpageeventhandler Pin
awinash kr6-Aug-16 6:47
memberawinash kr6-Aug-16 6:47 
QuestionHatur nuhun Pin
Muhamad Rasyid Ridho16-Apr-15 23:49
memberMuhamad Rasyid Ridho16-Apr-15 23:49 
AnswerRe: Hatur nuhun Pin
Tushar K. Skype(tushar.kshirsagar24)22-Jun-15 17:47
professionalTushar K. Skype(tushar.kshirsagar24)22-Jun-15 17:47 
QuestionGracias Pin
Member 1148002717-Mar-15 12:31
memberMember 1148002717-Mar-15 12:31 
GeneralHow to Print Single Line on Dot Matrix Printer in asp.net 4.0 C# Pin
Lakhpat-Singh12-Jan-15 20:54
memberLakhpat-Singh12-Jan-15 20:54 
GeneralRe: How to Print Single Line on Dot Matrix Printer in asp.net 4.0 C# Pin
Tushar K. Skype(tushar.kshirsagar24)15-Jan-15 20:15
professionalTushar K. Skype(tushar.kshirsagar24)15-Jan-15 20:15 
Questionsmall edit to the form. Pin
Member 113419162-Jan-15 3:42
memberMember 113419162-Jan-15 3:42 
QuestionCode for Print receipt/Page in Asp.net-C# Pin
Member 995422424-Apr-14 5:34
memberMember 995422424-Apr-14 5:34 
AnswerRe: Code for Print receipt/Page in Asp.net-C# Pin
Tushar Kshirsagar, Lonand12-May-14 3:39
professionalTushar Kshirsagar, Lonand12-May-14 3:39 
QuestionHow to print Company logo (Image) using this API ???????? Pin
Azeem.17-Feb-14 10:49
memberAzeem.17-Feb-14 10:49 
AnswerRe: How to print Company logo (Image) using this API ???????? Pin
Enobong Adahada15-Jul-16 10:47
memberEnobong Adahada15-Jul-16 10:47 
QuestionNo printout Pin
vyx16-Jan-14 17:48
membervyx16-Jan-14 17:48 
AnswerRe: No printout Pin
tusharit2524-Jan-14 17:18
membertusharit2524-Jan-14 17:18 
Generalerror Pin
Punk Rockefeller Fatty16-Jan-14 8:25
memberPunk Rockefeller Fatty16-Jan-14 8:25 
GeneralRe: error Pin
tusharit2524-Jan-14 17:19
membertusharit2524-Jan-14 17:19 
QuestionSmall alteration Pin
Dennis_Aries17-Apr-13 4:18
memberDennis_Aries17-Apr-13 4:18 
AnswerRe: Small alteration Pin
tusharit2525-Dec-13 3:37
membertusharit2525-Dec-13 3:37 
QuestionSimple Receipt Like Printing Using the C# Printing API Pin
Ruben lOuw26-Feb-13 19:42
memberRuben lOuw26-Feb-13 19:42 
AnswerRe: Simple Receipt Like Printing Using the C# Printing API Pin
tusharit2525-Dec-13 3:37
membertusharit2525-Dec-13 3:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160811.3 | Last Updated 5 Sep 2012
Article Copyright 2012 by Tushar K. Skype(tushar.kshirsagar24)
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid