我写了一个打印文本文件的类库,功能包括:打印预览、打印。打印时可以选择打印机,可以指定页码范围。调用方法非常简单:
TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);
p.View(); // 打印预览
p.Print(); // 打印文件
使用 TextFilePrinter 类的以下构造函数可以指定打印时使用的字体:
TextFilePrinter(string fileName, Encoding theEncode, Font theFont)
下面测试程序运行时的截图:

点击“预览”按钮后:

点击“打印”按钮后:

这幅图中的打印机:“Microsoft Office Doument Image Writer”是 Microsoft Office 2003 软件提供一个虚拟打印机,用来调试打印程序非常方便(使用“打印预览”也可以调试打印程序,但“打印预览”只能使用默认的打印机和默认的打印属性,也不能设置页码范围),可以设置打印属性和页码范围以及打印份数。使用它来调试打印程序,可以节省不少打印纸。为建设节约型社会作贡献 :)

这幅图就是该虚拟打印机在屏幕上的显示的结果。
这里是测试程序的源代码:

// PrintFile.cs - 文件打印程序
// 编译方法: csc /t:winexe PrintFile.cs TextFilePrinter.cs using System;
using System.Drawing;
using System.Windows.Forms;
using Skyiv.Util; namespace Skyiv.Ben.Test
{
class PrintFileForm : Form
{
TextBox tbxFileName; public PrintFileForm()
{
SuspendLayout(); Button btnFileName = new Button();
btnFileName.Text = "文件名";
btnFileName.Location = new Point(, );
btnFileName.Size = new Size(, );
btnFileName.Click += new EventHandler(BtnFileName_Click); Button btnPrint = new Button();
btnPrint.Text = "打印";
btnPrint.Location = new Point(, );
btnPrint.Size = new Size(, );
btnPrint.Click += new EventHandler(BtnPrint_Click); Button btnView = new Button();
btnView.Text = "预览";
btnView.Location = new Point(, );
btnView.Size = new Size(, );
btnView.Click += new EventHandler(BtnView_Click); tbxFileName = new TextBox();
tbxFileName.Text = "PrintFile.cs";
tbxFileName.Location = new Point(, );
tbxFileName.Size = new Size(, );
tbxFileName.ReadOnly = true;
tbxFileName.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right); Controls.AddRange(new Control[]{btnFileName, btnPrint, btnView, tbxFileName});
Text = "文本文件打印程序";
ClientSize = new Size(, ); ResumeLayout(false);
} void BtnFileName_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if(dlg.ShowDialog() != DialogResult.OK) return;
tbxFileName.Text = dlg.FileName;
} void BtnPrint_Click(object sender, EventArgs e)
{
TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);
p.Print();
} void BtnView_Click(object sender, EventArgs e)
{
TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);
p.View();
} static void Main()
{
Application.Run(new PrintFileForm());
}
}
}

这里是该类的源代码:

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("新宋体", ))
{
} 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 = ;
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 = ; 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 = ; 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;
}
}
}

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

