Hi @Alessandro, Welcome to Microsoft Q&A.
Updated in WPF:
private void InvokePrint(object sender, RoutedEventArgs e)
{
// Create the print dialog object and set options
PrintDialog pDialog = new PrintDialog();
PrintTicket printTicket = pDialog.PrintTicket;
printTicket.PageOrientation = PageOrientation.Landscape; // Set the print orientation here
pDialog.PageRangeSelection = PageRangeSelection.AllPages;
pDialog.UserPageRangeEnabled = true;
// Display the dialog. This returns true if the user presses the Print button.
Nullable<Boolean> print = pDialog.ShowDialog();
if (print == true)
{
}
}
Use PrintDialog in C# winform.
You can modify printer settings using PrinterSettings.
The following code modifies printerSettings.DefaultPageSettings directly. If you choose a printer other than the default, you may have to modify the code location. It can be directly placed inside if (printDialog1.ShowDialog() == DialogResult.OK).
private void button1_Click(object sender, EventArgs e)
{
// Get the selected printer settings from the PrintDialog
PrinterSettings printerSettings = printDialog1.PrinterSettings;
// Set the default printer settings
printerSettings.DefaultPageSettings.Landscape = true;
// Create the PrintDialog object
// Show the print dialog
if (printDialog1.ShowDialog() == DialogResult.OK)
{
// Create the PrintDocument object
PrintDocument printDocument = new PrintDocument();
// Set the PrinterSettings for the PrintDocument
printDocument.PrinterSettings = printerSettings;
// Set the PrintPage event handler for the PrintDocument
printDocument.PrintPage += PrintDocument_PrintPage;
// Start the printing process
printDocument.Print();
}
}
private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// Add your printing logic here
// This event is triggered for each page that needs to be printed
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.