一个打印的好例子

阅读全文:http://www.cckan.net/forum.php?mod=viewthread&tid=37

先拉下面两个控件

然后看一下代码就明白了

每打印一页的时候要使用的事件

                                                                                                                                                                                                          

 

代码
   private   void  printDocument1_PrintPage( object  sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics graphics 
=  e.Graphics;
            e.Graphics.DrawString(
" 社区便民 服务百姓 " new  Font( " Arial " , 13f, FontStyle.Bold), Brushes.Black,  10 1 );
            redstr();

            Font ziti 
=   new  Font( new  FontFamily( " 宋体 " ), 10F, FontStyle.Bold);
            
// 行高
             float  h  =  ziti.GetHeight(graphics);
            
// 每一页的行数
             float  hangshu  =  ( int )(e.MarginBounds.Height  /  h); ;
            
// 在边矩
            
// float left = e.MarginBounds.Left;
             float  left  =   3 ;
            
// float shang = e.MarginBounds.Top;
             float  shang  =   45 ;
            
string  str  =   "" ;
            
for  ( int  i  =   0 ; i  <=  hangshu; i ++ )
            {
                str 
=  sr.ReadLine();
                
if  (str  ==   null )
                {
                    e.HasMorePages 
=   false ;
                    
return ;
                }
                
float  x  =  left;
                
float  y  =  shang  +  i  *  h;
                graphics.DrawString(str, ziti, 
new  SolidBrush(Color.Black), x, y,  new  StringFormat());
            }
            e.HasMorePages 
=   true ;
        }

 

打印的内容

 

代码
   // 打印
         private  StringReader sr;

        
// 要打印的数据
         private   void  redstr()
        {
            StringBuilder builder1 
=   new  StringBuilder();
            builder1.Append(
" ...................... " );
            builder1.Append(
" \n " );
            builder1.Append(
" 名  称: 中国铁路 " );
            builder1.Append(
" \n " );
            builder1.Append(
" 车  次:  "   +  txtCheCi.Text.Trim());
            builder1.Append(
" \n " );
            builder1.Append(
" 起始站:  "   +  txtStart.Text.Trim());
            builder1.Append(
" \n " );
            builder1.Append(
" 到达站:  "   +  txtEnd.Text.Trim());
            builder1.Append(
" \n " );
            builder1.Append(
" 席  别:  "   +  cbbxibie.Text.Trim());
            builder1.Append(
" \n " );
            builder1.Append(
" 数  量:  "   +  nudNumber.Value.ToString().Trim()  +   " " );
            builder1.Append(
" \n " );
            builder1.Append(
" 单  价:  "   +  txtPrice.Text.Trim()  +   " " );
            builder1.Append(
" \n " );
            
string  shoufei  =  user.tran1(objOfficeInfo.ofPara1, nudNumber.Value  *  Convert.ToDecimal(txtPrice.Text.Trim()));
            builder1.Append(
" 手续费:  "   +  shoufei  +   " " );
            builder1.Append(
" \n " );
            builder1.Append(
" 总  价:  "   +  (Convert.ToDecimal(shoufei)  +  nudNumber.Value  *  Convert.ToDecimal(txtPrice.Text.Trim())).ToString().Trim()  +   " " );
            builder1.Append(
" \n " );
            builder1.Append(
" 订票时间: " );
            builder1.Append(
" \n " );
            builder1.Append(timeStart.Value.ToString().Trim() 
+   " -\n "   +  timeEnd.Value.ToString().Trim());
            builder1.Append(
" \n " );
            builder1.Append(
" ...................... " );
            builder1.Append(
" \n " );
            builder1.Append(
" 注:铁路客运车次、车票变化 " );
            builder1.Append(
" \n " );
            builder1.Append(
" 以铁路客运官方信息为准,此 " );
            builder1.Append(
" \n " );
            builder1.Append(
" 单据仅作订票凭证。 " );
            builder1.Append(
" \n " );
            builder1.Append(
" 打印时间:\n "   +  DateTime.Now.ToString());
            builder1.Append(
" \n " );
            builder1.Append(
" 营业编号: "   +  objOfficeInfo.ofLogin);
            builder1.Append(
" \n " );
            builder1.Append(
" 营业电话: "   +  objOfficeInfo.ofPhone);
            builder1.Append(
" \n " );

            
string  str  =  objOfficeInfo.ofAddress.ToString();
            
string  str1  =   "" ;
            
if  (str.Length  >   1 )
            {
                str1 
+=  str.Substring( 0 1 );
                
for  ( int  i  =   1 ; i  <  str.Length; i ++ )
                {
                    
if  (i  %   7   !=   0 )
                    {
                        str1 
+=  str.Substring(i,  1 );
                    }
                    
else
                    {
                        str1 
+=  str.Substring(i,  1 +   " \n " ;
                    }
                }
            }
            
else
            {
                str1 
=  str;
            }

            builder1.Append(
" 营业地址: "   +  str1);
            builder1.Append(
" \n " );
            builder1.Append(
" ...................... " );
            builder1.Append(
" \n " );
            builder1.Append(
" 全国加盟热线:15639808693 " );
            builder1.Append(
" \n " );
          
            sr 
=   new  StringReader(builder1.ToString());
        }

 

开始打印

 

 

代码
  try
                                {
                                    printDocument1.Print();
                                }
                                
catch  (Exception)
                                {
                                    MessageBox.Show(
" 打印时出错 " );
                                }

 

 

 

你可能感兴趣的:(例子)