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. 增强学习贪心算法与Softmax算法

    (一) 这个算法是基于一个概率来对探索和利用进行折中:每次尝试时,以概率进行探索,即以均匀概率随机选取一个摇臂,以的概率进行利用,即以这个概率选择当前平均奖赏最高的摇臂(如有多个,则随机选取). 其中 ...

  2. perl随记(1)

    chmod 755 test,改变文件权限,rwx 421 chomp,如果字符串结尾有换行符,chomp 可以去掉它 正则表达式量词:*表示有或无,+表示1或多,?表示0或1 my,局部变量,只在定 ...

  3. ajax跨域请求,页面和java服务端的写法

    方法一(jsonp): 页面ajax请求的写法: $.ajax({ type : "get", async : false, cache : false, url : " ...

  4. 全球SEO行业调查报告

    这是一份来自MOZ的调查报告,本报告是两年一次的SEO行业调查,主要围绕SEO从业人员的特征.工作内容时间分配比例.对未来市场的看法.使用的seo工具以及SEO知识扩充渠道等展开. 这份报告可以对从事 ...

  5. Codeforces Round #263 (Div. 2)

    吐槽:一辈子要在DIV 2混了. A,B,C都是简单题,看AC人数就知道了. A:如果我们定义数组为N*N的话就不用考虑边界了 #include<iostream> #include &l ...

  6. iOS7 状态栏 修改为白色字体的步骤

    1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的ViewController中在ViewDidLoad方 ...

  7. Sqli-labs less 59

    Less-59 与less58一致,直接给出一个示例payload: http://127.0.0.1/sqli-labs/Less-59/?id=-1 union select extractval ...

  8. 金融证券协议FIX/FAST/STEP

    金融信息交换协议FIX是适用于实时证券.金融电子交易的数据通信标准.它是把各类证券金融业务需求流程格式化,使之成为一个可用计 算机语言描述的功能流程,并在每个业务功能接口上统一交换格式.STEP(Se ...

  9. MySQL 5.6 和 MariaDB-10.0 的性能比较测试

    MySQL 5.6 和 MariaDB-10.0 的性能比较测试 时间 2013-02-14 10:11:34  开源中国 原文  http://www.oschina.net/question/12 ...

  10. Java异常类和自定义异常类

    自定义异常类: public class ExtendsException extends Exception { private static final long serialVersionUID ...