Recipe 11.4. Creating a Print Preview


You want to present a preview of a printed document to the user.


Sample code folder: Chapter 11\PrintPreview

Use the PrintPreviewDialog class to show the print preview through a form that includes some basic presentation features.


The following code displays a basic text string on a print preview document:

 Imports System.Drawing.Printing Public Class Form1    Private WithEvents SampleDoc As Printing.PrintDocument    Private Sub Button1_Click( _          ByVal sender As System.Object, _          ByVal e As System.EventArgs) Handles Button1.Click       ' ----- Initiate a print preview.       Dim previewMode As New PrintPreviewDialog       ' ----- Create the document to preview.       SampleDoc = New Printing.PrintDocument       ' ----- Show the preview.       previewMode.Document = SampleDoc       previewMode.ShowDialog()    End Sub    Private Sub SampleDoc_PrintPage(ByVal sender As Object, _          ByVal e As Printing.PrintPageEventArgs) _            Handles SampleDoc.PrintPage       ' ----- Generate a fun one-page document.       e.Graphics.DrawString("Preview is Fun!", _          New Font("Ariel", 48, FontStyle.Regular), _          Brushes.Black, 0, 0)       e.HasMorePages = False    End Sub End Class 

Running this sample code (by clicking on a button named Button1) results in the print preview window shown in Figure 11-1.

Figure 11-1. Print preview in action

The .NET Framework includes a generalized printing system that allows you to use the same code for both the previewing and the actual printing. All printing is done to a generic graphics surface within a PrintDocument object. .NET uses this surface to print to your printer's paper and to the artificial paper in the print preview form.

The PrintPreviewDialog class also comes in a Windows Formsbased control variation (see Figure 11-2). You can add this control and a related PrintDocument control to your form and generate the print preview that way, but it works just the same. You assign the PrintDocument control to the PrintPreviewDialog's Document property, and then respond to the PrintDocument's PrintPage event. It's the exact same code that appears in this recipe's solution; only the declarations of the PrintPreviewDialog and PrintDocument objects have moved from your source code to the form's surface.

Figure 11-2. The control version of the PrintPreviewDialog class

.NET includes two classes that let you preview your own printed documents. The easiest to use is the PrintPreviewDialog class, as demonstrated in this recipe. It defines a complete form, and it includes some useful controls in the form of a tool-bar. But it's a one-size-fits-all solution. Altering the toolbar to include your own set of custom features isn't really an option.

The alternative uses the PrintPreviewControl class, or, more commonly, its equivalent Windows Forms control. By adding this control to an existing form along with any other toolbar-type controls you wish, you can provide an enhanced print preview experience custom-designed for your application.

See Also

Recipe 11.6 provides additional examples of using the PrintDocument class.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400 © 2008-2017.
If you may any questions please contact us: