Creating a Printing Application using Windows Forms and C#

In this article, we will see how to create a simple printing application in C# and GDI+. The application will call a printer on the machine and print a simple text.
Now let's talk about how to write your first simple printing application. In this application we will send the text "Hello Printer!" to the printer from a Windows application. To create this application, follow the simple steps described here.
Using Visual Studio .NET, create a Windows application project names HelloPrinterSamp, as shown in Figure 11.6.
After we create the project, we add the following line to it:
  1. using System.Drawing.Printing;
Then we add controls for a label, a combo box, and a button to the form. We change the Text and Name properties of the form and these controls. (See the online source code for more details.) The final form should look like Figure 11.7.
When you run this application, the combo box will display the available printer on your machine. You can select any printer from this list, and when you click the Hello Printer button, it will print "Hello Printer!" on your printer.
Figure 11.6.jpg
FIGURE 11.6: Creating a Windows application
Figure 11.7.jpg
FIGURE 11.7: Your first printing application 
We load the available printers on the form's load event handler. The PrinterSetting.InstalledPrinter property returns the installed printers on a machine.
PrinterSettings.InstalledPrinters.Count returns the total number of printers. In Listing 11.2 we check if printers are installed on the machine, read them, and add them to the printer list combo box.
LISTING 11.2: Getting all installed printers
  1. private void Form1_Load(object sender, System.EventArgs e)  
  2. {  
  4.     //See if any printers are installed  
  5.     if (PrinterSettings.InstalledPrinters.Count <= 0)  
  6.     {  
  7.         MessageBox.Show("Printer not found!");  
  8.         return;  
  9.     }  
  11.     //Get all available printers and add them to the combo box  
  12.     foreach (String printer in PrinterSettings.InstalledPrinters)  
  13.     {  
  14.         printersList.Itmes.Add(printer.ToString());  
  15.     }  
  16. }  
The next step is to add code to the Hello Printer button click event handler (see Listing 11.3). This code is responsible for printing. We create a PrintDocument object and set the PrintDocument.PrinterSettings.PrinterName property to the printer selected from the printer list combo box. Then we add print-page event handler and call the PrintDocument.Print method, which prints the document.
LISTING 11.3: The Hello printer button click event handler
  1. private void HelloPrinterBtn_Click(object sender, System.EventArgs e)  
  2. {  
  3.     //Create a PrintDocument object  
  4.     PrintDocument pd = new PrintDocument();  
  6.     //Set PrinterName as the selected printer in the printers list  
  7.     pd. PrinterSetting.PrinterName =  
  8.     printersList.SelectedItem.ToString();  
  10.     //Add PrintPage event handler  
  11.     pd.PrintPage + = new PrintPageEventHandler (pd_PrintPage);  
  13.     //Print the document  
  14.     pd.Print();  
  15. }  
The last step is to add the print-page event handler code (see Listing 11.4). This code is responsible for creating a Graphics object for the printer. It calls the DrawString method, which is responsible for drawing text. First we create a Graphics object from PrintPageEventArgs.Graphics. Then we create Font and SolidBrush objects and call DrawString to draw some text on the printer. The DrawString method takes a string that represents the text to be drawn; the font; a brush; and a layout rectangle that represents the starting point, width, and height of a rectangle for the text.
LISTING 11.4: The print-page event handler
  1. //The PrintPage event handler   
  2. public void pd_PrintPage(object sender, PrintPageEventArgs ev)  
  3. {  
  5.     //Get the Graphics object  
  6.     Graphics g = ev.Graphics;  
  8.     //Create a font Arial with size 16  
  9.     Font font = new Font("Arial", 16);  
  11.     //Create a solid brush with black color  
  12.     SolidBrush brush = new SolidBrush(Color.Black);  
  14.     //Draw "Hello Printer!";  
  15.     g.DrawString("Hello Printer!",  
  16.     font, brush,  
  17.     new Rectangle(20, 20, 200, 100));  
  18. }  
Now you can run the application, select a printer from the list, and click the Hello Printer button. You should see "Hello Printer!" on you printed page.
Read more
Here are some more articles on Printing using C# and GDI+
In this article, we saw how to create a simple printing application in C#.