PrintPreviewDialog Class
Represents a dialog box form that contains a PrintPreviewControl for printing from a Windows Forms application.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
When you create an instance of the PrintPreviewDialog class, some of the read/write properties are set to initial values. For a list of these values, see the PrintPreviewDialog constructor.
For more information about printing with Windows Forms, see the System.Drawing.Printing namespace overview. If you wish to print from a Windows Presentation Foundation application, see the System.Printing namespace.
The following code example demonstrates the PrintPreviewDialog setting the Document and UseAntiAlias properties. The example assumes the form contains a TreeView named TreeView1 that contains TreeNode objects. The Tag property of each TreeNode object must be set to a fully qualified document name that can be accessed by the machine running the example. Set each TreeNode.Text property to a string that identifies the file specified by the TreeNode.Tag property. For example, you could set TreeNode1.Tag to "c:\myDocuments\recipe.doc" and TreeNode1.Text to "recipe.doc". The example also assumes the form contains a PrintPreviewDialog named PrintPreviewDialog1 and a button named Button1. To run this example, call the InitializePrintPreviewDialog method in the form's constructor or Load event handler.
' Declare the dialog. Friend WithEvents PrintPreviewDialog1 As PrintPreviewDialog ' Declare a PrintDocument object named document. Private WithEvents document As New System.Drawing.Printing.PrintDocument ' Initalize the dialog. Private Sub InitializePrintPreviewDialog() ' Create a new PrintPreviewDialog using constructor. Me.PrintPreviewDialog1 = New PrintPreviewDialog 'Set the size, location, and name. Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300) Me.PrintPreviewDialog1.Location = New System.Drawing.Point(29, 29) Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1" ' Set the minimum size the dialog can be resized to. Me.PrintPreviewDialog1.MinimumSize = New System.Drawing.Size(375, 250) ' Set the UseAntiAlias property to true, which will allow the ' operating system to smooth fonts. Me.PrintPreviewDialog1.UseAntiAlias = True End Sub Private Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button1.Click If (TreeView1.SelectedNode IsNot Nothing) Then ' Set the PrintDocument object's name to the selectedNode ' object's tag, which in this case contains the ' fully-qualified name of the document. This value will ' show when the dialog reports progress. document.DocumentName = TreeView1.SelectedNode.Tag End If ' Set the PrintPreviewDialog.Document property to ' the PrintDocument object selected by the user. PrintPreviewDialog1.Document = document ' Call the ShowDialog method. This will trigger the document's ' PrintPage event. PrintPreviewDialog1.ShowDialog() End Sub Private Sub document_PrintPage(ByVal sender As Object, _ ByVal e As System.Drawing.Printing.PrintPageEventArgs) _ Handles document.PrintPage ' Insert code to render the page here. ' This code will be called when the PrintPreviewDialog.Show ' method is called. ' The following code will render a simple ' message on the document in the dialog. Dim text As String = "In document_PrintPage method." Dim printFont As New System.Drawing.Font _ ("Arial", 35, System.Drawing.FontStyle.Regular) e.Graphics.DrawString(text, printFont, _ System.Drawing.Brushes.Black, 0, 0) End Sub
The following code example assumes the form contains a TreeView object named TreeView1 that contains TreeNode objects. Each TreeNode object’s Tag property must be set to a fully-qualified document name that can be accessed by the machine running the example. Set each Text property to a string that identifies the file specified by the Tag property. For example, you could set TreeNode1.Tag to “c:\myDocuments\recipe.doc” and TreeNode1.Text to “recipe.doc”. It also assumes the form contains a PrintPreviewDialog object named PrintPreviewDialog1 and a button named Button1. To run this example call the InitializePrintPreviewDialog method in the form's constructor.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.