Click here to Skip to main content
15,890,186 members
Articles / Programming Languages / C#
Tip/Trick

Simple Receipt Like Printing Using the C# Printing API

Rate me:
Please Sign up or sign in to vote.
4.96/5 (22 votes)
5 Sep 2012CPOL 228.4K   19.2K   41   37
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:

C#
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.

https://www.buymeacoffee.com/tusharit25

 

License

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


Written By
Founder Twig Software Solutions Pvt. Ltd.
India India
Passionate Software Developer.

Comments and Discussions

 
QuestionProblem When Printing Multiple Pages Pin
Member 1239236824-Dec-20 0:32
Member 1239236824-Dec-20 0:32 
AnswerRe: Problem When Printing Multiple Pages Pin
Tushar K. Skype(tushar.kshirsagar24)17-Mar-21 22:17
professionalTushar K. Skype(tushar.kshirsagar24)17-Mar-21 22:17 
Answernice articel Pin
layarfilm215-May-20 2:56
layarfilm215-May-20 2:56 
Questionprinter and paper type Pin
Ma'd Saeed28-Jul-18 22:16
Ma'd Saeed28-Jul-18 22:16 
AnswerRe: printer and paper type Pin
Tushar K. Skype(tushar.kshirsagar24)3-Dec-18 2:46
professionalTushar K. Skype(tushar.kshirsagar24)3-Dec-18 2:46 
QuestionCouldn't Download Pin
Member 1386759511-Jun-18 1:18
Member 1386759511-Jun-18 1:18 
QuestionProblem in aligning text in pos receipt Pin
Member 126751068-Apr-18 20:30
Member 126751068-Apr-18 20:30 
QuestionPaper auto cuts after few items in pos Pin
Member 126142384-Feb-18 23:20
Member 126142384-Feb-18 23:20 
QuestionVB.NET version (TESTED) Pin
htmlmaster12-Oct-17 19:22
professionalhtmlmaster12-Oct-17 19:22 
VB
Imports System.Collections.Generic
Imports System.Linq
Imports System.Windows.Forms
Imports System.Drawing.Printing
Imports System.Drawing

