一段打印代码

打印代码

C# code
        
        
using System; using System.Collections.Generic; using System.Text; using System.Drawing.Printing; using System.Windows.Forms; using xYuanShian.TOOLS; using System.Drawing; namespace xYuanShian.SaleSystem { public class PrintHelper { /// <summary> /// 文档打印对象 /// </summary> private PrintDocument lp; /// <summary> /// 打印队列 /// </summary> private List < string > TextQueue; /// <summary> /// 打印的纵坐标 /// </summary> private int YPos = 0 ; /// <summary> /// 被打印的文本字体属性 /// </summary> private Font Font; /// <summary> /// 打印预览 /// </summary> private PrintPreviewDialog pView; /// <summary> /// 页面设置 /// </summary> private PageSetupDialog pPageSetup; /// <summary> /// 选择打印机 /// </summary> private PrintDialog pSPrint; /// <summary> /// 打印标题 /// </summary> public string Title; /// <summary> /// 初始化打印机 /// </summary> private void InitPrint() { this .lp.DefaultPageSettings.Color = false ; this .lp.DefaultPageSettings.Landscape = false ; this .lp.DefaultPageSettings.Margins = new Margins( 0 , 0 , 0 , 0 ); this .lp.DefaultPageSettings.PaperSize = new PaperSize( " 自定义 " , 200 , 0 ); } /// <summary> /// 构造函数 /// </summary> /// <param name="lp"> 打印机 </param> /// <param name="font"> 打印的字体 </param> public PrintHelper( PrintDocument lp, Font font ) { this .lp = lp; this .lp.PrintPage += PrintEvent_Page; this .InitPrint(); this .Font = font; this .pView = new PrintPreviewDialog(); this .pPageSetup = new PageSetupDialog(); this .pSPrint = new PrintDialog(); } /// <summary> /// 设置纸张和边距 /// </summary> /// <param name="migs"> 边距 </param> /// <param name="paperSize"> 纸张规格 </param> public void SettingPrint( Margins migs, PaperSize paperSize ) { if ( migs != null ) this .lp.DefaultPageSettings.Margins = migs; if ( paperSize != null ) this .lp.DefaultPageSettings.PaperSize = paperSize; } /// <summary> /// 打印文件 /// </summary> public void PrintString( string strText ) { this .TextQueue = new List < string > (); if ( ! String.IsNullOrEmpty( strText ) ) { this .TextQueue.AddRange( strText.Split( new string [] { Environment.NewLine }, StringSplitOptions.None ) ); try { this .lp.Print(); } catch ( Exception ex ) { // 打印出错处理 MsgAlter.Alter( " 打印小票发生错误: " + ex.Message ); } } GC.Collect(); } /// <summary> /// 设置预览 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void SetPreviewDialog() { // 设置Document属性 this .pView.Document = this .lp; try { // 显示打印预览窗口 this .pView.ShowDialog(); } catch ( Exception ex ) { // 显示打印出错消息 MsgAlter.Alter( " 打印出错: " + ex.Message ); } } /// <summary> /// 设置页面 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void PageSetup() { this .pPageSetup.Document = this .lp; try { // 显示打印页面设置窗口 this .pPageSetup.ShowDialog(); } catch ( Exception ex ) { // 显示打印出错消息 MsgAlter.Alter( " 打印出错: " + ex.Message ); } } /// <summary> /// 设置打印机 /// </summary> public void PrintDialog( string strText ) { this .pSPrint.Document = this .lp; // 显示打印窗口 if ( this .pSPrint.ShowDialog() == DialogResult.OK ) { try { this .PrintString( strText ); } catch ( Exception ex ) { // 显示打印出错消息 MsgAlter.Alter( " 打印出错: " + ex.Message ); } } } /// <summary> /// 页面打印事件 /// </summary> /// <param name="Sender"></param> /// <param name="e"></param> private void PrintEvent_Page( object Sender, PrintPageEventArgs e ) { /// 获得绘制对象 Graphics g = e.Graphics; /// 顶边界 float topMargin = e.MarginBounds.Top; /// 获取打印纵坐标 YPos = ( int )topMargin; SizeF s; Rectangle rectText; if ( ! String.IsNullOrEmpty( Title ) ) { s = g.MeasureString( Title, new Font( " 宋体 " , 14 ), e.MarginBounds.Width ); rectText = new Rectangle( 0 , YPos, e.MarginBounds.Width, ( int )s.Height + 1 ); g.DrawString( Title, new Font( " 宋体 " , 14 ), Brushes.Black, rectText ); YPos += ( int )s.Height + 1 ; } string PrintText; /// 每次从文件中读取一行并打印 foreach ( string strTmp in this .TextQueue ) { PrintText = strTmp; if ( String.IsNullOrEmpty( strTmp ) ) PrintText = " " ; /// 计算出打印指定文本的所需要的矩形区域大小 s = g.MeasureString( PrintText, this .Font, e.MarginBounds.Width ); /// 画出矩形区域 rectText = new Rectangle( 0 , YPos, e.MarginBounds.Width, ( int )s.Height + 1 ); /// 开始绘制 g.DrawString( PrintText, this .Font, Brushes.Black, rectText ); YPos += ( int )s.Height + 1 ; } TextQueue.Clear(); e.HasMorePages = false ; } /// <summary> /// 可以计算分页的打印事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void printDocument11_PrintPage( object sender, PrintPageEventArgs e ) { // 打印信息处理 /// /获得绘制对象 // Graphics g = e.Graphics; /// /一页中的行数 // float LinePages = 0; /// /待绘文本的纵向坐标 // float YPos = 0; /// /行计数7 // int count = 0; /// /左边界 // float LeftMargin = e.MarginBounds.Left; /// /顶边界 // float topMargin = e.MarginBounds.Top; /// /字符串流 // String line = null; /// /根据页面的高度和字体的高度计算 /// /一页中可以打印的行数 // LinePages = e.MarginBounds.Height / this.Font.GetHeight( g ); /// /if ( i >= this.richTextBox1.Lines.Length - 1 ) /// /{ /// / e.HasMorePages = false; /// / return; /// /} /// /每次从文件中读取一行并打印 // while ( count < LinePages && ( ( line = this.richTextBox1.Lines[i++] ) != null ) ) // { // // 计算这一行的显示位置 // YPos = topMargin + ( count * this.Font.GetHeight( g ) ); // // 绘制文本 // SizeF s = g.MeasureString( "你说什么时候啊你说什么时候", new Font( "宋体", 9 ) ); // g.DrawString( line, this.Font, Brushes.Black, LeftMargin, YPos, new StringFormat() ); // // 行数增加 // count++; // if ( i > this.richTextBox1.Lines.Length - 1 ) // break; // } /// /如果有多行,则另外打印一页 // if ( line != null ) // e.HasMorePages = true; // else // e.HasMorePages = false; } /// <summary> /// 微软网站上的打印事件代码 /// </summary> /// <param name="sender"></param> /// <param name="ev"></param> private void pd_PrintPage( object sender, PrintPageEventArgs ev ) { // float linesPerPage = 0; // float yPos = 0; // int count = 0; // float leftMargin = ev.MarginBounds.Left; // float topMargin = ev.MarginBounds.Top; // string line = null; /// / Calculate the number of lines per page. // linesPerPage = ev.MarginBounds.Height / // printFont.GetHeight( ev.Graphics ); /// / Print each line of the file. // while ( count < linesPerPage && // ( ( line = streamToPrint.ReadLine() ) != null ) ) // { // yPos = topMargin + ( count * // printFont.GetHeight( ev.Graphics ) ); // ev.Graphics.DrawString( line, printFont, Brushes.Black, // leftMargin, yPos, new StringFormat() ); // count++; // } /// / If more lines exist, print another page. // if ( line != null ) // ev.HasMorePages = true; // else // ev.HasMorePages = false; } } } 用例: PrintHelper ph = new PrintHelper( this .lp, this .Font ); ph.Title = " 标题 " ; ph.PrintString( " 文本内容 " );

另一段打印代码

Imports   System.IO   
Imports   System.Drawing.Printing  


  Public   Class   PrintingExample  
          Private   printFont   As   Font  
          Private   streamToPrint   As   StreamReader  
          Private   Shared   filePath   As   String  
          Public   Sub   New()  
                  Printing()  
          End   Sub  


          '   The   PrintPage   event   is   raised   for   each   page   to   be   printed.  
          Private   Sub   pd_PrintPage(ByVal   sender   As   Object,   ByVal   ev   As   PrintPageEventArgs)  
                  Dim   linesPerPage   As   Single   =   0  
                  Dim   yPos   As   Single   =   0  
                  Dim   count   As   Integer   =   0  
                  Dim   leftMargin   As   Single   =   ev.MarginBounds.Left  
                  Dim   topMargin   As   Single   =   ev.MarginBounds.Top  
                  Dim   line   As   String   =   Nothing  
                  '   Calculate   the   number   of   lines   per   page.  
                  linesPerPage   =   ev.MarginBounds.Height   /   printFont.GetHeight(ev.Graphics)  
                  '   Iterate   over   the   file,   printing   each   line.  
                  While   count   <   linesPerPage  
                          line   =   streamToPrint.ReadLine()  
                          If   line   Is   Nothing   Then  
                                  Exit   While  
                          End   If  
                          yPos   =   topMargin   +   count   *   printFont.GetHeight(ev.Graphics)  
                          ev.Graphics.DrawString(line,   printFont,   Brushes.Black,   leftMargin,   _  
                                  yPos,   New   StringFormat)  
                          count   +=   1  
                  End   While  
                  '   If   more   lines   exist,   print   another   page.  
                  If   Not   (line   Is   Nothing)   Then  
                          ev.HasMorePages   =   True  
                  Else  
                          ev.HasMorePages   =   False  
                  End   If  
          End   Sub  


          '   Print   the   file.  
          Public   Sub   Printing()  
                  Try  
                          streamToPrint   =   New   StreamReader(filePath)  
                          Try  
                                  printFont   =   New   Font("Arial",   10)  
                                  Dim   pd   As   New   PrintDocument  
                                  AddHandler   pd.PrintPage,   AddressOf   pd_PrintPage  
                                  '   Print   the   document.  
                                  pd.Print()  
                          Finally  
                                  streamToPrint.Close()  
                          End   Try  
                  Catch   ex   As   Exception  
                          MessageBox.Show(ex.Message)  
                  End   Try  
          End   Sub   'Printing          


          '   This   is   the   main   entry   point   for   the   application.  
          Public   Shared   Sub   Main()  
                  Dim   args()   As   String   =   System.Environment.GetCommandLineArgs()  
                  Dim   sampleName   As   String   =   args(0)  
                  If   args.Length   <>   1   Then  
                          Console.WriteLine("Usage:   "   &   sampleName   &   "   <file   path>")  
                          Return  
                  End   If  
                  filePath   =   args(0)  
          End   Sub  
  End   Class 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值