using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Runtime.InteropServices; namespace PrintService
{ sealed class TextFilePrinter
{
string sTreamPriStr;
Encoding theEncode;
Font theFont;
StreamReader srToPrint;
int currPage; public TextFilePrinter(string sTreamPriStr)
: this(sTreamPriStr, Encoding.GetEncoding("utf-8"), new Font("新宋体", ))
{
} public TextFilePrinter(string sTreamPriStr, Encoding theEncode, Font theFont)
{
this.sTreamPriStr = sTreamPriStr;
this.theEncode = theEncode;
this.theFont = theFont;
} public void Print()
{
srToPrint = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(sTreamPriStr)));
PrintDialog dlg = new PrintDialog();
dlg.Document = GetPrintDocument();
dlg.AllowSomePages = true;
dlg.AllowPrintToFile = false;
if (dlg.ShowDialog() == DialogResult.OK) dlg.Document.Print(); } /// <summary>
/// 不需要打印预览直接打印
/// </summary>
public void Print2()
{
srToPrint = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(sTreamPriStr)));
PrintDialog dlg = new PrintDialog();
dlg.Document = GetPrintDocument();
dlg.AllowSomePages = true;
dlg.AllowPrintToFile = false;
dlg.Document.Print();
} public void View()
{
srToPrint = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(sTreamPriStr)));
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = GetPrintDocument();
dlg.ShowDialog();
} PrintDocument GetPrintDocument()
{
currPage = ;
PrintDocument doc = new PrintDocument();
doc.DocumentName = "打印";
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()); ev.Graphics.DrawString(line, theFont, Brushes.Black, ,
count * theFont.GetHeight(ev.Graphics) - , new StringFormat());
}
currPage++;
if (isSomePages && currPage > ev.PageSettings.PrinterSettings.ToPage) return;
if (line != null) ev.HasMorePages = true;
}
} public static class PrinterHel
{
//GetDefaultPrinter用到的API函数说明
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int size); //SetDefaultPrinter用到的API函数声明
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetDefaultPrinter(string Name); #region 获取本地打印机列表
/// <summary>
/// 获取本地打印机列表
/// </summary>
/// <returns>打印机列表</returns>
public static List<string> GetPrinterList()
{
List<string> printRet = Cprinter.GetLocalPrinter();
return printRet;
}
#endregion 获取本地打印机列表 #region 获取本机的默认打印机名称
/// <summary>
/// 获取本机的默认打印机名称
/// </summary>
/// <returns>默认打印机名称</returns>
public static string GetDeaultPrinterName()
{
StringBuilder dp = new StringBuilder();
int size = dp.Capacity;
if (GetDefaultPrinter(dp, ref size))
{
return dp.ToString();
}
else
{
return string.Empty;
}
}
#endregion 获取本机的默认打印机名称 #region 设置默认打印机
/// <summary>
/// 设置默认打印机
/// </summary>
/// <param name="PrinterName">可用的打印机名称</param>
public static void SetPrinterToDefault(string PrinterName)
{
SetDefaultPrinter(PrinterName);
}
#endregion 设置默认打印机 #region 判断打印机是否在系统可用的打印机列表中
///// <summary>
///// 判断打印机是否在系统可用的打印机列表中
///// </summary>
///// <param name="PrinterName">打印机名称</param>
///// <returns>是:在;否:不在</returns>
public static bool PrinterInList(string PrinterName)
{
bool bolRet = false;
List<string> alPrinters = GetPrinterList();
for (int i = ; i < alPrinters.Count; i++)
{
if (PrinterName == alPrinters[i].ToString())
{
bolRet = true;
break;
}
}
alPrinters.Clear();
alPrinters = null;
return bolRet;
}
#endregion 判断打印机是否在系统可用的打印机列表中
} }

