Managing printing in Windows Applications

Enable printing in your .net windows applications:
 
Supporting printing in your .net application is a simple process. In this article I'll show you of printing in your .net 2.0, how you can configure page setup, print multiple pages, and preview a document before being printed, as well as let users select a printer to which to print.
 
Creating the sample application:
 
Open Visual Studio; create a new Windows application project using C#:
 
 
Figure 1.
 
In .NET, all the printing functionality is encapsulated within the PrintDocument control which can be found in the ToolBox. The PrintDocument control defines various methods that allow you to send output to the printer.
 
To incorporate printing functionality into you Windows application, you can simply drag and drop the PrintDocument control on to your form (see Figure 2), or create an instance of the PrintDocument class during runtime.
 
 
Figure 2
 
There are generally three events that you need to get acquainted with. They are:
  • BeginPrint Occurs when the Print() method is called and before the first page of the document prints. Typically, you make use of the BeginPrint event to initialize fonts, file streams, and other resources used during the print process.
  • PrintPage Occurs when the output to print current page is needed. This is the main event to code the logic required for sending the output to the printer.
  • EndPrint Occurs when the last page of the document has printed. Typically, you use the EndPrint event to release fonts, file streams, and other resources that can be used during the print process.
Now to the default Form1, populate a PictureBox and a Button control. Set the PictureBox to display an image. Here I have displayed a logo which I took from the internet!
 
 
Figure 3.
 
Switch to code view and import the following namespace:
  1. using System.Drawing.Printing;  
Declare the following member variables:
  1. private Font f_title;    
  2. private Font f_body;  
  3. private int intPageCounter;  
  4. private PrintDocument printDoc;  
When the form is loaded create a new instance of the PrintDocument and wire up the three events described earlier:
  1. private void Form1_Load(object sender, EventArgs e)  
  2. {  
  3.     printDoc = new PrintDocument();  
  4.     printDoc.DocumentName = "Printing from Windows forms";   
  5.     printDoc.BeginPrint += new PrintEventHandler(printDoc_BeginPrint);  
  6.     printDoc.EndPrint += new PrintEventHandler(printDoc_EndPrint);  
  7.     printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);  
  8. }  
  9. void printDoc_BeginPrint(object sender, PrintEventArgs e)  
  10. {  
  11.     f_title = new Font("Arial", 16, FontStyle.Bold);  
  12.     f_body = new Font("Times New Roman", 10);  
  13. }  
  14. void printDoc_EndPrint(object sender, PrintEventArgs e)  
  15. {  
  16.     f_title = null;  
  17.     f_body = null;  
  18. }  
Finally in the PrintPage event write the following code:
  1. void printDoc_PrintPage(object sender, PrintPageEventArgs e)  
  2. {  
  3.     //display a title  
  4.     e.Graphics.DrawString("Printing from Windows form", f_title, Brushes.Black, 20, 30);  
  5.     //now draw a border  
  6.     Rectangle border = new Rectangle(10, 10, 500, 240);  
  7.     e.Graphics.DrawRectangle(Pens.Black, border);  
  8.     //now draw our image  
  9.     if (this.pictureBox1.Image != null)  
  10.     {  
  11.         e.Graphics.DrawImage(this.pictureBox1.Image, 20, 60, this.pictureBox1.Size.Width, this.pictureBox1.Size.Height);  
  12.     }  
  13.     //finally display the page number  
  14.     e.Graphics.DrawString("Page " + this.intPageCounter.ToString(), f_body, Brushes.Black, 20, 220);  
  15.     this.intPageCounter++;  
  16. } 
Previewing the print out
 
Instead of sending the output directly to the printer, it is more friendly to let the user preview the output before sending it to the printer. To accomplish this functionality, we use the PrintPreviewDialog() class:
  1. private void btnPrint_Click(object sender, EventArgs e)  
  2. {  
  3.     PrintPreviewDialog printDialog = new PrintPreviewDialog();  
  4.     printDialog.Document = this.printDoc;  
  5.     printDialog.ShowDialog();  
  6. }
 
Figure 4.
 
When you click on the printer icon on the left corner of the PrintPreview window, all events of the print document discussed earlier will be fired again.
 
Now if you have multiple pages to print, then we can give the user the option to print a range or series of pages. User can enter the required series of pages to print, and the individual pages will be printed.
 
In the default form add a label and two text boxes controls as shown in the following figure:
 
 
Figure 5.
 
Now in the BeginPrint event handler initialize the intPageCounter as shown below:
  1. this.intPageCounter = int.Parse(this.txtFrom.Text);  
In the PrintPage event you will determine if there are any more pages to print. If there are pages to be printed you'll set the HasMorePages of the PrintPageEventArgs class to true; this will cause the PrintPage event to be fired one more time. Once there are no more pages to be printed, you'll set the HasMorePages propterty to false.
  1. this.intPageCounter++;   
  2. if (this.intPageCounter <= int.Parse(this.txtTo.Text))  
  3.     e.HasMorePages = true;  
  4. else  
  5. {  
  6.     e.HasMorePages = false;  
  7. } 
Selecting a printer
 
Finally, if you have multiple printers attached to your computer, it is a good idea to let the user choose the desired printer instead of sending the output to the default printer.
  1. PrintDialog pDialog = new PrintDialog();  
  2. pDialog.AllowSomePages = true;  
  3. if (pDialog.ShowDialog(this) == DialogResult.OK)  
  4.     this.printDoc.Print();  
The following figure shows the PrintDialog when it is shown

 
Figure 6.
 
Summary
 
In this article, you have seen the basic steps of enabling printing in your windows applications.


Similar Articles