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 判断打印机是否在系统可用的打印机列表中 } }
C# 字符流打印类
免责声明:文章转载自《C# 字符流打印类》仅用于学习参考。如对内容有疑问,请及时联系本站处理。
上篇Bootstrap3 表格-鼠标悬停软件质量有什么特性?下篇
宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=