C# 字符流打印类的更多相关文章

  1. java 字节流和字符流转换类InputStreamReader,OutPutStreamReader

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2pjMjExMzIy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  2. IO流总结---- 字节流 ,字符流, 序列化 ,数据操作流,打印流 , Properties 集合

    笔记内容: 什么是流 字节流 字符流 序列化 数据操作流(操作基本数据类型的流)DataInputStream 打印流 Properties 集合 什么是流: 流是个抽象的概念,是对输入输出设备的抽象 ...

  3. java IO输入输出流中的各种字节流,字符流类

    字节流字节流主要是操作byte类型数据,也byte数组为准,主要操作类就是·字节输出流:OutputStream·字节输入流:InputStream字符流在程序中一个字符等于2个字节,那么java提供 ...

  4. Java基础---IO(一)---IO流概述、字符流、字节流、流操作规律

    第一讲     IO概述 概述 1.IO流:即InputOutput的缩写. 2.特点: 1)IO流用来处理设备间的数据传输. 2)Java对数据的操作是通过流的方式. 3)Java用于操作流的对象都 ...

  5. Java 字符流文件读写

    上篇文章,我们介绍了 Java 的文件字节流框架中的相关内容,而我们本篇文章将着重于文件字符流的相关内容. 首先需要明确一点的是,字节流处理文件的时候是基于字节的,而字符流处理文件则是基于一个个字符为 ...

  6. Java 字符流与基本IO

    字符流基类 java.io包中专门用于字符流处理的类,是以 Reader 和 Writer 为基础派生的一系列类.字符流以字符为单位,根据码表映射字符,一次可能读多个字节,只能处理字符类型的数据.Re ...

  7. java io 学习笔记(三) 字符流读写

    1.字符流读取 字符流读取的所有类都是从Reader这个超类继承的,都是用于读取字符的,这些类分别是InputSteamReader(从字符流读取).FileReader(继承与InputStream ...

  8. 09、IO流—File类与IO流

    目录 一.File类 基本认识 实用方法 获取功能 重命名功能(包含剪切) 判断功能 创建.删除文件 实际小案例 二.IO流 1.认识IO流 2.IO流基类介绍 字节流基类介绍 字符流基类介绍 三.节 ...

  9. [Java IO]03_字符流

    Java程序中,一个字符等于两个字节. Reader 和 Writer 两个就是专门用于操作字符流的类. Writer Writer是一个字符流的抽象类.  它的定义如下: public abstra ...

随机推荐

  1. css图片切换效果分析+翻译整理

    Demos:http://tympanus.net/Tutorials/CSS3SlidingImagePanels/ 出处:http://tympanus.net/codrops/2012/01/1 ...

  2. C++ Template之非类型模板参数

    非类型模板参数是通过基本变量类型引入,例如int,在使用时必须显式自定值,不能通过推断. 非类型模板参数的限制:不能是浮点数(在vc6.0上测试可以为浮点型),对象以及指向内部链接对象的指针. #in ...

  3. BZOJ 1001: [BeiJing2006]狼抓兔子 最小割

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓 ...

  4. 2014ACM/ICPC亚洲区北京站 上交命题

    A http://acm.hdu.edu.cn/showproblem.php?pid=5112 输入n个时刻和位置,问那两个时刻间速度最快. 解法:按照时间排序,然后依次求相邻两个之间的速度,速度= ...

  5. Excel每隔两行自动求和一次怎么操作?

    今天ytkah得到一份数据,要求进行统计分析,由于是原始数据,还没处理过,数据量有点大,如下图所示(Excel每隔两行自动求和),每天的数字由两项组成,男生的人数.消费值和女生的人数和消费值,数字都在 ...

  6. c3p0 --1

    # # This file is detritus from various testing attempts  # the values below may change, and often do ...

  7. JobClient学习------作业提交与初始化

    public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); ...

  8. hadoop安装问题

    1. 运行start-dfs.sh启动HDFS守护进程,start-yarn.sh面向YARN的资源器和节点管理器,资源管理器web地址是http://localhost:8080/.输入stop.d ...

  9. CAP定理与RDBMS的ACID

    一.分布式领域CAP理论 CAP定理指在设计分布式系统时,一致性(Consistent).可用性(Availability).可靠性(分区容忍性Partition Tolerance)三个属性不可能同 ...

  10. ZOJ2929 Penalty Kick(概率)

    题目挺水的,但由于其独特的阅读量比赛的时候没发现这道水题,在此做一下翻译,如果有人搜到这翻译的话有帮助的话自然最好啦. 中国队平局进入最后的点球决胜局,首先抛硬币决定谁先罚球,然后先是罚五球,如果罚的 ...