Bob Avallone
Bob Avallone

Reputation: 379

Use of Print Preview in .Net Winforms

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

Upvotes: 1

Views: 11029

Answers (2)

baskaran
baskaran

Reputation: 1

thanks

public OpenFileDialog dlg ; private PrintDocument printDocument = new PrintDocument(); private void FileUpload_Click(object sender, EventArgs e) { dlg = new OpenFileDialog(); dlg.Filter = "Doc File (.doc,.docx)|.pdf;*.xls,.xlsx,.txt";

        if (dlg.ShowDialog() == DialogResult.OK)
           txtFilename.Text = dlg.FileName;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        label2.Text = txtFilename.Text;
        string sFileName = "";
        long nLength = 0;
        byte[] barFile = null;
        if (dlg.FileName != "")
        {
            System.IO.FileStream fs = new System.IO.FileStream(dlg.FileName, System.IO.FileMode.Open);
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(dlg.FileName);
            sFileName = fileInfo.Name;
            nLength = fs.Length;
            barFile = new byte[fs.Length];
            fs.Read(barFile, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            PrintDialog pDialog = new PrintDialog();
            pDialog.Document = printDocument;
            if (pDialog.ShowDialog() == DialogResult.OK)
            {
                printDocument.DocumentName = dlg.FileName;
                printDocument.Print();
            }
        }
        else
        {
            MessageBox.Show("Please Select the File For File Upload");
        }
    } 

Upvotes: 0

Dour High Arch
Dour High Arch

Reputation: 21722

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.

Upvotes: 5

Related Questions