Use of Print Preview in .Net Winforms

10,356

Print Preview and Print are different functions and should be different menu options. Choosing Print Preview should not print your document, it is entirely likely that a user would want to see what their document looks like laid out on a page without actually printing it.

To print a page and allow selecting printer devices, use :

PrintDialog pDialog = new PrintDialog( );
pDialog.Document = printDocument;
if (pDialog.ShowDialog( ) == DialogResult.OK) {
    printDocument.DocumentName = fileName;
    printDocument.Print( );
    }

The PrintDialog class has a UseEXDialog property you can use to show an expanded Page Setup dialog with print selections, ranges, n-up printing, et. al. Handling all these options is a lot of work, get PrintDialog working first.

Share:
10,356
Bob Avallone
Author by

Bob Avallone

Merge Me

Updated on June 04, 2022

Comments

  • Bob Avallone
    Bob Avallone almost 2 years

    I am writing c# code in .Net 2008 Winforms.

    I created a print preview window to create a report. It works fine I can preview the report and then print it. The only problem is it is not as flexible as the Office Print preview. The users cannot chose a printer other that the default printer and they cannot limit the print to certain pages. Perhaps I am missing some prperties I need.

    Here is a portion of the code I use:

    PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(this.PrintTheGraph);
                pd.DefaultPageSettings.Landscape = true;
                // Allocate a print preview dialog object.
                PrintPreviewDialog dlg = new PrintPreviewDialog();
                dlg.Width = 100;
                dlg.MinimumSize = new Size(375, 250);
                dlg.SetBounds(100, -550, 800, 800);
                dlg.Document = pd;
                DialogResult result = dlg.ShowDialog();
    

    Thanks,

    Bob

    • Joel Coehoorn
      Joel Coehoorn over 15 years
      Nowhere in that code do you ever call pd.Print(). Where does that happen?
  • Artemix
    Artemix almost 11 years
    Please try to explain how does your solution matches OP needs. Posting just code without explanation is not an answer.