Christian Seiler
Christian Seiler

Reputation: 1140

How to present print settings from print preview?

In my C# Project I have 2 print functions. One that prints the document directly and another that presents a preview to the user and prints if the user chooses to do so.

While both methods are working, the direkt print version presents the print settings window before it prints the document.

private void printButton_Click(object sender, EventArgs e)    
{
    PrintDialog printDialog = new PrintDialog();
    printDialog.Document = printIssues;
    printDialog.UseEXDialog = true;

    if (DialogResult.OK == printDialog.ShowDialog())
    {
        printIssues.DocumentName = "Some Name";
        printIssues.DefaultPageSettings.Landscape = true;
        printIssues.Print();
    }
}

private void previewButton_Click(object sender, EventArgs e)
{
    PrintPreviewDialog printPreview = new PrintPreviewDialog();
    printPreview.Icon = Properties.Resources.favicon;
    printPreview.Document = printIssues;
    printIssues.DefaultPageSettings.Landscape = true;
    ((Form)printPreview).WindowState = FormWindowState.Maximized;

    printPreview.ShowDialog();
}

The second Version where I present the preview first, once I click the print button the document gets printed on the Default Printer without presenting the Settings window. I've tried some things and searched quiet some time but couldn't find anything that helped me.

I appreciate your help.

Upvotes: 0

Views: 2424

Answers (1)

Gwen Royakkers
Gwen Royakkers

Reputation: 441

I'm afraid this is a known limitation of the PrintPreviewDialog. It needs to know the printer to draw the layout so it uses the default printer.

I've had the same problem in the past, and I believe it can be solved by showing a PrintDialog before showing the PrintPreviewDialog.

private void previewButton_Click(object sender, EventArgs e)
{
    PrintDialog printDialog = new PrintDialog();
    if (DialogResult.OK == printDialog.ShowDialog())
        {
             PrintPreviewDialog printPreview = new PrintPreviewDialog();
             printPreview.Document = printIssues;

             // this is were you take the printersettings from the printDialog
             printPreview.Document.PrinterSettings = printDialog.PrinterSettings;

             printIssues.DefaultPageSettings.Landscape = true;
             printPreview.ShowDialog();         
        }  
}

Another workaround would be to make your own PrintPreviewDialog. But it requires more coding.

can you tell me if the above code works for you?

Upvotes: 1

Related Questions