自定义打印组件打印的功能正常,预览正常,在预览窗体点击打印机图标,打印出来是空白页,没有数据。代码如下:请大家帮我看看,多谢!usingSystem;usingSystem.Compone
自定义打印组件打印的功能正常,预览正常,在预览窗体点击打印机图标,打印出来是空白页,没有数据。代码如下:请大家帮我看看,多谢!
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
namespace pt
{
///
/// Form1 的摘要说明。
///
public class PrintForm : System.Windows.Forms.Form
{
protected internal System.Windows.Forms.Button printButton;
protected internal System.Windows.Forms.Button printPreviewButton;
private PageSettings storedPageSettings = null ;
public PrintForm()
{
InitializeComponent();
printButton.Click += new System.EventHandler(printButton_Click);
//pageSetupButton.Click += new System.EventHandler(pageSetupButton_Click);
printPreviewButton.Click += new System.EventHandler(printPreviewButton_Click);
}
//在用户按下页面设置按钮时激发的事件
private void pageSetupButton_Click(object sender, EventArgs e)
{
try
{
PageSetupDialog psDlg = new PageSetupDialog() ;
if (storedPageSettings == null)
{
storedPageSettings = new PageSettings();
}
psDlg.PageSettings = storedPageSettings ;
psDlg.ShowDialog();
}
catch(Exception ex)
{
MessageBox.Show("发生错误 - " + ex.Message);
}
}
//在用户按下打印按钮时激发的事件
private void printButton_Click(object sender, EventArgs e)
{
try
{
StreamReader streamToPrint = new StreamReader ("d:\\aaa.txt");
try
{
TextFilePrintDocument pd = new TextFilePrintDocument(streamToPrint); //假定为默认打印机
if (storedPageSettings != null)
{
pd.DefaultPageSettings = storedPageSettings ;
}
PrintDialog dlg = new PrintDialog() ;
dlg.Document = pd;
DialogResult result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
pd.Print();
}
}
finally
{
streamToPrint.Close() ;
}
}
catch(Exception ex)
{
MessageBox.Show("打印文件时发生错误 - " + ex.Message);
}
}
//在用户按下页面预览按钮时激发的事件
private void printPreviewButton_Click(object sender, EventArgs e)
{
try
{
StreamReader streamToPrint = new StreamReader ("d:\\aaa.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);
}
}
private void InitializeComponent()
{
this.printPreviewButton = new System.Windows.Forms.Button();
this.printButton = new System.Windows.Forms.Button();
this.SuspendLayout();
this.printPreviewButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.printPreviewButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.printPreviewButton.Location = new System.Drawing.Point(38, 226);
this.printPreviewButton.Name = "printPreviewButton";
this.printPreviewButton.Size = new System.Drawing.Size(164, 43);
this.printPreviewButton.TabIndex = 0;
this.printPreviewButton.Text = "打印预览";
this.printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.printButton.Location = new System.Drawing.Point(38, 121);
this.printButton.Name = "printButton";
this.printButton.Size = new System.Drawing.Size(164, 43);
this.printButton.TabIndex = 0;
this.printButton.Text = "打印文件";
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(604, 410);
this.Controls.Add(this.printButton);
this.Controls.Add(this.printPreviewButton);
this.Name = "PrintForm";
this.Load += new System.EventHandler(this.PrintForm_Load);
this.ResumeLayout(false);
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new PrintForm());
}
private void PrintForm_Load(object sender, System.EventArgs e)
{
}
}
public class TextFilePrintDocument : PrintDocument
{
private Font printFOnt=null;
private StreamReader streamToPrint = null ;
public TextFilePrintDocument(StreamReader streamToPrint) : base ()
{
this.streamToPrint = streamToPrint ;
}
protected override void OnBeginPrint(PrintEventArgs ev)
{
base.OnBeginPrint(ev) ;
printFont = new Font("宋体", 10);
}
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;
String line=null;
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) ;
while (count < lpp && ((line=streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString (line, printFont, Brushes.Black, leftMargin,
yPos, new StringFormat());
count++;
}
if (line != null)
ev.HasMorePages = true ;
else
ev.HasMorePages = false ;
}
}
}
5 个解决方案
如果不预览,直接打印,结果会正确。如果一定要预览再打印,需在OnPrintPage中设置初始化代码(当第一页时,已显示记录数=0,页面的一些设置还原,当打印到最后一页时,即HasMorePages=false,当前页初始化为1)。假如有两页内容,点击预览后,2次进入OnPrintPage,页数已到2,记录数已到最后,再点击打印,又会进入OnPrintPage,而你没有初始化的话,已显示记录数已到最后,不会再有数据显示,而页数加一的话将会是第3页,所以是空白页。
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
while(true)
{
//PageInitialize() 以下2行
LeavePageHeight=AvailablePageHeight;
Current_Y=_PageSetup.Margin_Top ;
if (Page==0)
{
//ReportInitialize(); 以下5行,你要注意和修改的重点
Offset_X=_PageSetup.Margin_Left ;
RecordNumber=_Content.Count ;
Page=0;
CurrentRecord=0;
AvailablePageHeight=_PageSetup .Paper_Height-_PageSetup.Margin_Top -_PageSetup.Margin_Bottom ;
Page++;
#region Title
if (_Title ._RowHeight>0)
{
PrintTitle(e);
LeavePageHeight-=_Title ._RowHeight ;
Current_Y+=_Title ._RowHeight ;
}
#endregion
#region ReportHeader
if (LeavePageHeight>=_ReportHeader ._RowHeight )
{
if (_ReportHeader._RowHeight>0)
{
PrintReportHeader(e);
LeavePageHeight-=_ReportHeader._RowHeight ;
Current_Y+=_ReportHeader._RowHeight ;
}
}
#endregion
#region PageHeader and Frame
if (LeavePageHeight>=this._LineFrame ._RowHeight )
{
if (_PageHeader._RowHeight >0)
{
PrintPageHeaderText(e);
PrintPageFrame(e);
LeavePageHeight-=_LineFrame ._RowHeight ;
Current_Y+=_LineFrame ._RowHeight ;
}
}
#endregion
#region Detail and Frame
while (LeavePageHeight>=this._LineFrame ._RowHeight && CurrentRecord {
PrintPageDetail(e);
CurrentRecord++;
PrintPageFrame(e);
LeavePageHeight-=_LineFrame ._RowHeight ;
Current_Y+=_LineFrame ._RowHeight ;
}
#endregion
}
else
{
Page++;
if (LeavePageHeight>=this._LineFrame ._RowHeight )
{
if (_PageHeader._RowHeight >0)
{
PrintPageHeaderText(e);
PrintPageFrame(e);
LeavePageHeight-=_LineFrame ._RowHeight ;
Current_Y+=_LineFrame ._RowHeight ;
}
}
while (LeavePageHeight>=this._LineFrame ._RowHeight && CurrentRecord {
PrintPageDetail(e);
CurrentRecord++;
PrintPageFrame(e);
LeavePageHeight-=_LineFrame ._RowHeight ;
Current_Y+=_LineFrame ._RowHeight ;
}
}
if (CurrentRecord {
e.HasMorePages =true;
}
else
{
if (_ReportTailer._RowHeight >0)
{
PrintReportTailer(e);
}
e.HasMorePages =false;
Page=0;//你要注意和修改的重点
break;
}
if (Page>=FromPage && Page<=ToPage)
{
if (Page==ToPage) e.HasMorePages =false;
break;
}
}
}