文本文件打印类库(C#)
2013-08-27 17:34
381 查看
我写了一个打印文本文件的类库,功能包括:打印预览、打印。打印时可以选择打印机,可以指定页码范围。调用方法非常简单:
TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);
p.View(); // 打印预览
p.Print(); // 打印文件
使用 TextFilePrinter 类的以下构造函数可以指定打印时使用的字体:
TextFilePrinter(string fileName, Encoding theEncode, Font theFont)
下面测试程序运行时的截图:
View Code
这些程序都相当简当明了,这里就不再解释了。
这个类库有个缺点:当文本文件中的一行不能在打印纸的一行中打印完时,该行的后半部就丢失了。
TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);
p.View(); // 打印预览
p.Print(); // 打印文件
使用 TextFilePrinter 类的以下构造函数可以指定打印时使用的字体:
TextFilePrinter(string fileName, Encoding theEncode, Font theFont)
下面测试程序运行时的截图:
using System; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; using System.IO; using System.Text; namespace Skyiv.Util { sealed class TextFilePrinter { string fileName; Encoding theEncode; Font theFont; StreamReader srToPrint; int currPage; public TextFilePrinter(string fileName) : this(fileName, Encoding.GetEncoding("GB18030"), new Font("新宋体", 10)) { } public TextFilePrinter(string fileName, Encoding theEncode, Font theFont) { this.fileName = fileName; this.theEncode = theEncode; this.theFont = theFont; } public void Print() { using (srToPrint = new StreamReader(fileName, theEncode)) { PrintDialog dlg = new PrintDialog(); dlg.Document = GetPrintDocument(); dlg.AllowSomePages = true; dlg.AllowPrintToFile = false; if (dlg.ShowDialog() == DialogResult.OK) dlg.Document.Print(); } } public void View() { using (srToPrint = new StreamReader(fileName, theEncode)) { PrintPreviewDialog dlg = new PrintPreviewDialog(); dlg.Document = GetPrintDocument(); dlg.ShowDialog(); } } PrintDocument GetPrintDocument() { currPage = 1; PrintDocument doc = new PrintDocument(); doc.DocumentName = fileName; doc.PrintPage += new PrintPageEventHandler(PrintPageEvent); return doc; } void PrintPageEvent(object sender, PrintPageEventArgs ev) { string line = null; float linesPerPage = ev.MarginBounds.Height / theFont.GetHeight(ev.Graphics); bool isSomePages = ev.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages; if (isSomePages) { while (currPage < ev.PageSettings.PrinterSettings.FromPage) { for (int count = 0; count < linesPerPage; count++) { line = srToPrint.ReadLine(); if (line == null) break; } if (line == null) return; currPage++; } if (currPage > ev.PageSettings.PrinterSettings.ToPage) return; } for (int count = 0; count < linesPerPage; count++) { line = srToPrint.ReadLine(); if (line == null) break; ev.Graphics.DrawString(line, theFont, Brushes.Black, ev.MarginBounds.Left, ev.MarginBounds.Top + (count * theFont.GetHeight(ev.Graphics)), new StringFormat()); } currPage++; if (isSomePages && currPage > ev.PageSettings.PrinterSettings.ToPage) return; if (line != null) ev.HasMorePages = true; } } }
View Code
这些程序都相当简当明了,这里就不再解释了。
这个类库有个缺点:当文本文件中的一行不能在打印纸的一行中打印完时,该行的后半部就丢失了。
相关文章推荐
- 文本文件打印类库(C#)
- 文本文件打印类库(C#)
- C#打印文本文件实例详解
- C#文本文件打印示例
- C#处理文本文件(转 by:nowave)《以下的打印和打印预览比较的有价值》
- C#编写Windows桌面应用程序打印文本文件
- 文本文件打印类库(C#)
- C#文本文件打印示例3
- c#打印文本文件简单实例
- 文本文件打印类库(C#)
- C#实现打印与打印预览功能的思路及代码
- 读博日记(C#常用开源类库收集
- 如何在C#中实现DATAGRIDVIEW 数据打印
- C#使用结构体,输入5个人的学号,姓名,分数,按照成绩高低排列打印出来
- C#调用word打印
- C#:读写简单的文本文件
- 解决本地文本文件上传到服务器乱码问题(C#),ANSI转UTF-8编码
- 关于C#的windons窗体的打印功能知识点解析和功能实例
- 利用C#的反射机制动态调用DLL类库
- 六一儿童节的礼物:基于GB18030-2000汉字拼音类库(C#)