PrintPreviewControl In C#

The PrintPreviewDialog control displays the Print Preview Dialog with all buttons and settings so the users can change their settings before a document goes to the printer. But there are circumstances when you would need to just display print preview without any buttons and options and show only the preview. This is where the PrintPreviewControl can be used.

The PrintPreviewControl control represents the raw part of print preview of a document that is ready go to the printer. In this article, I will discuss how to use a PrintPreviewControl control to display print previews of documents in Windows Forms applications.

Creating a PrintPreviewControl

PrintPreviewControl is defined in the System.Drawing.Printing namespace. You must import this namespace before using the PrintPreviewControl class.

We can create a PrintPreviewControl by dragging the control from the Toolbox onto a Form or we can use the PrintPreviewControl class.

The following code snippet creates a PrintPreviewControl, sets it's properties and adds control to the Form by calling Controls.Add() method.

C# Code

  1. private PrintPreviewControl ppc;  
  2. private PrintDocument docToPrint = new PrintDocument();        
  3.   
  4. private void CreatePrintPreviewControl()  
  5. {  
  6.     ppc = new PrintPreviewControl();    
  7.     ppc.Name = "PrintPreviewControl1";  
  8.     ppc.Dock = DockStyle.Fill;  
  9.     ppc.Location = new Point(88, 80);             
  10.     ppc.Document = docToPrint;  
  11.     ppc.Zoom = 0.25;  
  12.     ppc.Document.DocumentName = "c:\\";  
  13.     ppc.UseAntiAlias = true;  
  14.   
  15.     // Add PrintPreviewControl to Form  
  16.     Controls.Add(this.ppc);             
  17. }  

Adding the PrintDocument.PrintPage Event Handler

Remember, a PrintPreviewControl displays preview only and does not print a document. To print a document, we must implement the PrintDocument.PrintPage event handler. The following code snippet adds a PrintDocument.PrintPage event handler and prints some text using the DrawString method of the Graphics object.

C# Code

  1. // Add PrintDocument PrintPage event handler  
  2. this.docToPrint.PrintPage +=  
  3.     new System.Drawing.Printing.PrintPageEventHandler(PrintPage);  
  4.   
  5. private void PrintPage(object sender, PrintPageEventArgs e)  
  6. {  
  7.     string text = "This text to be printed. ";  
  8.     e.Graphics.DrawString(text, new Font("Georgia", 35, FontStyle.Bold),  
  9.         Brushes.Black, 10, 10);  
  10. }  
  11.   
  12.    

The output looks like Figure 1 where the print preview of a document to be printed is displayed. 

PrintPreviewImg1.jpg

Summary

In this article, we discussed how to use a PrintPreviewControl control to display print preview of documents in Windows Forms applications. Download the attached project to learn more about this control.

Further Readings

Here is a list of top articles and tutorials about printing you will want to read to learn more about printing.


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.