文本文件打印类库(C#)

我写了一个打印文本文件的类库,功能包括:打印预览、打印。打印时可以选择打印机,可以指定页码范围。调用方法非常简单:
TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);
p.View();  // 打印预览
p.Print(); // 打印文件
使用 TextFilePrinter 类的以下构造函数可以指定打印时使用的字体:
TextFilePrinter(string fileName, Encoding theEncode, Font theFont)
下面测试程序运行时的截图:
TextFilePrinter-01.PNG
点击“预览”按钮后:
TextFilePrinter-02.PNG
点击“打印”按钮后:
TextFilePrinter-03.PNG
这幅图中的打印机:“Microsoft Office Doument Image Writer”是 Microsoft Office 2003 软件提供一个虚拟打印机,用来调试打印程序非常方便(使用“打印预览”也可以调试打印程序,但“打印预览”只能使用默认的打印机和默认的打印属性,也不能设置页码范围),可以设置打印属性和页码范围以及打印份数。使用它来调试打印程序,可以节省不少打印纸。为建设节约型社会作贡献 :) 
TextFilePrinter-04.PNG
这幅图就是该虚拟打印机在屏幕上的显示的结果。
这里是测试程序的源代码:

None.gif //  PrintFile.cs - 文件打印程序
None.gif
//  编译方法: csc /t:winexe PrintFile.cs TextFilePrinter.cs
None.gif

None.gif
using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Windows.Forms;
None.gif
using  Skyiv.Util;
None.gif
None.gif
namespace  Skyiv.Ben.Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
class PrintFileForm : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    TextBox tbxFileName;
InBlock.gif    
InBlock.gif    
public PrintFileForm()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      SuspendLayout();
InBlock.gif      
InBlock.gif      Button btnFileName 
= new Button();
InBlock.gif      btnFileName.Text 
= "文件名";
InBlock.gif      btnFileName.Location 
= new Point(1010);
InBlock.gif      btnFileName.Size 
= new Size(6024);
InBlock.gif      btnFileName.Click 
+= new EventHandler(BtnFileName_Click);
InBlock.gif
InBlock.gif      Button btnPrint 
= new Button();
InBlock.gif      btnPrint.Text 
= "打印";
InBlock.gif      btnPrint.Location 
= new Point(7510);
InBlock.gif      btnPrint.Size 
= new Size(6024);
InBlock.gif      btnPrint.Click 
+= new EventHandler(BtnPrint_Click);
InBlock.gif
InBlock.gif      Button btnView 
= new Button();
InBlock.gif      btnView.Text 
= "预览";
InBlock.gif      btnView.Location 
= new Point(14010);
InBlock.gif      btnView.Size 
= new Size(6024);
InBlock.gif      btnView.Click 
+= new EventHandler(BtnView_Click);
InBlock.gif
InBlock.gif      tbxFileName 
= new TextBox();
InBlock.gif      tbxFileName.Text 
= "PrintFile.cs";
InBlock.gif      tbxFileName.Location 
= new Point(1044);
InBlock.gif      tbxFileName.Size 
= new Size(19020);
InBlock.gif      tbxFileName.ReadOnly 
= true;
InBlock.gif      tbxFileName.Anchor 
= (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      Controls.AddRange(
new Control[]dot.gif{btnFileName, btnPrint, btnView, tbxFileName});
InBlock.gif      Text 
= "文本文件打印程序";
InBlock.gif      ClientSize 
= new Size(21080);
InBlock.gif
InBlock.gif      ResumeLayout(
false);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
void BtnFileName_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      OpenFileDialog dlg 
= new OpenFileDialog();
InBlock.gif      
if(dlg.ShowDialog() != DialogResult.OK) return;
InBlock.gif      tbxFileName.Text 
= dlg.FileName;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
void BtnPrint_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      TextFilePrinter p 
= new TextFilePrinter(tbxFileName.Text);
InBlock.gif      p.Print();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
void BtnView_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      TextFilePrinter p 
= new TextFilePrinter(tbxFileName.Text);
InBlock.gif      p.View();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Application.Run(
new PrintFileForm());
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif

这里是该类的源代码:

None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Drawing.Printing;
None.gif
using  System.Windows.Forms;
None.gif
using  System.IO;
None.gif
using  System.Text;
None.gif
None.gif
namespace  Skyiv.Util
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
sealed class TextFilePrinter
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
string fileName;
InBlock.gif    Encoding theEncode;
InBlock.gif    Font theFont;
InBlock.gif    StreamReader srToPrint;
InBlock.gif    
int currPage;
InBlock.gif
InBlock.gif    
public TextFilePrinter(string fileName)
InBlock.gif      : 
this(fileName, Encoding.GetEncoding("GB18030"), new Font("新宋体"10))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public TextFilePrinter(string fileName, Encoding theEncode, Font theFont)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
this.fileName = fileName;
InBlock.gif      
this.theEncode = theEncode;
InBlock.gif      
this.theFont = theFont;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void Print()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
using (srToPrint = new StreamReader(fileName, theEncode))
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        PrintDialog dlg 
= new PrintDialog();
InBlock.gif        dlg.Document 
= GetPrintDocument();
InBlock.gif        dlg.AllowSomePages 
= true;
InBlock.gif        dlg.AllowPrintToFile 
= false;
InBlock.gif        
if (dlg.ShowDialog() == DialogResult.OK) dlg.Document.Print();
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void View()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
using (srToPrint = new StreamReader(fileName, theEncode))
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        PrintPreviewDialog dlg 
= new PrintPreviewDialog();
InBlock.gif        dlg.Document 
= GetPrintDocument();
InBlock.gif        dlg.ShowDialog();
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    PrintDocument GetPrintDocument()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      currPage 
= 1;
InBlock.gif      PrintDocument doc 
= new PrintDocument();
InBlock.gif      doc.DocumentName 
= fileName;
InBlock.gif      doc.PrintPage 
+= new PrintPageEventHandler(PrintPageEvent);
InBlock.gif      
return doc;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
void PrintPageEvent(object sender, PrintPageEventArgs ev)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
string line = null;
InBlock.gif      
float linesPerPage = ev.MarginBounds.Height / theFont.GetHeight(ev.Graphics);
InBlock.gif      
bool isSomePages = ev.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages;
InBlock.gif      
if (isSomePages)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
while (currPage < ev.PageSettings.PrinterSettings.FromPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          
for (int count = 0; count < linesPerPage; count++)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif            line 
= srToPrint.ReadLine();
InBlock.gif            
if (line == nullbreak;
ExpandedSubBlockEnd.gif          }

InBlock.gif          
if (line == nullreturn;
InBlock.gif          currPage
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if (currPage > ev.PageSettings.PrinterSettings.ToPage) return;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
for (int count = 0; count < linesPerPage; count++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        line 
= srToPrint.ReadLine();
InBlock.gif        
if (line == nullbreak;
InBlock.gif        ev.Graphics.DrawString(line, theFont, Brushes.Black, ev.MarginBounds.Left,
InBlock.gif          ev.MarginBounds.Top 
+ (count * theFont.GetHeight(ev.Graphics)), new StringFormat());
ExpandedSubBlockEnd.gif      }

InBlock.gif      currPage
++;
InBlock.gif      
if (isSomePages && currPage > ev.PageSettings.PrinterSettings.ToPage) return;
InBlock.gif      
if (line != null) ev.HasMorePages = true;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif

这些程序都相当简当明了,这里就不再解释了。
这个类库有个缺点:当文本文件中的一行不能在打印纸的一行中打印完时,该行的后半部就丢失了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值