public static void printe_receipt(PrintPageEventArgs e)
        {
            lowtax = 0;
            hightax = 0;
            drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
            e.Graphics.DrawString("Count", new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y);
            x = x + 50;
            e.Graphics.DrawString("FoodName", new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y);
            x = x + 100;
            e.Graphics.DrawString("Mark", new Font("verdana", 7, FontStyle.Regular), Brushes.Black, x, y);
            x = x + 80;
            e.Graphics.DrawString("Perp", new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y, drawFormat);
            x = x + 50;
            e.Graphics.DrawString("Tolp", new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y, drawFormat);
            y = y + 20;
            x = 2;
            for (int j = 0; j < ds.Tables["tb"].Rows.Count; j++)//行循环
            {
                string dishname1;
                account = ds.Tables["tb"].Rows[j][0].ToString();//每行第一列的字串参数
                 price = ds.Tables["tb"].Rows[j][3].ToString();
                 if (price != null)
                 {
                     if (account == "└→")
                     {
                         account = "0.0";
                     }
                     tolprice = (Convert.ToDouble(price) * Convert.ToDouble(account)).ToString("f2");
                 }
                 if (account == "0.0" || account == "└→")// 为“0.0”则表示为特殊行
                {
                    account = "→";
                    price = "";
                    tolprice = "";
                }
                dishname1 = ds.Tables["tb"].Rows[j][1].ToString();                string group = ds.Tables["tb"].Rows[j][7].ToString();
                if (dishname1.Length > 112)
                {
                    dishname = dishname1.Substring(0, 11) + "...";
                }
                else
                {
                    dishname = dishname1;
                }
                 = ds.Tables["tb"].Rows[j][2].ToString();
                pertax =Convert.ToDecimal( ds.Tables["tb"].Rows[j][6].ToString());
                parameter.prints.totaleprice = ds.Tables["tb"].Rows[j][4].ToString();
                e.Graphics.DrawString(account, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y);
                x = x + 50;
                e.Graphics.DrawString(dishname, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y);
                x = x + 100;
                e.Graphics.DrawString(, new Font("verdana", 5, FontStyle.Regular), Brushes.Black, x, y);
                x = x + 80;
                e.Graphics.DrawString(price, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y, drawFormat);
                x = x + 50;
                e.Graphics.DrawString(tolprice, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y, drawFormat);
                y = y + 15;
                x = 2;
                if (group == "drink")
                {
                    hightax = hightax + pertax*Convert.ToDecimal(account);
                }
                else if (group == "food" & account != "→")
                {
                    lowtax = lowtax + pertax * Convert.ToDecimal(account);
                }
            }
            string line9 = "Totaal" + "         €";
            string line = "-----------------------------------------------------------";
            string lowtax1 = Math.Round(lowtax, 2).ToString();
            string hightax1 = Math.Round(hightax, 2).ToString();
            string line10 = "BTW(" + parameter.lowincl.ToString() + ")         €";
            string line11 = "BTW(" + parameter.highincl.ToString() + ")         €";
            e.Graphics.DrawString(line, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, 0, y);
            y = y + 15;
            e.Graphics.DrawString(line9, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, 120, y);
            e.Graphics.DrawString(parameter.prints.totaleprice, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, 250, y, drawFormat);
            y = y + 15;
            e.Graphics.DrawString(line10, new Font("verdana", 6, FontStyle.Regular), Brushes.Black, 120, y);
            e.Graphics.DrawString(lowtax1, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, 250, y, drawFormat);
            y = y + 15;
            e.Graphics.DrawString(line11, new Font("verdana", 6, FontStyle.Regular), Brushes.Black, 120, y);
            e.Graphics.DrawString(hightax1, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, 250, y, drawFormat);
        }//发票
我用e.Graphics.DrawString画好的一个页面,在这个方法里。 我能否一行行将它打印出来?想用如下的逐行打印,但不知道怎么去一行行读出自己刚才那个方法里画的。
private void printDocument_PrintPage(object sender,PrintPageEventArgs e)   {      Graphics g = e.Graphics; //获得绘图对象      float linesPerPage = 0; //页面的行号      float yPosition = 0;   //绘制字符串的纵向位置      int count = 0; //行计数器      float leftMargin = e.MarginBounds.Left; //左边距      float topMargin = e.MarginBounds.Top; //上边距      string line = null; 行字符串      Font printFont = this.textBox.Font; //当前的打印字体      SolidBrush myBrush = new SolidBrush(Color.Black);//刷子      linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);//每页可打印的行数      //逐行的循环打印一页      while(count < linesPerPage && ((line=lineReader.ReadLine()) != null))      {          yPosition = topMargin + (count * printFont.GetHeight(g));          g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());          count++;      }      //如果本页打印完成而line不为空说明还有没完成的页面这将触发下一次的打印事件在下一次的打印中lineReader会      //自动读取上次没有打印完的内容因为lineReader是这个打印方法外的类的成员它可以记录当前读取的位置      if(line != null)          e.HasMorePages = true;      else          e.HasMorePages = false;    }  

解决方案 »

  1.   


    private void printDocument_PrintPage(object sender,PrintPageEventArgs e)   {  
        Graphics g = e.Graphics; //获得绘图对象   
       float linesPerPage = 0; //页面的行号   
       float yPosition = 0;   //绘制字符串的纵向位置     
     int count = 0; //行计数器    
      float leftMargin = e.MarginBounds.Left; //左边距  
        float topMargin = e.MarginBounds.Top; //上边距   
       string line = null; 行字符串     
     Font printFont = this.textBox.Font; //当前的打印字体      
    SolidBrush myBrush = new SolidBrush(Color.Black);//刷子     
     linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);//每页可打印的数    
      //逐行的循环打印一页    
      while(count < linesPerPage && ((line=lineReader.ReadLine()) != null))  {           yPosition = topMargin + (count * printFont.GetHeight(g)); 
    g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());          
    count++;      
    }      
    //如果本页打印完成而line不为空说明还有没完成的页面这将触发下一次的打印事件在下一次的打印中lineReader会      
    //自动读取上次没有打印完的内容因为lineReader是这个打印方法外的类的成员它可以记录当前读取的位置      
    if(line != null)         
     e.HasMorePages = true;     
     else   
     e.HasMorePages = false;  
      }