文本文件打印类库(C#)
我写了一个打印文本文件的类库,功能包含:打印预览、打印。打印时能够选择打印机。能够指定页码范围。
调用方法很easy:
TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);
p.View();  // 打印预览
p.Print(); // 打印文件
使用 TextFilePrinter 类的下面构造函数能够指定打印时使用的字体:
TextFilePrinter(string fileName, Encoding theEncode, Font theFont)
以下測试程序执行时的截图:
点击“预览”button后:
点击“打印”button后:
这幅图中的打印机:“Microsoft Office Doument Image Writer”是 Microsoft Office 2003 软件提供一个虚拟打印机,用来调试打印程序很方便(使用“打印预览”也能够调试打印程序,但“打印预览”仅仅能使用默认的打印机和默认的打印属性。也不能设置页码范围),能够设置打印属性和页码范围以及打印份数。
使用它来调试打印程序,能够节省不少打印纸。为建设节约型社会作贡献 :)  
这幅图就是该虚拟打印机在屏幕上的显示的结果。
这里是測试程序的源码:
 // PrintFile.cs - 文件打印程序
// PrintFile.cs - 文件打印程序 // 编译方法: csc /t:winexe PrintFile.cs TextFilePrinter.cs
// 编译方法: csc /t:winexe PrintFile.cs TextFilePrinter.cs
 using System;
using System; using System.Drawing;
using System.Drawing; using System.Windows.Forms;
using System.Windows.Forms; using Skyiv.Util;
using Skyiv.Util;
 namespace Skyiv.Ben.Test
namespace Skyiv.Ben.Test {
{ class PrintFileForm : Form
  class PrintFileForm : Form {
  { TextBox tbxFileName;
    TextBox tbxFileName; 
     public PrintFileForm()
    public PrintFileForm() {
    { SuspendLayout();
      SuspendLayout(); 
       Button btnFileName = new Button();
      Button btnFileName = new Button(); btnFileName.Text = "文件名称";
      btnFileName.Text = "文件名称"; btnFileName.Location = new Point(10, 10);
      btnFileName.Location = new Point(10, 10); btnFileName.Size = new Size(60, 24);
      btnFileName.Size = new Size(60, 24); btnFileName.Click += new EventHandler(BtnFileName_Click);
      btnFileName.Click += new EventHandler(BtnFileName_Click);
 Button btnPrint = new Button();
      Button btnPrint = new Button(); btnPrint.Text = "打印";
      btnPrint.Text = "打印"; btnPrint.Location = new Point(75, 10);
      btnPrint.Location = new Point(75, 10); btnPrint.Size = new Size(60, 24);
      btnPrint.Size = new Size(60, 24); btnPrint.Click += new EventHandler(BtnPrint_Click);
      btnPrint.Click += new EventHandler(BtnPrint_Click);
 Button btnView = new Button();
      Button btnView = new Button(); btnView.Text = "预览";
      btnView.Text = "预览"; btnView.Location = new Point(140, 10);
      btnView.Location = new Point(140, 10); btnView.Size = new Size(60, 24);
      btnView.Size = new Size(60, 24); btnView.Click += new EventHandler(BtnView_Click);
      btnView.Click += new EventHandler(BtnView_Click);
 tbxFileName = new TextBox();
      tbxFileName = new TextBox(); tbxFileName.Text = "PrintFile.cs";
      tbxFileName.Text = "PrintFile.cs"; tbxFileName.Location = new Point(10, 44);
      tbxFileName.Location = new Point(10, 44); tbxFileName.Size = new Size(190, 20);
      tbxFileName.Size = new Size(190, 20); tbxFileName.ReadOnly = true;
      tbxFileName.ReadOnly = true; tbxFileName.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
      tbxFileName.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
 Controls.AddRange(new Control[]{btnFileName, btnPrint, btnView, tbxFileName});
      Controls.AddRange(new Control[]{btnFileName, btnPrint, btnView, tbxFileName}); Text = "文本文件打印程序";
      Text = "文本文件打印程序"; ClientSize = new Size(210, 80);
      ClientSize = new Size(210, 80);
 ResumeLayout(false);
      ResumeLayout(false); }
    } 
     void BtnFileName_Click(object sender, EventArgs e)
    void BtnFileName_Click(object sender, EventArgs e) {
    { OpenFileDialog dlg = new OpenFileDialog();
      OpenFileDialog dlg = new OpenFileDialog(); if(dlg.ShowDialog() != DialogResult.OK) return;
      if(dlg.ShowDialog() != DialogResult.OK) return; tbxFileName.Text = dlg.FileName;
      tbxFileName.Text = dlg.FileName; }
    }
 void BtnPrint_Click(object sender, EventArgs e)
    void BtnPrint_Click(object sender, EventArgs e) {
    { TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);
      TextFilePrinter p = new TextFilePrinter(tbxFileName.Text); p.Print();
      p.Print(); }
    } 
     void BtnView_Click(object sender, EventArgs e)
    void BtnView_Click(object sender, EventArgs e) {
    { TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);
      TextFilePrinter p = new TextFilePrinter(tbxFileName.Text); p.View();
      p.View(); }
    } 
     static void Main()
    static void Main() {
    { Application.Run(new PrintFileForm());
      Application.Run(new PrintFileForm()); }
    } }
  } }
}
这里是该类的源码:
 using System;
