2

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.

Marnus Steyn
  • 1,053
  • 2
  • 16
  • 44
  • What is `printdoc`? Where do you call your `printDialog.Print___` function? You're leaving out some possibly significant details. – Kcvin Apr 02 '15 at 18:15
  • It 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. Sorry i should have included this in my OP. – Marnus Steyn Apr 04 '15 at 16:47

1 Answers1

5

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.

Kcvin
  • 5,073
  • 2
  • 32
  • 54
  • Thank you. This seemed to solve the problem. Would have up-voted this answer but It seems that I cannot do so at this time. – Marnus Steyn Apr 04 '15 at 16:40