Home

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 can assist the user consists of displaying a preview of what the printed sheet would look like. This is 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 Print Preview Dialog 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:

Print Preview

 

Home Copyright © 2007-2012 FunctionX Next