* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
6.1 KiB
title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | description | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||
|---|---|---|---|---|---|---|---|---|---|---|
| Print Using Print Preview | Learn how to add print preview services to your application by using the Windows Forms PrintPreviewDialog control. | 03/30/2017 |
|
|
4a16f7e2-ae10-4485-b0ae-3d558334d0fe |
How to: Print in Windows Forms Using Print Preview
It is very common in Windows Forms programming to offer print preview in addition to printing services. An easy way to add print preview services to your application is to use a xref:System.Windows.Forms.PrintPreviewDialog control in combination with the xref:System.Drawing.Printing.PrintDocument.PrintPage event-handling logic for printing a file.
To preview a text document with a PrintPreviewDialog control
-
Add a xref:System.Windows.Forms.PrintPreviewDialog, xref:System.Drawing.Printing.PrintDocument, and two strings to your form.
[!code-csharpSystem.Drawing.Printing.PrintPreviewExample#1] [!code-vbSystem.Drawing.Printing.PrintPreviewExample#1]
-
Set the xref:System.Drawing.Printing.PrintDocument.DocumentName%2A property to the document you wish to print, and open and read the document's contents to the string you added previously.
[!code-csharpSystem.Drawing.Printing.PrintPreviewExample#2] [!code-vbSystem.Drawing.Printing.PrintPreviewExample#2]
-
As you would for printing the document, in the xref:System.Drawing.Printing.PrintDocument.PrintPage event handler, use the xref:System.Drawing.Printing.PrintPageEventArgs.Graphics%2A property of the xref:System.Drawing.Printing.PrintPageEventArgs class and the file contents to calculate lines per page and render the document's contents. After each page is drawn, check to see if it is the last page, and set the xref:System.Drawing.Printing.PrintPageEventArgs.HasMorePages%2A property of the xref:System.Drawing.Printing.PrintPageEventArgs accordingly. The xref:System.Drawing.Printing.PrintDocument.PrintPage event is raised until xref:System.Drawing.Printing.PrintPageEventArgs.HasMorePages%2A is
false. When the document has finished rendering, reset the string to be rendered. Also, make sure the xref:System.Drawing.Printing.PrintDocument.PrintPage event is associated with its event-handling method.Note
You may have already completed steps 2 and 3 if you have implemented printing in your application.
In the following code example, the event handler is used to print the "testPage.txt" file in the same font used on the form.
[!code-csharpSystem.Drawing.Printing.PrintPreviewExample#3] [!code-vbSystem.Drawing.Printing.PrintPreviewExample#3]
-
Set the xref:System.Windows.Forms.PrintPreviewDialog.Document%2A property of the xref:System.Windows.Forms.PrintPreviewDialog control to the xref:System.Drawing.Printing.PrintDocument component on the form.
[!code-csharpSystem.Drawing.Printing.PrintPreviewExample#5] [!code-vbSystem.Drawing.Printing.PrintPreviewExample#5]
-
Call the xref:System.Windows.Forms.CommonDialog.ShowDialog%2A method on the xref:System.Windows.Forms.PrintPreviewDialog control. You would typically call xref:System.Windows.Forms.CommonDialog.ShowDialog%2A from the xref:System.Windows.Forms.Control.Click event-handling method of a button. Calling xref:System.Windows.Forms.CommonDialog.ShowDialog%2A raises the xref:System.Drawing.Printing.PrintDocument.PrintPage event and renders the output to the xref:System.Windows.Forms.PrintPreviewDialog control. When the user clicks the print icon on the dialog, the xref:System.Drawing.Printing.PrintDocument.PrintPage event is raised again, sending the output to the printer instead of the preview dialog. This is why the string is reset at the end of the rendering process in step 3.
The following code example shows the xref:System.Windows.Forms.Control.Click event-handling method for a button on the form. This event-handling method calls the methods to read the document and show the print preview dialog.
[!code-csharpSystem.Drawing.Printing.PrintPreviewExample#4] [!code-vbSystem.Drawing.Printing.PrintPreviewExample#4]
Example
[!code-csharpSystem.Drawing.Printing.PrintPreviewExample#0] [!code-vbSystem.Drawing.Printing.PrintPreviewExample#0]
Compiling the Code
This example requires:
- References to the System, System.Windows.Forms, System.Drawing assemblies.