zoukankan      html  css  js  c++  java
  • C# 实现打印

      using   System;  
      using   System.Collections.Generic;  
      using   System.Text;  
       
      using   System.Drawing;  
      using   System.Drawing.Printing;  
      using   System.Windows.Forms;  
      using   System.IO;  
       
      namespace   MyCCTV  
      {  
              sealed   class   TextFilePrinter  
              {  
                      string   fileName;  
                      Encoding   theEncode;  
                      Font   theFont;  
                      StreamReader   srToPrint;  
                      int   currPage;  
       
                      public   TextFilePrinter(string   fileName)  
                              :   this(fileName,   Encoding.GetEncoding(936),   new   Font("MS   UI   Gothic",   9))  
                      {  
                      }  
       
                      public   TextFilePrinter(string   fileName,   Encoding   theEncode,   Font   theFont)  
                      {  
                              this.fileName   =   fileName;  
                              this.theEncode   =   theEncode;  
                              this.theFont   =   theFont;  
                      }  
       
                      public   void   Print()  
                      {  
                              using   (srToPrint   =   new   StreamReader(fileName,   theEncode))  
                              {  
                                      PrintDialog   dlg   =   new   PrintDialog();  
                                      dlg.Document   =   GetPrintDocument();  
                                      dlg.AllowSomePages   =   true;  
                                      dlg.AllowPrintToFile   =   false;  
                                      if   (dlg.ShowDialog()   ==   DialogResult.OK)   dlg.Document.Print();  
                              }  
                      }  
       
                      public   void   View()  
                      {  
                              using   (srToPrint   =   new   StreamReader(fileName,   theEncode))  
                              {  
                                      PrintPreviewDialog   dlg   =   new   PrintPreviewDialog();  
                                      dlg.Document   =   GetPrintDocument();  
                                      dlg.ShowDialog();  
                              }  
                      }  
       
                      PrintDocument   GetPrintDocument()  
                      {  
                              currPage   =   1;  
                              PrintDocument   doc   =   new   PrintDocument();  
                              doc.DocumentName   =   fileName;  
                              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   =   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());  
                              }  
                              currPage++;  
                              if   (isSomePages   &&   currPage   >   ev.PageSettings.PrinterSettings.ToPage)   return;  
                              if   (line   !=   null)   ev.HasMorePages   =   true;  
                      }  
              }  
      }  
  • 相关阅读:
    【转】UML中类与类之间的5种关系表示
    OSGI框架—HelloWorld小实例
    解决:“Workbench has not been created yet” error in eclipse plugin programming”,OSGI启动控制台报错问题
    Restful风格到底是什么?怎么应用到我们的项目中?
    Java程序员面试题集(1-50
    【转】Spring中@Component的作用
    【转】Spring AOP 实现原理与 CGLIB 应用
    【转】spring和springMVC的面试问题总结
    Java算法之“兔子问题”
    DDD创始人Eric Vans:要实现DDD原始意图,必须CQRS+Event Sourcing架构
  • 原文地址:https://www.cnblogs.com/chenbg2001/p/1369924.html
Copyright © 2011-2022 走看看