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;  
                  }  
          }  
  }  
原文地址:https://www.cnblogs.com/chenbg2001/p/1369924.html