C# 字符流打印类

摘要:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Drawing;usingSystem.Drawing.Printing;usingSystem.IO;usingSystem.Runtime.InteropServi
C# 字符流打印类第1张C# 字符流打印类第2张
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Drawing;
usingSystem.Drawing.Printing;
usingSystem.IO;
usingSystem.Runtime.InteropServices;
namespacePrintService
{
    sealed classTextFilePrinter
    {
        stringsTreamPriStr;
        Encoding theEncode;
        Font theFont;
        StreamReader srToPrint;
        intcurrPage;
        public TextFilePrinter(stringsTreamPriStr)
            : this(sTreamPriStr, Encoding.GetEncoding("utf-8"), new Font("新宋体", 10))
        {
        }
        public TextFilePrinter(stringsTreamPriStr, Encoding theEncode, Font theFont)
        {
            this.sTreamPriStr =sTreamPriStr;
            this.theEncode =theEncode;
            this.theFont =theFont;
        }
        public voidPrint()
        {
            srToPrint = new StreamReader(newMemoryStream(Encoding.UTF8.GetBytes(sTreamPriStr)));
            PrintDialog dlg = newPrintDialog();
            dlg.Document =GetPrintDocument();
            dlg.AllowSomePages = true;
            dlg.AllowPrintToFile = false;
            if (dlg.ShowDialog() ==DialogResult.OK) dlg.Document.Print();
        }
        /// <summary>
        ///不需要打印预览直接打印
        /// </summary>
        public voidPrint2()
        {
            srToPrint = new StreamReader(newMemoryStream(Encoding.UTF8.GetBytes(sTreamPriStr)));
            PrintDialog dlg = newPrintDialog();
            dlg.Document =GetPrintDocument();
            dlg.AllowSomePages = true;
            dlg.AllowPrintToFile = false;
            dlg.Document.Print();
        }
        public voidView()
        {
            srToPrint = new StreamReader(newMemoryStream(Encoding.UTF8.GetBytes(sTreamPriStr)));
            PrintPreviewDialog dlg = newPrintPreviewDialog();
            dlg.Document =GetPrintDocument();
            dlg.ShowDialog();
        }
        PrintDocument GetPrintDocument()
        {
            currPage = 1;
            PrintDocument doc = newPrintDocument();
            doc.DocumentName = "打印";
            doc.PrintPage += newPrintPageEventHandler(PrintPageEvent);
            returndoc;
        }
        void PrintPageEvent(objectsender, 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());

                ev.Graphics.DrawString(line, theFont, Brushes.Black, 2,
                  count * theFont.GetHeight(ev.Graphics) - 1, newStringFormat());
            }
            currPage++;
            if (isSomePages && currPage > ev.PageSettings.PrinterSettings.ToPage) return;
            if (line != null) ev.HasMorePages = true;
        }
    }
    public static classPrinterHel
    {
        //GetDefaultPrinter用到的API函数说明 
        [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref intsize);
        //SetDefaultPrinter用到的API函数声明 
        [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern bool SetDefaultPrinter(stringName);
        #region 获取本地打印机列表
        /// <summary> 
        ///获取本地打印机列表 
        /// </summary> 
        /// <returns>打印机列表</returns> 
        public static List<string>GetPrinterList()
        {
            List<string> printRet =Cprinter.GetLocalPrinter();
            returnprintRet;
        }
        #endregion 获取本地打印机列表
        #region 获取本机的默认打印机名称
        /// <summary> 
        ///获取本机的默认打印机名称 
        /// </summary> 
        /// <returns>默认打印机名称</returns> 
        public static stringGetDeaultPrinterName()
        {
            StringBuilder dp = new StringBuilder(256);
            int size =dp.Capacity;
            if (GetDefaultPrinter(dp, refsize))
            {
                returndp.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        #endregion 获取本机的默认打印机名称
        #region 设置默认打印机
        /// <summary> 
        ///设置默认打印机 
        /// </summary> 
        /// <param name="PrinterName">可用的打印机名称</param> 
        public static void SetPrinterToDefault(stringPrinterName)
        {
            SetDefaultPrinter(PrinterName);
        }
        #endregion 设置默认打印机
        #region 判断打印机是否在系统可用的打印机列表中
        ///// <summary> 
        ///// 判断打印机是否在系统可用的打印机列表中 
        ///// </summary> 
        ///// <param name="PrinterName">打印机名称</param> 
        ///// <returns>是:在;否:不在</returns> 
        public static bool PrinterInList(stringPrinterName)
        {
            bool bolRet = false;
            List<string> alPrinters =GetPrinterList();
            for (int i = 0; i < alPrinters.Count; i++)
            {
                if (PrinterName ==alPrinters[i].ToString())
                {
                    bolRet = true;
                    break;
                }
            }
            alPrinters.Clear();
            alPrinters = null;
            returnbolRet;
        }
        #endregion 判断打印机是否在系统可用的打印机列表中
    }
}
View Code

免责声明:文章转载自《C# 字符流打印类》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Bootstrap3 表格-鼠标悬停软件质量有什么特性?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

vant-picker二次封装

痛点在项目经常会遇到这样的设计,下拉选择框,在vant中没有提供直接的select组件,但是可以使用Field、Popup和Picker这三个组件组合来完成。this.show;}},watch:{selectValue:function{this.result=newVal;},result{this.$emit;}}};效果链接:https://www....

凤凰系统(Phoenix OS)PC版安装,电脑上体验功能丰富的安卓系统

Tid=9074&fid=12安装前准备下载最新的PhoenixOSIO安装映像,并将其刻录到USB闪存驱动器;如果使用Windows操作系统,建议使用UltraISO刻录。下载地址:http://www.phoenixos.com/download_x86开始安装。1.重新启动机器,从启动选项中选择USB闪存磁盘启动,然后选择“Installati...

SpringBoot工程通过Maven引入自定义Jar包

A工程为:common工程打成jar包:common-0.0.1-SNAPSHOT.jar注意:A工程打包时要使用maven的插件进行打包,不然会打成SpringBoot的Jar包,无法使用。--字符集编码--˃打包时跳过测试配置1.8˂!...

WritableWorkbook 详细用例 (转)

1WritableWorkbookworkbook=工作簿.createWorkbook(newFile(“d:\test.xls”));1Workbookwb=工作簿.getWorkbook(newFile(“src\test\test.xls”));...

Activiti-个人任务

1.分配任务所有者1.1固定分配在业务流程建模期间指定固定任务所有者;在properties视图中,填写Assignee项作为任务所有者;注:通过固定分配方法,任务是逐步执行的,任务负责人将根据bpmn的配置分配给每个任务;1.2表达式分配1.2.1 UEL表达式Activiti使用UEL表达式,UEL是javaEE6...