关于打印预览的问题!在线等!
private void button1_Click(object sender, EventArgs e)
{
this..PrinterSettings.PrintFileName = @ "d:\a.txt ";
this.printDocument1.DocumentName = "a.txt ";
this.printPreviewDialog1.Document = this.printDocument1;
printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
this.printPreviewDialog1.ShowDialog();
}
这是我在一个点击按钮中写的代码,当我点击时候,我就想预览文档,但是每次点击后只有一个预览框出来,没有预览的内容.想请教下,为什么啊?是我的printDocument1没设置好吗?
------解决方案-------------------- //在用户按下页面预览按钮时激发的事件
private void printPreviewButton_Click(object sender, EventArgs e) {
try {
StreamReader streamToPrint = new StreamReader ( "PrintMe.Txt ");
try {
TextFilePrintDocument pd = new TextFilePrintDocument(streamToPrint); //假定为默认打印机
if (storedPageSettings != null) {
pd.DefaultPageSettings = storedPageSettings ;
}
PrintPreviewDialog dlg = new PrintPreviewDialog() ;
dlg.Document = pd;
dlg.ShowDialog();
} finally {
streamToPrint.Close() ;
}
} catch(Exception ex) {
MessageBox.Show( "试图预览要打印的文件时发生错误 - " + ex.Message);
}
}
//------------------------------------------
namespace Microsoft.Samples.WinForms.Cs.PrintingExample5 {
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
// <doc>
// <desc>
// TextFilePrintDocument 将流输出到打印机
//
// 注意:为了避免在关闭文件时出问题,
// 如果发生异常,此类仅使用一个流
// 并将它留给调用方,以打开该文件
// 进行打印
//
// </desc>
// </doc>
public class TextFilePrintDocument : PrintDocument {
private Font printFont = null ;
private StreamReader streamToPrint = null ;
public TextFilePrintDocument(StreamReader streamToPrint) : base () {
this.streamToPrint = streamToPrint ;
}
//重写 OnBeginPrint 以设置将要使用的字体
protected override void OnBeginPrint(PrintEventArgs ev) {
base.OnBeginPrint(ev) ;
printFont = new Font( "Arial ", 10);
}
//重写 OnPrintPage 以为文档提供打印逻辑
protected override void OnPrintPage(PrintPageEventArgs ev) {
base.OnPrintPage(ev) ;
float lpp = 0 ;
float yPos = 0 ;
int count = 0 ;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;