I have a simple printing solution set up and normal printing works fine(tested it a couple of times), however when I use the PrintDialog to specify a custom page range, it is as if the range is ingored. When I debug I inspect the printDocument object and confirm that the range values are correct but the end product that the printer produces does not much the values I gave it.
Here is my code :
printDialog.Document = printdoc;
printDialog.AllowSomePages = true;
if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
printdoc.PrinterSettings.FromPage = printDialog.PrinterSettings.FromPage;
printdoc.PrinterSettings.ToPage = printDialog.PrinterSettings.ToPage;
printdoc.PrinterSettings.PrintRange = printDialog.PrinterSettings.PrintRange;
printPreviewDialog.Document = printdoc;
printPreviewDialog.FindForm().WindowState = FormWindowState.Maximized;
printPreviewDialog.ShowDialog();
}
*Note - printdoc is a instance of System.Drawing.Printing.PrintDocument. I added code in the PrintDocument's PrintPage event handler to populate the page I'm printing.
You need to tell the print dialog that it should accept user input for page ranges. To do this, you can specify the PrinterSettings.PrintRange
.
var printDialog = new PrintDialog();
printDialog.AllowSomePages = true; //May not be needed
printDialog.PrinterSettings.PrintRange = PrintRange.SomePages; //Needed
if(printDialog.ShowDialog() == DialogResult.OK)
{
// ... do the rest here
}
Notes: The main takeaway you should get is that you need to set PrintDialog.AllowSomePages = true
(along with From/ToPage) in order to tell the dialog to only print those ranges. Also, I am not sure if setting the AllowSomePages
after the dialog closes will take effect, so that's why I put the code before ShowDialog
. You can try to set it inside the if-statement at your convenience.