Print with Dialog
Use the ShowPrintDialog
method to display the settings dialog before printing the document. This can be handy for presenting GUI print settings.
Here's an example in C#:
using System;
using System.Drawing.Printing;
class Program
{
static void Main()
{
// Create a PrintDocument instance
PrintDocument printDocument = new PrintDocument();
// Assign an event handler for the PrintPage event
printDocument.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);
// Display the print dialog
PrintDialog printDialog = new PrintDialog
{
Document = printDocument
};
// Show the dialog to the user and check if the user pressed OK
if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// Print the document
printDocument.Print();
}
}
// Event handler responsible for printing the page
private static void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// Define what will be printed on the page
string textToPrint = "Hello, world! This is a test print.";
Font printFont = new Font("Arial", 12);
// Specify the area to draw the string
PointF drawLocation = new PointF(50, 50);
// Draw the string on the page
e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, drawLocation);
}
}
using System;
using System.Drawing.Printing;
class Program
{
static void Main()
{
// Create a PrintDocument instance
PrintDocument printDocument = new PrintDocument();
// Assign an event handler for the PrintPage event
printDocument.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);
// Display the print dialog
PrintDialog printDialog = new PrintDialog
{
Document = printDocument
};
// Show the dialog to the user and check if the user pressed OK
if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// Print the document
printDocument.Print();
}
}
// Event handler responsible for printing the page
private static void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// Define what will be printed on the page
string textToPrint = "Hello, world! This is a test print.";
Font printFont = new Font("Arial", 12);
// Specify the area to draw the string
PointF drawLocation = new PointF(50, 50);
// Draw the string on the page
e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, drawLocation);
}
}
Imports System
Imports System.Drawing.Printing
Friend Class Program
Shared Sub Main()
' Create a PrintDocument instance
Dim printDocument As New PrintDocument()
' Assign an event handler for the PrintPage event
AddHandler printDocument.PrintPage, AddressOf PrintDocument_PrintPage
' Display the print dialog
Dim printDialog As New PrintDialog With {.Document = printDocument}
' Show the dialog to the user and check if the user pressed OK
If printDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
' Print the document
printDocument.Print()
End If
End Sub
' Event handler responsible for printing the page
Private Shared Sub PrintDocument_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
' Define what will be printed on the page
Dim textToPrint As String = "Hello, world! This is a test print."
Dim printFont As New Font("Arial", 12)
' Specify the area to draw the string
Dim drawLocation As New PointF(50, 50)
' Draw the string on the page
e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, drawLocation)
End Sub
End Class
Explanation
- PrintDocument Creation: We create an instance of
PrintDocument
, which is used to handle the document to be printed. - Event Handler: We attach an event handler (
PrintDocument_PrintPage
) to manage the actions to be taken when a page is printed. - PrintDialog Setup: A
PrintDialog
is initialized and assigned to thePrintDocument
. This dialog allows users to configure their printing preferences. - Executing Print: We show the print dialog using
ShowDialog()
. If the user confirms the settings by clicking OK, the document is sent to the printer usingprintDocument.Print()
. - PrintPage Event: Within the event handler (
PrintDocument_PrintPage
), we define what content appears on the printed page using graphics methods, such asDrawString
, to render text onto the page.