2008-12-31 79 views
1

我正在編寫.Net 2008 Winforms中的c#代碼。使用打印預覽.Net Winforms

我創建了一個打印預覽窗口來創建報告。它工作正常,我可以預覽報告,然後打印它。唯一的問題是它不像Office打印預覽那樣靈活。用戶不能選擇默認打印機以外的打印機,也不能將打印限制在特定頁面。也許我錯過了我需要的一些特性。

這裏是我使用的代碼的一部分:

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(); 

謝謝,

鮑勃

+0

在代碼中沒有任何地方可以稱之爲pd.Print()。這發生在哪裏? – 2009-01-01 00:25:34

回答

5

打印預覽和打印是不同的函數,並應是不同的菜單選項。選擇「打印預覽」不應該打印文檔,用戶可能希望看到他們的文檔看起來像是在頁面上放置而不實際打印出來。

要打印頁面,並允許您選擇打印機設備,使用方法:

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

PrintDialog類有一個UseEXDialog屬性,你可以用它來顯示與打印選擇擴展頁面設置對話框,範圍,N-up打印等。人。處理所有這些選項是很多工作,首先得到PrintDialog

0

謝謝

public OpenFileDialog dlg;私人PrintDocument printDocument =新的PrintDocument(); private void FileUpload_Click(object sender,EventArgs e) dlg = new OpenFileDialog(); dlg.Filter = 「doc文件(.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"); 
     } 
    } 
+0

請嘗試解釋您的解決方案如何符合OP需求。僅僅發佈沒有解釋的代碼並不是一個答案。 – Artemix 2013-06-26 10:25:53