Namespace TicketingSystem
    Public Class Ticket
        Private pdoc As PrintDocument = Nothing
        Private m_ticketNo As Integer
        Private m_TicketDate As DateTime
        Private m_Source As [String], m_Destination As [String], m_DrawnBy As [String]
        Private m_Amount As Single

        Public Property TicketNo() As Integer
            'set the person name
            'get the person name 
            Get
                Return Me.m_ticketNo
            End Get
            Set
                Me.m_ticketNo = Value
            End Set
        End Property
        Public Property ticketDate() As DateTime
            'set the person name
            'get the person name 
            Get
                Return Me.m_TicketDate
            End Get
            Set
                Me.m_TicketDate = Value
            End Set
        End Property

        Public Property source() As [String]
            'set the person name
            'get the person name 
            Get
                Return Me.m_Source
            End Get
            Set
                Me.m_Source = Value
            End Set
        End Property
        Public Property destination() As [String]
            'set the person name
            'get the person name 
            Get
                Return Me.m_Destination
            End Get
            Set
                Me.m_Destination = Value
            End Set
        End Property
        Public Property amount() As Single
            'set the person name
            'get the person name 
            Get
                Return Me.m_Amount
            End Get
            Set
                Me.m_Amount = Value
            End Set
        End Property
        Public Property drawnBy() As [String]
            'set the person name
            'get the person name 
            Get
                Return Me.m_DrawnBy
            End Get
            Set
                Me.m_DrawnBy = Value
            End Set
        End Property


        Public Sub New()
        End Sub
        Public Sub New(ticketNo As Integer, TicketDate As DateTime, Source As [String], Destination As [String], Amount As Single, DrawnBy As [String])
            Me.m_ticketNo = ticketNo
            Me.m_TicketDate = TicketDate
            Me.m_Source = Source
            Me.m_Destination = Destination
            Me.m_Amount = Amount
            Me.m_DrawnBy = DrawnBy
        End Sub
        Public Sub print()
            Dim pd As New PrintDialog()
            pdoc = New PrintDocument()
            Dim ps As New PrinterSettings()
            Dim font As New Font("Courier New", 15)


            Dim psize As 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

            AddHandler pdoc.PrintPage, New PrintPageEventHandler(AddressOf pdoc_PrintPage)

            Dim result As DialogResult = pd.ShowDialog()
            If result = DialogResult.OK Then
                Dim pp As New PrintPreviewDialog()
                pp.Document = pdoc
                result = pp.ShowDialog()
                If result = DialogResult.OK Then
                    pdoc.Print()
                End If
            End If

        End Sub
        Private Sub pdoc_PrintPage(sender As Object, e As PrintPageEventArgs)
            Dim graphics As Graphics = e.Graphics
            Dim font As New Font("Courier New", 10)
            Dim fontHeight As Single = font.GetHeight()
            Dim startX As Integer = 50
            Dim startY As Integer = 55
            Dim Offset As Integer = 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:" & Me.TicketNo, New Font("Courier New", 14), New SolidBrush(Color.Black), startX, startY + Offset)
            Offset = Offset + 20
            graphics.DrawString("Ticket Date :" & Me.ticketDate, New Font("Courier New", 12), New SolidBrush(Color.Black), startX, startY + Offset)
            Offset = Offset + 20
            Dim underLine As [String] = "------------------------------------------"
            graphics.DrawString(underLine, New Font("Courier New", 10), New SolidBrush(Color.Black), startX, startY + Offset)

            Offset = Offset + 20
            Dim Source As [String] = Me.source
            graphics.DrawString("From " & Source & " To " & m_Destination, New Font("Courier New", 10), New SolidBrush(Color.Black), startX, startY + Offset)

            Offset = Offset + 20
            Dim Grosstotal As [String] = "Total Amount to Pay = " & Me.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
            Dim DrawnBy As [String] = Me.drawnBy
            graphics.DrawString("Conductor - " & DrawnBy, New Font("Courier New", 10), New SolidBrush(Color.Black), startX, startY + Offset)
        End Sub
    End Class
End Namespace

AnswerRe: VB.NET version (TESTED) Pin
Tushar K. Skype(tushar.kshirsagar24)6-Nov-17 22:39
professionalTushar K. Skype(tushar.kshirsagar24)6-Nov-17 22:39 
GeneralMy vote of 5 Pin
Joshin Joy20-Jul-17 21:05
Joshin Joy20-Jul-17 21:05 
QuestionThose who are facing problem with adding Image to the Printing for them Pin
Tushar K. Skype(tushar.kshirsagar24)14-May-17 22:58
professionalTushar K. Skype(tushar.kshirsagar24)14-May-17 22:58 
BugError Pin
Member 130402524-Apr-17 6:50
Member 130402524-Apr-17 6:50 
GeneralRe: Error Pin
Tushar K. Skype(tushar.kshirsagar24)3-Jul-17 23:45
professionalTushar K. Skype(tushar.kshirsagar24)3-Jul-17 23:45 
QuestionNo PrintOut Pin
bluestuck2-Apr-17 7:58
bluestuck2-Apr-17 7:58 
AnswerRe: No PrintOut Pin
Tushar K. Skype(tushar.kshirsagar24)3-Jul-17 23:44
professionalTushar K. Skype(tushar.kshirsagar24)3-Jul-17 23:44 
PraiseLooks great Pin
Member 1150387931-Jan-17 2:27
Member 1150387931-Jan-17 2:27 
Questionneed help on printpageeventhandler Pin
awinash kr6-Aug-16 6:47
awinash kr6-Aug-16 6:47 
AnswerRe: need help on printpageeventhandler Pin
Tushar K. Skype(tushar.kshirsagar24)6-Nov-17 22:53
professionalTushar K. Skype(tushar.kshirsagar24)6-Nov-17 22:53 
QuestionHatur nuhun Pin
Muhamad Rasyid Ridho16-Apr-15 23:49
Muhamad 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
Member 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
Lakhpat-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
Member 113419162-Jan-15 3:42 

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.