How to use PrintDialog in WPF

This code snippet shows how to create and use a PrintDialog control in WPF using C#.

The PrintDialog is defined in System.Windows.Controls namespace. First you create the control using PrintDialog class and set its properties such as PageRangeSelection and UserPageRangeEnabled.

The ShowDialog method displays the dialog.


Configure printer dialog box
System.Windows.Controls.PrintDialog dlg = new System.Windows.Controls.PrintDialog();
dlg.PageRangeSelection = PageRangeSelection.AllPages;
dlg.UserPageRangeEnabled = true;

// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process save file dialog box results
if (result == true)
{
    // Print document
}

For more