You want to slightly alter the output to the print surface when the user is printing to either the preview window or the actual printer.
Access the PrintDocument object's PrintController.IsPreview property during printing to determine if you are in preview mode or not.
The following code prints a "preview only" message at the top of each page when printing in preview mode:
Private Sub SampleDoc_PrintPage(ByVal sender As Object, _ ByVal e As Printing.PrintPageEventArgs) _ Handles SampleDoc.PrintPage ' ----- Print a "preview only" message. If (SampleDoc.PrintController.IsPreview = True) Then _ e.Graphics.DrawString("This is a preview only.", _ New Font("Ariel", 12, FontStyle.Regular), _ Brushes.Red, 0, 0) ' ----- Add other printing code here. End Sub
.NET includes two different ways to determine the print-preview status of the current PrintDocument object. The PrintDocument.PrintController.IsPreview property is a simple Boolean value that can be read at any time during the printing process.
During printing, you can also access the e.PrintAction property in the PrintDocument object's QueryPageSettings event to determine the printer-output target. This property uses the three possible values of the System.Drawing.Printing.PrintAction enumeration:
The following code checks the PrintAction flag for a PrintDocument object named SampleDoc and takes action based on its value:
Private Sub SampleDoc_QueryPageSettings( _ ByVal sender As Object, ByVal e As _ System.Drawing.Printing.QueryPageSettingsEventArgs) _ Handles SampleDoc.QueryPageSettings If (e.PrintAction = PrintAction.PrintToPreview) Then ' ----- Take preview-specific action here. End If End Sub
This property is available only from the QueryPageSettings event. If you want to access its value during the PrintPage event, you will have to save it in a class-level or global variable during the QueryPageSettings event.