Monday, August 25, 2008

Printing Through .NET

Any sophisticated application would definitely have support for printing. .NET facilitates printing with the help of a PrintDocument component. Along with this we can make use of the various dialog boxes that are commonly used for printing.

The PrintDocument Component

.NET supports printing through methods and properties of the PrintDocument component. The PrintDocument component is available in the Toolbox and when added to a form, it appears in the component tray at the bottom of the Windows Forms Designer. This is because the PrintDocument component is not visible at runtime.

The PrintDocument class represents the PrintDocument component and falls under the System.Drawing.Printing namespace. Typically in a printing application we call the Print( ) method of the PrintDocument class. It also contains properties such as DefaultPageSettings, which gets or sets page settings that are used as defaults for all pages to be printed, PrinterSettings, which gets or sets the printer that prints the document. The class contains events like BeginPrint, EndPrint and PageEvent. BeginPrint is raised when the Print( ) method is called and before the first page of the document is printed. Here we can do any initializations if needed. EndPrint event is raised when the last page of the document has been printed. We can do cleaning up jobs here. The PrintPage event is raised when it’s time to print the current page. We can write code in the handler of this event to do the desired printing. Hence the order of events raised when Print( ) method is called is BeginPrint, PrintPage and then EndPrint. In the following sections, we have programmed simple examples that illustrate how to use the PrintDocument component to achieve printing.

Printing A Salary Slip

Let us see an example where we wish to print salary slip of an employee. Here we have created a Windows Form Application named Salaryslip. We have added 5 textboxes to the form to gather information about the employee—a ‘Name’ textbox named name, a ‘Month’ textbox named month, a ‘Gross Salary’ textbox named sal, a ‘Tax’ textbox named tax and a ‘Total’ textbox named total. We have added these textboxes to collect information about an employee like his name, his salary, the tax he needs to pay, etc. Next we have calculated his salary according to the tax. We have also added two buttons to the form—a ‘Print’ button named print and a ‘Calculate’ button named cal. As soon as the user clicks on the Calculate button the salary gets calculated and if he clicks on the Print button the salary slip gets printed. Next we have added the PrintDocument component to our form. It gets added in the component tray. We have changed the name of the component from PrintDocument1 to mypdoc.

We have added an event handler called cal_Click( ) to handle the Click event of the Calculate button. This handler is shown below:

private void cal_Click ( object sender, System.EventArgs e )

int t = int.Parse ( sal.Text ) - int.Parse ( tax.Text ) ;
total.Text = t.ToString( ) ;


Here we have subtracted the tax from the gross salary and stored it in an integer variable called t. Then we have set the Text property of the textbox named total to t (we converted t from an integer to a string). On clicking the Print button the event handler called print_Click( ) would get called. This handler is shown below:

private void print_Click ( object sender, System.EventArgs e )

mypdoc.Print( ) ;


In this handler we have simply called the Print( ) method of the PrintDocument class. Whenever the Print( ) method is called the events are raised. We have not added handlers for the BeginPrint and EndPrint handlers. We have added a handler for the PrintPage event. If we double click on mypdoc in the component tray of the Windows Form Designer, the mypdoc_PrintPage( ) event handler gets added automatically to the code. This handler is shown below:

private void mypdoc_PrintPage ( object sender, System.Drawing.Printing.PrintPageEventArgs e )

Graphics g = e.Graphics ;
SolidBrush b = new SolidBrush ( Color.Black ) ;
Pen p = new Pen ( Color.Black, 3 ) ;
Font f = new Font ( "Arial", 10 ) ;

g.DrawString ( "Name", f, b, 30, 50 ) ;
g.DrawString ( empname.Text, f, b, 130, 50 ) ;

g.DrawString ( "Month", f, b, 30, 70 ) ;
g.DrawString ( month.Text, f, b, 130, 70 ) ;

g.DrawString ( "Gross Salary", f, b, 30, 100 ) ;
g.DrawString ( sal.Text, f, b, 130, 100 ) ;

g.DrawString ( "Tax", f, b, 30, 120 ) ;
g.DrawString ( tax.Text, f, b, 130, 120 ) ;

g.DrawString ( "Total", f, b, 30, 140 ) ;
g.DrawString ( total.Text, f, b, 130, 140 ) ;

Font f1 = new Font ( "Arial", 15, FontStyle.Bold ) ;
g.DrawString ( "KNK Pvt Ltd", f1, b, 50, 10 ) ;

g.DrawRectangle ( p, 14, 4, 180, 175 ) ;
p.Width = 2 ;
g.DrawRectangle ( p, 22, 95, 165, 75 ) ;


PrintPageEventsArgs provides data for the PrintPage event. Using this class we have retrieved the Graphics object using which we will do the painting. Next we have created Pen, Brush and Font objects. Then using these objects we have printed the strings.
Run the program. Enter the salary and tax and click the Calculate button. The result would get displayed in the Total text box. Figure 1 shows the form with salary details.

Figure 1

As soon as we click the Print button, the ‘Printing’ dialog indicating the number of page that is getting printed gets displayed. The salary slip that gets printed is shown in Figure 2.

Figure 2

No comments:

Your Title