C# 文本文件打印类库(C#)的更多相关文章

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

    我写了一个打印文本文件的类库,功能包含:打印预览.打印.打印时能够选择打印机.能够指定页码范围. 调用方法很easy: TextFilePrinter p = new TextFilePrinter( ...

  2. whoosh----索引|搜索文本类库

    先了解基本概念和步骤: Quick Start Whoosh是一个索引文本和搜索文本的类库,他可以为你提供搜索文本的服务,比如如果你在创建一个博客的软件,你可以用whoosh为它添加添加一个搜索功能以 ...

  3. 利用人工智能(Magpie开源库)给一段中文的文本内容进行分类打标签

    当下人工智能是真心的火热呀,各种原来传统的业务也都在尝试用人工智能技术来处理,以此来节省人工成本,提高生产效率.既然有这么火的利器,那么我们就先来简单认识下什么是人工智能吧,人工智能是指利用语音识别. ...

  4. 移除HTML标签(CharacterUtility)类库

    最近时间,专案需要,写了一个类库,主要是为了移除HTML标签以及截取前一段文本的类库. 下载地址:http://download.cnblogs.com/insus/Library/Character ...

  5. 怎样做出通用的pos小票打印程序

    POS小票打印机分为热敏和针式俩种. 打印纸的宽度分为58毫米.76毫米和80毫米三种. 打印接口分为:串口.并口.USB和网口(以太网). 热敏打印机速度较快,打印的时候噪音少,针打可以使用多联纸自 ...

  6. Linux系统文件与目录管理(1)

    文件与目录的操作 在Linux系统的文件与目录的管理上,不外乎『显示属性』.『拷贝』.『删除文件』.『移动文件或目录』.『重命名』等常用操作,由于文件与目录的管理在 Linux当中是很重要的,尤其是每 ...

  7. java-编码解码-流的操作规律

    一 编码解码 字符串:String 字节数组:byte[]字符串--编码(getBytes())-->字节数组字节数组--解码(new String(byte[]))-->字符串 publ ...

  8. Linux 系统常用管理命令(精简笔记)

    Linux是一套免费使用和自由传播的类Unix操作系统,下面的笔记是我从鸟菜中摘抄出来的重要命令,并进行了一定的排版,摒弃了一些用不到的乱七八糟的命令,目的是在生产环境中能够快速的定位并查询需要命令的 ...

  9. Arch系统软件列表

    1. 安装统计 2. 安装列表 3. 安装说明 4. 作为依赖项的安装列表 5. 更正 mangaro使用减的方式安装系统.开箱即用的豪华版本,大部分人需要的都有了,同样包括个别用户不需要的,配置方面 ...

随机推荐

  1. 【CodeForces】【311C】Fetch the Treasures

    最短路 神题一道…… //CF 311C #include<queue> #include<cstdio> #include<cstdlib> #include&l ...

  2. GS线程

    void GameServer::ProcessThread() { try {//在ui线程里面搞个大try不是说try效率不好吗,难道只是为了出现错误发现在GS线程里面出现的吗 ProcessTh ...

  3. 在JavaScript中实现yield,实用简洁实现方式。

    原题还是老赵的: http://blog.zhaojie.me/2010/06/code-for-fun-iterator-generator-yield-in-javascript.html 原以为 ...

  4. JS获取节点方法

    1. 通过顶层document节点获取:(1) document.getElementById(elementId):该方法通过节点的ID,可以准确获得需要的元素,是比较简单快捷的方法.如果页面上含有 ...

  5. 如何用 Parse 和 Swift 搭建一个像 Instagram 那样的应用?(2)

    [编者按]本篇文章作者是 Reinder de Vries,既是一名企业家,也是优秀的程序员,发表多篇应用程序的博客.本篇文章中,作者主要介绍了如何基于 Parse 特点,打造一款类似 Instagr ...

  6. Pots of gold game:看谁拿的钱多

    问题描述: Pots of gold game: Two players A & B. There are pots of gold arranged in a line, each cont ...

  7. javascript的Function 和其 Arguments

    http://shengren-wang.iteye.com/blog/1343256 javascript的Function属性:1.Arguments对象2.caller 对调用单前函数的Func ...

  8. 概述Log4j简介

    在强调可重用组件开发的今天,除了自己从头到尾开发一个可重用的日志操作类外,Apache为我们提供了一个强有力的日志操作包-Log4j. Log4j是Apache的一个开放源代码项目,通过使用Log4j ...

  9. Xamarin for Mac 2.0.2.35 稳定版 破解补丁 Preview 2

    注意:该破解补丁未经过广泛测试 前提概要 1.该补丁,仅涉及以下产品,所有版权归 Xamarin 所有,仅供学习练手: ① Xamarin.iOS 8.10.2.37 Xamarin.iOS 8.10 ...

  10. hdu 4023 Game 博弈论

    思路: 将15种分成5类: 1.1和2为一类: 2.3,4,5,6为一类: 3.7,8,9,10为一类: 4.11,12,13,14,15为一类: 5.15为一类. 比较各类的优先级,就会发现放置的顺 ...