前几日,偶然看到网上的一个关于文本文件打印的帖子,解决了偶的大问题....真该谢谢那位发帖子的老兄!![](/Editor/FCKeditor/editor/images/smiley/msn/thumbs_up.gif)
今天在把这段代码贴出来,以便记忆
using
System;
using
System.IO;
using
System.Data;
using
System.Drawing;
using
System.Drawing.Printing;
using
System.Collections ;
using
System.Windows.Forms;
using
System.Text;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
LeisureTown.COMMON.BASE
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// CommonPrintDocument 的摘要说明。
/// </summary>
public class CommonPrintDocument
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
public const float comLeftMar = 5;
public const float comTopMar = 5;
public const string comFontStyle = "方正姚体";
public const float comEmSize = 9;
public CommonPrintDocument()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// ============================================================================================================
/// 类名称 : TextFilePrintDocument
/// <summary>
/// 文本文件的通用打印纸打印类
/// </summary>
/// <remarks>
/// 文本文件的通用打印纸打印类(A4纸),将流输出到打印机。
/// 注意:为了避免在关闭文件时出问题,如果发生异常,此类仅使用一个流并将它留给调用方,以打开该文件进行打印。
/// </remarks>
/// <history>
/// ============================================================================================================
/// 开发日期 开发者
/// 2007-06-22 赵文哲
/// </history>
/// ============================================================================================================
public class TextFilePrintDocument : PrintDocument
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
private Font printFont = null;
private StreamReader streamToPrint = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public TextFilePrintDocument(StreamReader streamToPrint) : base ()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.streamToPrint = streamToPrint;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//重写OnBeginPrint,设置默认字体为"Arial",大小为9(小五)。
protected override void OnBeginPrint(PrintEventArgs ev)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
base.OnBeginPrint(ev);
printFont = new Font("Arial", 9);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//重写OnPrintPage 以为文档提供打印逻辑
protected override void OnPrintPage(PrintPageEventArgs ev)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
base.OnPrintPage(ev);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
float lpp = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
String line = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//算出每页的行数
//在事件上使用 MarginBounds 以达到此目的
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//现在,在文件上重复此操作以输出每行
//注意:假设单行比页宽窄
//首先检查行数,以便看不到不打印的行
while (count < lpp && ((line = streamToPrint.ReadLine()) != null))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
count++;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//如果有多行,则另外打印一页
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// ============================================================================================================
/// 类名称 : SmallListFilePrintDocument
/// <summary>
/// 文本文件的小票打印纸打印类
/// </summary>
/// <remarks>
/// 文本文件的小票打印纸打印类(76x297mm),将流输出到打印机。
/// 注意:为了避免在关闭文件时出问题,如果发生异常,此类仅使用一个流并将它留给调用方,以打开该文件进行打印。
/// </remarks>
/// <history>
/// ============================================================================================================
/// 开发日期 开发者
/// 2007-06-22 赵文哲
/// </history>
/// ============================================================================================================
public class SmallListFilePrintDocument : PrintDocument
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
private Font printFont = null;
private StreamReader streamToPrint = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public SmallListFilePrintDocument(StreamReader streamToPrint) : base ()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.streamToPrint = streamToPrint;
}
//重写OnBeginPrint,设置默认字体为"Arial",大小为9(小五)。
protected override void OnBeginPrint(PrintEventArgs ev)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
base.OnBeginPrint(ev);
printFont = new Font(CommonPrintDocument.comFontStyle , CommonPrintDocument.comEmSize );
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//重写OnPrintPage 以为文档提供打印逻辑
protected override void OnPrintPage(PrintPageEventArgs ev)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
base.OnPrintPage(ev);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
float yPos = 0;
int count = 0;
float leftMargin = CommonPrintDocument.comLeftMar;
float topMargin = CommonPrintDocument.comTopMar;
String line = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//现在,在文件上重复此操作以输出每行
while (((line = streamToPrint.ReadLine()) != null))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//如果有多行,则另外打印一页
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}