views:

1692

answers:

3

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

+2  A: 

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.

Dour High Arch
A: 

Nowhere in that code do you ever call pd.Print(). Where does that happen?

Joel Coehoorn
A: 

I don't actually want to print in code. The user will decide to print (or not) after previewing the document.

Bob

Bob Avallone
You should edit the question to add more information, not add an answer.
Dour High Arch
To print in a Winforms app you have to add code, the code I posted will do that and allow user to choose a printer. Did you try it? What happened?
Dour High Arch