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 2005; create a new Windows application project using C#:
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.
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!
Switch to code view and import the following namespace:
Declare the following member variables:
private Font f_title;
private Font f_body;
private int intPageCounter;
private PrintDocument printDoc;
When the form is loaded create a new instance of the PrintDocument and wire up the three events described earlier:
private void Form1_Load(object sender, EventArgs e)
printDoc = new PrintDocument();
printDoc.DocumentName = "Printing from Windows forms";
printDoc.BeginPrint += new PrintEventHandler(printDoc_BeginPrint);
printDoc.EndPrint += new PrintEventHandler(printDoc_EndPrint);
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
void printDoc_BeginPrint(object sender, PrintEventArgs e)
f_title = new Font("Arial", 16, FontStyle.Bold);
f_body = new Font("Times New Roman", 10);
void printDoc_EndPrint(object sender, PrintEventArgs e)
f_title = null;
f_body = null;
Finally in the PrintPage event write the following code:
void printDoc_PrintPage(object sender, PrintPageEventArgs e)
//display a title
e.Graphics.DrawString("Printing from Windows form", f_title, Brushes.Black, 20, 30);
//now draw a border
Rectangle border = new Rectangle(10, 10, 500, 240);
//now draw our image
if (this.pictureBox1.Image != null)
e.Graphics.DrawImage(this.pictureBox1.Image, 20, 60, this.pictureBox1.Size.Width, this.pictureBox1.Size.Height);
//finally display the page number
e.Graphics.DrawString("Page " + this.intPageCounter.ToString(), f_body, Brushes.Black, 20, 220);
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:
private void btnPrint_Click(object sender, EventArgs e)
PrintPreviewDialog printDialog = new PrintPreviewDialog();
printDialog.Document = this.printDoc;
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: