Use of Print Preview in .Net Winforms

余生长醉 提交于 2019-12-22 00:46:26

问题


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


回答1:


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.




回答2:


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");
        }
    } 


来源:https://stackoverflow.com/questions/404010/use-of-print-preview-in-net-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!