using System; using System.Drawing;
using System.Drawing; using System.Drawing.Printing;
using System.Drawing.Printing; using System.Windows.Forms;
using System.Windows.Forms; using System.IO;
using System.IO; using System.Text;
using System.Text;
 namespace Skyiv.Util
namespace Skyiv.Util {
{ sealed class TextFilePrinter
  sealed class TextFilePrinter {
  { string fileName;
    string fileName; Encoding theEncode;
    Encoding theEncode; Font theFont;
    Font theFont; StreamReader srToPrint;
    StreamReader srToPrint; int currPage;
    int currPage;
 public TextFilePrinter(string fileName)
    public TextFilePrinter(string fileName) : this(fileName, Encoding.GetEncoding("GB18030"), new Font("新宋体", 10))
      : this(fileName, Encoding.GetEncoding("GB18030"), new Font("新宋体", 10)) {
    { }
    }
 public TextFilePrinter(string fileName, Encoding theEncode, Font theFont)
    public TextFilePrinter(string fileName, Encoding theEncode, Font theFont) {
    { this.fileName = fileName;
      this.fileName = fileName; this.theEncode = theEncode;
      this.theEncode = theEncode; this.theFont = theFont;
      this.theFont = theFont; }
    }
 public void Print()
    public void Print() {
    { using (srToPrint = new StreamReader(fileName, theEncode))
      using (srToPrint = new StreamReader(fileName, theEncode)) {
      { PrintDialog dlg = new PrintDialog();
        PrintDialog dlg = new PrintDialog(); dlg.Document = GetPrintDocument();
        dlg.Document = GetPrintDocument(); dlg.AllowSomePages = true;
        dlg.AllowSomePages = true; dlg.AllowPrintToFile = false;
        dlg.AllowPrintToFile = false; if (dlg.ShowDialog() == DialogResult.OK) dlg.Document.Print();
        if (dlg.ShowDialog() == DialogResult.OK) dlg.Document.Print(); }
      } }
    }
 public void View()
    public void View() {
    { using (srToPrint = new StreamReader(fileName, theEncode))
      using (srToPrint = new StreamReader(fileName, theEncode)) {
      { PrintPreviewDialog dlg = new PrintPreviewDialog();
        PrintPreviewDialog dlg = new PrintPreviewDialog(); dlg.Document = GetPrintDocument();
        dlg.Document = GetPrintDocument(); dlg.ShowDialog();
        dlg.ShowDialog(); }
      } }
    }
 PrintDocument GetPrintDocument()
    PrintDocument GetPrintDocument() {
    { currPage = 1;
      currPage = 1; PrintDocument doc = new PrintDocument();
      PrintDocument doc = new PrintDocument(); doc.DocumentName = fileName;
      doc.DocumentName = fileName; doc.PrintPage += new PrintPageEventHandler(PrintPageEvent);
      doc.PrintPage += new PrintPageEventHandler(PrintPageEvent); return doc;
      return doc; }
    }
 void PrintPageEvent(object sender, PrintPageEventArgs ev)
    void PrintPageEvent(object sender, PrintPageEventArgs ev) {
    { string line = null;
      string line = null; float linesPerPage = ev.MarginBounds.Height / theFont.GetHeight(ev.Graphics);
      float linesPerPage = ev.MarginBounds.Height / theFont.GetHeight(ev.Graphics); bool isSomePages = ev.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages;
      bool isSomePages = ev.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages; if (isSomePages)
      if (isSomePages) {
      { while (currPage < ev.PageSettings.PrinterSettings.FromPage)
        while (currPage < ev.PageSettings.PrinterSettings.FromPage) {
        { for (int count = 0; count < linesPerPage; count++)
          for (int count = 0; count < linesPerPage; count++) {
          { line = srToPrint.ReadLine();
            line = srToPrint.ReadLine(); if (line == null) break;
            if (line == null) break; }
          } if (line == null) return;
          if (line == null) return; currPage++;
          currPage++; }
        } if (currPage > ev.PageSettings.PrinterSettings.ToPage) return;
        if (currPage > ev.PageSettings.PrinterSettings.ToPage) return; }
      } for (int count = 0; count < linesPerPage; count++)
      for (int count = 0; count < linesPerPage; count++) {
      { line = srToPrint.ReadLine();
        line = srToPrint.ReadLine(); if (line == null) break;
        if (line == null) break; ev.Graphics.DrawString(line, theFont, Brushes.Black, ev.MarginBounds.Left,
        ev.Graphics.DrawString(line, theFont, Brushes.Black, ev.MarginBounds.Left, ev.MarginBounds.Top + (count * theFont.GetHeight(ev.Graphics)), new StringFormat());
          ev.MarginBounds.Top + (count * theFont.GetHeight(ev.Graphics)), new StringFormat()); }
      } currPage++;
      currPage++; if (isSomePages && currPage > ev.PageSettings.PrinterSettings.ToPage) return;
      if (isSomePages && currPage > ev.PageSettings.PrinterSettings.ToPage) return; if (line != null) ev.HasMorePages = true;
      if (line != null) ev.HasMorePages = true; }
    } }
  } }
}
这些程序都相当简当明了,这里就不再解释了。
这个类库有个缺点:当文本文件里的一行不能在打印纸的一行中打印完时,该行的后半部就丢失了。
文本文件打印类库(C#)的更多相关文章
- C# 文本文件打印类库(C#)
		我写了一个打印文本文件的类库,功能包括:打印预览.打印.打印时可以选择打印机,可以指定页码范围.调用方法非常简单:TextFilePrinter p = new TextFilePrinter(tbx ... 
- whoosh----索引|搜索文本类库
		先了解基本概念和步骤: Quick Start Whoosh是一个索引文本和搜索文本的类库,他可以为你提供搜索文本的服务,比如如果你在创建一个博客的软件,你可以用whoosh为它添加添加一个搜索功能以 ... 
- 利用人工智能(Magpie开源库)给一段中文的文本内容进行分类打标签
		当下人工智能是真心的火热呀,各种原来传统的业务也都在尝试用人工智能技术来处理,以此来节省人工成本,提高生产效率.既然有这么火的利器,那么我们就先来简单认识下什么是人工智能吧,人工智能是指利用语音识别. ... 
- 移除HTML标签(CharacterUtility)类库
		最近时间,专案需要,写了一个类库,主要是为了移除HTML标签以及截取前一段文本的类库. 下载地址:http://download.cnblogs.com/insus/Library/Character ... 
- 怎样做出通用的pos小票打印程序
		POS小票打印机分为热敏和针式俩种. 打印纸的宽度分为58毫米.76毫米和80毫米三种. 打印接口分为:串口.并口.USB和网口(以太网). 热敏打印机速度较快,打印的时候噪音少,针打可以使用多联纸自 ... 
- Linux系统文件与目录管理(1)
		文件与目录的操作 在Linux系统的文件与目录的管理上,不外乎『显示属性』.『拷贝』.『删除文件』.『移动文件或目录』.『重命名』等常用操作,由于文件与目录的管理在 Linux当中是很重要的,尤其是每 ... 
- java-编码解码-流的操作规律
		一 编码解码 字符串:String 字节数组:byte[]字符串--编码(getBytes())-->字节数组字节数组--解码(new String(byte[]))-->字符串 publ ... 
- Linux 系统常用管理命令(精简笔记)
		Linux是一套免费使用和自由传播的类Unix操作系统,下面的笔记是我从鸟菜中摘抄出来的重要命令,并进行了一定的排版,摒弃了一些用不到的乱七八糟的命令,目的是在生产环境中能够快速的定位并查询需要命令的 ... 
- Arch系统软件列表
		1. 安装统计 2. 安装列表 3. 安装说明 4. 作为依赖项的安装列表 5. 更正 mangaro使用减的方式安装系统.开箱即用的豪华版本,大部分人需要的都有了,同样包括个别用户不需要的,配置方面 ... 
随机推荐
- 非常好的Linux教程,让你的linux之路更通畅
			1 第1讲.Linux应用与发展(上) 2013-10-22 17:43 | 播放(46) | 评论(0) | 时长:51:38 2 第1讲.Linux应用与发展(下) 2013-10-22 17 ... 
- hdu 4305 概率dp
			/* 题目大意:有n个房间由n-1个隧道连接起来,从1号房间开始, 每个节点i都有三种可能: 1.被杀死,回到节点1,概率为ki; 2.找到出口,离开迷宫,概率ei; 3.与它相连的有m个房间,到任意 ... 
- FIRST SCRAPY PRJ
			zpc@Lenovo-PC:/prj/pyscrapy/a$ scrapy startproject helloword New Scrapy project 'helloword' created ... 
- 如何通过友盟分析发布后App崩溃日志
			http://blog.csdn.net/totogo2010/article/details/39892467 要分析崩溃日志,首先需要保留发布时的编译出来的.xcarchive文件.这个文件包含了 ... 
- Linux网络编程一步一步学【转】
			转自:http://blog.chinaunix.net/uid-10747583-id-297982.html Linux网络编程一步一步学+基础 原文地址:http://blogold.chin ... 
- Unicode与UTF-8互转(C语言实现)   基本原理
			1. 基础 1.1 ASCII码 我们知道, 在计算机内部, 所有的信息最终都表示为一个二进制的字符串. 每一个二进制位(bit)有0和1两种状态, 因此八个二进制位就可以组合出 256种状态, 这被 ... 
- MSSQL—字符串分离(Split函数)
			前面提到了记录合并,有了合并需求肯定也会有分离需求,说到字符串分离,大家肯定会想到SPLIT函数,这个在.NET,Java和JS中都有函数,很可惜在SQL SERVER中没有,我们只能自己来写这么一个 ... 
- (2).net web api 请求方式与参数
			一.GET 二.POST 如果方法名前缀不带GET 默认为POST请求方法 1.无参数 2.带一个参数 客户端请求时,名称必须为空,不能是dictCategory.不是空的话,会返回空数据 [ ] 3 ... 
- HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)
			题目链接 2016 青岛网络赛 Problem C 题意 给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ... 
- Ribbon负载均衡(四)
			一.Ribbon定义 spring cloud Ribbon是基于Netflix Ribbon实现的一套客户端,负载均衡工具 简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端 ... 
