8.16 Show a Dynamic Print Preview


Problem

You need to use an on-screen preview that shows how a printed document will look.

Solution

Use PrintPreviewDialog or PrintPreviewControl (both of which are found in the System.Windows.Forms namespace).

Discussion

.NET provides two controls that can take a PrintDocument instance, run your printing code, and use it to generate a graphical on-screen preview. These controls are

  • The PrintPreviewDialog , which shows a preview in a standalone window.

  • The PrintPreviewControl , which shows a preview in one of your own custom forms.

To use a standalone print preview, you simply create a PrintPrevewDialog object, assign the document, and call the PrintPreviewDialog.Show method.

 PrintPreviewDialog dlgPreview = new PrintPreviewDialog(); dlgPreview.Document = doc; dlgPreview.Show(); 

The print preview window (shown in Figure 8.10) provides all the controls the user needs to move from page to page, zoom in, and so on. The window even provides a print button that allows the user to send the document directly to the printer. You can tailor the window to some extent by modifying the PrintPreviewDialog properties.

You can also add PrintPreviewControl to any of your forms to show a preview alongside other information. In this case, you don't need to call the Show method. As soon as you set the PrintPreviewControl.Document property, the preview is generated. To clear the preview, set the Document property to null , and to refresh the preview, simply reassign the Document property. PrintPreviewControl only shows the preview pages, not any additional controls. However, you can add your own controls for zooming, tiling multiple pages, and so on. You simply need to adjust the PrintPreviewControl properties accordingly .

For example, consider the form shown in Figure 8.11. It incorporates a PrintPreviewControl and allows the user to select a zoom setting.

click to expand
Figure 8.11: The PrintPreviewControl in a custom window.

Here's the complete form code:

 using System; using System.Windows.Forms; using System.Drawing; using System.Drawing.Printing; public class PrintPreview : System.Windows.Forms.Form {     private System.Windows.Forms.PrintPreviewControl printPreviewControl;     private System.Windows.Forms.Button cmdPreview;     private System.Windows.Forms.ListBox lstZoom;     private System.Windows.Forms.Label label1;     // (Designer code omitted.)     private PrintDocument doc;     // (PrintDocument.PrintPage event handler code omitted.     // See code in recipe 8.14.)     private void PrintPreview_Load(object sender, System.EventArgs e) {              // Set the allowed zoom settings.         for (int i=1; i <= 10; i++) {             lstZoom.Items.Add((i * 10).ToString());         }         // Create a document with 100 lines.         string[] printText = new string[100];         for (int i=0; i < 100; i++) {             printText[i] = i.ToString();             printText[i] += ": The quick brown fox jumps over the lazy dog.";         }         doc = new TextDocument(printText);         doc.PrintPage += new PrintPageEventHandler(this.Doc_PrintPage);         lstZoom.Text = "100";         printPreviewControl.Zoom = 1;         printPreviewControl.Document = doc;         printPreviewControl.Rows = 2;     }     private void cmdPreview_Click(object sender, System.EventArgs e) {              // Set the zoom.         printPreviewControl.Zoom = Single.Parse(lstZoom.Text) / 100;         // Show the full two pages, one above the other.         printPreviewControl.Rows = 2;         // Rebind the PrintDocument to refresh the preview.         printPreviewControl.Document = doc;     } } // (TextDocument class code omitted. See recipe 8.14.) 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net