Setting Up and Previewing Printing
As opposed to directly printing a file, a user may want to perform some preliminary preparation on the document or the printer. Microsoft Windows provides…
The Page Setup Dialog Box#
Introduction#
Instead of printing a document straight away, users might need to do some setup for the file or the hardware. Windows provides a specific dialog for this purpose, known as Page Setup.
To provide a Page Setup to your application, you can use the PageSetupDialog button from the Toolbox. The PageSetupDialog control is based on the PageSetupDialog class which derives from the CommonDialog class.
Characteristics of the Page Setup Dialog Box#
When using the Page Setup dialog box, the user must first select a printer. This is usually done already by the operating system that selects the default printer of the computer that called this dialog box. Otherwise, to select a printer or to change the printer, the user can click the Printer button and select or change it using the Name combo box:
Displaying the Page Setup Printer dialog box also allows you and/or the user to customize the printing process if necessary. If you want to do this, you can use the PageSetupDialog.PrinterSettings property which is a value of the PrinterSettings class reviewed earlier. After selecting the printer, the user can click OK. The options of the Page Setup dialog box depend on the driver of the printer selected in the Name combo box. The Page Setup dialog box allows the user to customize the appearance of the paper on which the document would be printed. On the Page Setup, the user can click the arrow of the Size combo box and select one of the configured sizes of paper. The characteristics of the paper are controlled by the PageSettings class that we mentioned earlier. For example, if the printer has many trays, as indicated by the driver of the selected printer, the user can select which tray would be used when printing. As it happens, one printer can have only one tray while another printer can have 3, 5, or more trays. If the desired printer is on a network, the user can click the Network button to locate it. To programmatically show or hide the Network button, specify a false or true result to the PageSetupDialog.ShowNetwork Boolean property. The user also has the option to print the document in Portrait (vertical) or in Landscape (horizontal) position. The option to allow the user to select Portrait or Landscape is controlled by the AllowOrientation Boolean property.
Introduction to Print Preview#
Introduction#
If you use the printing process that solely involves the print dialog box, you may send a document to the printer without knowing what the printed document would look like on the piece of paper. In the same way, the user would have to simply accept the way you designed the printed document to appear. One way you may assist the user consists of displaying a preview of what the printed sheet would look like. This remains the idea behind the concept of print preview. Print preview consists of displaying, on the computer monitor, a sample representation of what the document would look like once printed.
Providing Print Preview#
Print preview is primarily a technique of drawing a sample printed sheet on a form. It is implemented by the PrintPreviewDialog button from the Toolbox. Therefore, at design time, to provide print preview, from the Printing section of the Toolbox and click the form. As its name indicates, the dialog box is already created but like the other dialog boxes of the .NET Framework, you must add it to a form in order to make it available in your application.
In the .NET Framework, print preview is implemented through the PrintPreviewDialog class. This class is derived from the Form class. Based on this, to programmatically create a print preview, you can start by declaring a variable of type PrintPreviewDialog. Here is an example:
using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
Button btnPrintPreview;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnPrintPreview = new Button();
btnPrintPreview.Location = new Point(12, 12);
btnPrintPreview.Text = "&Print Preview...";
btnPrintPreview.Width = 100;
btnPrintPreview.Click += new EventHandler(PreviewDocumentClick);
Controls.Add(btnPrintPreview);
}
void PreviewDocumentClick(object sender, EventArgs e)
{
PrintPreviewDialog dlgPrint = new PrintPreviewDialog();
}
}
public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}As a dialog-based object, to display the print preview, the PrintPreviewDialog class inherits the ShowDialog() method from its parent the Form class. Here is an example:
void PreviewDocumentClick(object sender, EventArgs e)
{
PrintPreviewDialog dlgPrintPreview = new PrintPreviewDialog();
dlgPrintPreview.ShowDialog();
}This would produce:
Characteristics of the Print Preview#
The Preview Area#
The Print Preview window is designed as a complete form that includes a toolbar, a designated preview area, and scroll bars. The preview section gives a visual representation of how the page will look once printed. If the dialog hasn't been given something to display, it shows a message saying the document is empty. To show content, you must design it first; this is handled by the PrintPreviewDialog's Document property, which requires a PrintDocument object.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
public class Exercise : Form
{
Button btnPrintPreview;
PrintPreviewDialog dlgPrintPreview;
PrintDocument docPrint;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnPrintPreview = new Button();
btnPrintPreview.Location = new Point(12, 12);
btnPrintPreview.Text = "&Print Preview...";
btnPrintPreview.Width = 100;
btnPrintPreview.Click += new EventHandler(PreviewDocumentClick);
Controls.Add(btnPrintPreview);
dlgPrintPreview = new PrintPreviewDialog();
docPrint = new PrintDocument();
dlgPrintPreview.Document = docPrint;
}
void PreviewDocumentClick(object sender, EventArgs e)
{
dlgPrintPreview.ShowDialog();
}
}
public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}This would produce:
As you can see, simply assigning a PrintDocument object to a print preview form only creates a blank sheet. In order to show a preview, you must design it. To make this possible, the PrintDocument class To assist you with actually designing what you want to display in the preview area, the PrintDocument class fires an event named PrintPage. This event is of type PrintPageEventArgs. The PrintPageEventArgs class is equipped with a property named Graphics, which is of type Graphics. You can then use your knowledge of the Graphics class to create or design the preview. Here is an example:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
public class Exercise : Form
{
Button btnPrintPreview;
PrintPreviewDialog dlgPrintPreview;
PrintDocument docPrint;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnPrintPreview = new Button();
btnPrintPreview.Location = new Point(12, 12);
btnPrintPreview.Text = "&Print Preview...";
btnPrintPreview.Width = 100;
btnPrintPreview.Click += new EventHandler(PreviewDocumentClick);
Controls.Add(btnPrintPreview);
dlgPrintPreview = new PrintPreviewDialog();
docPrint = new PrintDocument();
docPrint.PrintPage += new PrintPageEventHandler(docPrintPage);
dlgPrintPreview.Document = docPrint;
}
void PreviewDocumentClick(object sender, EventArgs e)
{
dlgPrintPreview.ShowDialog();
}
void docPrintPage(object sender, PrintPageEventArgs e)
{
Image imgPerson = Image.FromFile(@"E:\Programs\persons1.gif");
e.Graphics.DrawImage(imgPerson, 10, 10);
}
}On our computer, this produced:
Printing From the Print Preview#
To print the contents of the preview area, the user can click the Print button from the toolbar. Two things would happen. The compiler would select the default printer and the document would be sent directly to that printer. This means that, first there should be a (known) default printer and the user should know what that printer is; second, the user would not be able to change the printer if more than one is available. If you want the user to be able to select the printer, you should provide a Print dialog box that the user can probably access from a menu of the application.
Zooming the Preview#
By default, when the print preview window appears to the user, it assumes some default dimensions that may make it small. Because it is derived from the Form class, you can maximize it if you want. Here is an example:
void PreviewDocumentClick(object sender, EventArgs e)
{
dlgPrintPreview.WindowState = FormWindowState.Maximized;
dlgPrintPreview.ShowDialog();
}If the print preview is not maximized, the content of the preview area may appear (too) small for the user, especially if it is made of text. To enlarge it, the user has two alternatives. If the user maximizes the window, the preview area would also be enlarged and the content would be easier to see. As an alternative, the user can click the arrow of the Zoom button. This would display a list of the zoom rates:
The user may then click one of the values.
A Document of Various Pages#
Previously, we assumed the document was only one page long, but many files extend over several pages. While the preview initially shows just one page, users can choose to view multiple pages simultaneously or arrange them in different ways. The toolbar provides several options for this, including buttons for displaying up to six pages at once. Once finished, the user can simply close the preview.

