在.Net Winforms中使用打印预览

我正在.Net 2008 Winforms中编写c#代码。

我创建了一个打印预览窗口来创建报告。 它工作正常我可以预览报告然后打印它。 唯一的问题是它不像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(); 

谢谢,

短发

打印预览和打印是不同的function,应该是不同的菜单选项。 选择“打印预览”不应该打印您的文档,用户可能希望看到他们的文档在页面上显示的内容而不实际打印它。

要打印页面并允许选择打印机设备,请使用:

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

PrintDialog类具有UseEXDialog属性,可用于显示带有打印选择,范围,n-up打印等的扩展页面设置对话框。 人。 处理所有这些选项需要做很多工作,让PrintDialog先行。

谢谢

public OpenFileDialog dlg; private PrintDocument printDocument = new 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"); } }