急:如何实现webform的打印(不用水晶报表)?为何组件里没有PrintDocument组件?

shevajmc 2004-08-04 11:02:39
我只按一个button,就可以实现整个页面的多次打印,最好有源程序。
...全文
262 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
greystar 2004-08-26
  • 打赏
  • 举报
回复
//------------------------------------------------------------------------------
/// <copyright from='1997' to='2001' company='Microsoft Corporation'>
/// 版权所有 (c) Microsoft Corporation。保留所有权利。
///
/// 此源代码仅作为 Microsoft 开发工具和/或联机文档
/// 的补充。有关 Microsoft 代码示例的详细信息,请
/// 参阅这些其他资料。
///
/// </copyright>
//------------------------------------------------------------------------------
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;

public class PrintForm : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components=null;
protected internal System.Windows.Forms.Button printButton;
protected internal System.Windows.Forms.Button pageSetupButton;
protected internal System.Windows.Forms.Button printPreviewButton;

private PageSettings storedPageSettings = null ;

public PrintForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

//在用户按下页面设置按钮时激发的事件
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 ("PrintMe.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 ("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);
}

}



/// <summary>
/// 设计器支持所必需的方法,不要使用
/// 代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.printPreviewButton = new System.Windows.Forms.Button();
this.pageSetupButton = new System.Windows.Forms.Button();
this.printButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// printPreviewButton
//
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.printPreviewButton.Click+=new EventHandler(printPreviewButton_Click);
//
// pageSetupButton
//
this.pageSetupButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.pageSetupButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.pageSetupButton.Location = new System.Drawing.Point(38, 172);
this.pageSetupButton.Name = "pageSetupButton";
this.pageSetupButton.Size = new System.Drawing.Size(164, 43);
this.pageSetupButton.TabIndex = 0;
this.pageSetupButton.Text = "页面设置";
this.pageSetupButton.Click +=new EventHandler(pageSetupButton_Click);
//
// printButton
//
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.printButton.Click+=new EventHandler(printButton_Click);
//
// PrintForm
//
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.pageSetupButton);
this.Controls.Add(this.printPreviewButton);
this.Name = "PrintForm";
this.Text = "打印示例 5";
this.Load += new System.EventHandler(this.PrintForm_Load);
this.ResumeLayout(false);

}

/// <summary>
/// 应用程序的主要入口点。
/// </summary>
[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 ;
}

//Override OnBeginPrint to set up the font we are going to use
protected override void OnBeginPrint(PrintEventArgs ev)
{
base.OnBeginPrint(ev) ;
printFont = new Font("Arial", 10);
}

//Override the OnPrintPage to provide the printing logic for the document
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;

//Work out the number of lines per page
//Use the MarginBounds on the event to do this
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) ;

//Now iterate over the file printing out each line
//NOTE WELL: This assumes that a single line is not wider than the page width
//Check count first so that we don't read line that we won't print
while (count < lpp && ((line=streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));

//Print Preview control will not work.
ev.Graphics.DrawString (line, printFont, Brushes.Black, leftMargin,
yPos, new StringFormat());

count++;
}

//If we have more lines then print another page
if (line != null)
ev.HasMorePages = true ;
else
ev.HasMorePages = false ;
}

}

}





长江支流 2004-08-26
  • 打赏
  • 举报
回复
java:print()

唉,没人回答
再给你一个打印程序源码
www.alinksoft.com

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