关于文本文件打印的小篇

前几日,偶然看到网上的一个关于文本文件打印的帖子,解决了偶的大问题....真该谢谢那位发帖子的老兄!

今天在把这段代码贴出来,以便记忆 

 

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;

namespace  LeisureTown.COMMON.BASE
{
    
/// <summary>
    
/// CommonPrintDocument 的摘要说明。
    
/// </summary>

    public class CommonPrintDocument
    
{
        
public const float comLeftMar = 5;
        
public const float comTopMar = 5;
        
public const string comFontStyle = "方正姚体";
        
public const float comEmSize = 9;
        
public CommonPrintDocument()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

    }


    
    
    
/// ============================================================================================================
    
/// 类名称 : TextFilePrintDocument
    
/// <summary>
    
/// 文本文件的通用打印纸打印类
    
/// </summary>
    
/// <remarks>
    
/// 文本文件的通用打印纸打印类(A4纸),将流输出到打印机。
    
/// 注意:为了避免在关闭文件时出问题,如果发生异常,此类仅使用一个流并将它留给调用方,以打开该文件进行打印。
    
/// </remarks>
    
/// <history>
    
/// ============================================================================================================
    
/// 开发日期             开发者
    
/// 2007-06-22           赵文哲
    
/// </history>
    
/// ============================================================================================================

    public class TextFilePrintDocument : PrintDocument
    
{
        
private Font printFont = null;
        
private StreamReader streamToPrint = null;

        
public TextFilePrintDocument(StreamReader streamToPrint) : base ()
        
{
            
this.streamToPrint = streamToPrint;
        }


        
//重写OnBeginPrint,设置默认字体为"Arial",大小为9(小五)。
        protected override void OnBeginPrint(PrintEventArgs ev)
        
{
            
base.OnBeginPrint(ev);
            printFont 
= new Font("Arial"9);
            
        }


        
//重写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;
            String line 
= null;

            
            
//算出每页的行数
            
//在事件上使用 MarginBounds 以达到此目的
            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;
        }


    }


    
/// ============================================================================================================
    
/// 类名称 : SmallListFilePrintDocument
    
/// <summary>
    
/// 文本文件的小票打印纸打印类
    
/// </summary>
    
/// <remarks>
    
/// 文本文件的小票打印纸打印类(76x297mm),将流输出到打印机。
    
/// 注意:为了避免在关闭文件时出问题,如果发生异常,此类仅使用一个流并将它留给调用方,以打开该文件进行打印。
    
/// </remarks>
    
/// <history>
    
/// ============================================================================================================
    
/// 开发日期             开发者
    
/// 2007-06-22           赵文哲
    
/// </history>
    
/// ============================================================================================================

    public class SmallListFilePrintDocument : PrintDocument
    
{
        
private Font printFont = null;
        
private StreamReader streamToPrint = null;

        
public SmallListFilePrintDocument(StreamReader streamToPrint) : base ()
        
{
            
this.streamToPrint = streamToPrint;
        }

        
//重写OnBeginPrint,设置默认字体为"Arial",大小为9(小五)。
        protected override void OnBeginPrint(PrintEventArgs ev)
        
{
            
base.OnBeginPrint(ev);
            printFont 
= new Font(CommonPrintDocument.comFontStyle , CommonPrintDocument.comEmSize );
            
        }


        
//重写OnPrintPage 以为文档提供打印逻辑
        protected override void OnPrintPage(PrintPageEventArgs ev)
        
{

            
base.OnPrintPage(ev);

            
float yPos = 0;
            
int count = 0;
            
float leftMargin = CommonPrintDocument.comLeftMar;
            
float topMargin = CommonPrintDocument.comTopMar;
            String line 
= null;

            
            
//现在,在文件上重复此操作以输出每行
            while (((line = streamToPrint.ReadLine()) != null))
            
{
                yPos 
= topMargin + (count * printFont.GetHeight(ev.Graphics));

                ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, 
new StringFormat());

            }


            
//如果有多行,则另外打印一页
            if (line != null)
                ev.HasMorePages 
= true;
            
else
                ev.HasMorePages 
= false;
        }

    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值