我需要在程序中实现实时打印,例如磅道,没过一车,就将计重数据打印出来,并等待下一车,直至到该纸的底部或者打够一定的数目,用控件printDocument 的Print,会打完一条数据后就退纸。有没有什么办法实现我需要的功能啊?

解决方案 »

  1.   

    纸张大小没问题,我已经设到9999cm了,下面是我的代码的一部分,测试用的,比较乱,谢谢了   private void button1_Click(object sender, EventArgs e)
            {
                printDocument1.PrintPage += new PrintPageEventHandler(MyPrintDoc_PrintPage);
                try
                {
                    //printPreviewDialog1.Document = printDocument1;
                    //printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
                    //printPreviewDialog1.ShowDialog();
                    printDocument1.Print();
                }
                catch
                {
                    MessageBox.Show("请安装打印机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }        }
           protected void MyPrintDoc_PrintPage(object sender, PrintPageEventArgs e)
            {
                //字体 颜色 格式 坐标
                drawFont = new Font("Arial", 8);
                TitledrawFont = new Font("宋体", 15);
                drawBrush = new SolidBrush(Color.Black);
                x = 0F;
                y = 0F;
                drawFormat.FormatFlags = StringFormatFlags.NoWrap;
                //线条长度
                Pen line = new Pen(drawBrush, 1);
                line.DashStyle = DashStyle.Dot; //虚线
                
                float leftbianJu = 120;
                float topbianJu = titleHeight + rowHeight * 5 + 1;
                float tableWidth = 690;
                float tableHeight = 387;
                float cellwidth = 30;
                float cellwidth1 = 66;
                count++;
                if (count == 1)
                {
                    printTop(e, leftbianJu, topbianJu, tableWidth, tableHeight, cellwidth, cellwidth1, line);
                    printMiddle(count, e, leftbianJu, topbianJu, tableWidth, tableHeight, cellwidth, cellwidth1, line);
                }
                else if (count < 22)
                {
                    printMiddle(count, e, leftbianJu, topbianJu, tableWidth, tableHeight, cellwidth, cellwidth1, line);
                }
                else
                {
                    printMiddle(count, e, leftbianJu, topbianJu, tableWidth, tableHeight, cellwidth, cellwidth1, line);
                    // 尾
                    printLow(count, e, leftbianJu, topbianJu, tableWidth, tableHeight, cellwidth, cellwidth1, line);
                }        }
      

  2.   

    那就在printdocment 的事件中一直循环你所需要打印的东西 
      

  3.   

    这种方式  貌似只有 把页面东西输出完,然后  printDocument1.Print();就会打印,并且 这个时候打印,会把纸切断,只是听说 什么POS/ESC 控制,我也没找到 C#方面的 资料
      

  4.   

    看来printDocument1.Print();是不能实现了?!
      

  5.   


     private void tbtnPrint_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("你确定打印吗?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    printDocument.Print();
                }
            }        private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                string strSql = "select * from adm_branch where branch_level='0' and parent_code=branch_code";
                DataTable dt = myDataLayer.getQueryResult("", strSql);            strSql = "select * from v_transport_land_subbill where transport_no='" + strTransportNo + "'";
                strSql += " ORDER BY id";
                DataTable dtTransportLand = myDataLayer.getQueryResult("", strSql);            try
                {
                    string strpage = "";
                    if (dtTransportLand.Rows.Count % 40 != 0 && (dtTransportLand.Rows.Count + 40) % 40 != 0 && dtTransportLand.Rows.Count!=0)
                    {
                        strpage  = ((int)(intPageIndex / 40 + 1) + "/" + (int)(dtTransportLand.Rows.Count / 40 + 1));
                    }
                    else
                    {
                        strpage = ((int)(intPageIndex / 40 + 1) + "/" + (int)(dtTransportLand.Rows.Count / 40 ));
                    }
                    e.Graphics.DrawString(dt.Rows[0]["branch_name"].ToString() + "汽运专线配货清单", new Font("宋体", 14, FontStyle.Bold), Brushes.Black, 260, 70);
                    e.Graphics.DrawString(strpage+"页", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 760, 70);                e.Graphics.DrawString("专线名:" + dtTransportLand.Rows[0]["line_name"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 60, 100);
                    e.Graphics.DrawString("起运地:" + dtTransportLand.Rows[0]["from_site"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 210, 100);
                    e.Graphics.DrawString("目的地:" + dtTransportLand.Rows[0]["to_site"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 360, 100);
                    e.Graphics.DrawString("发车时间:" + dtTransportLand.Rows[0]["transport_date"].ToString().Trim() + " " + dtTransportLand.Rows[0]["departure_time"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 510, 100);                e.Graphics.DrawString("车牌号:" + dtTransportLand.Rows[0]["vehicle_no"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 60, 130);
                    int intqyt = 0;
                    int intbills = 0;
                    double dweight = 0;
                   
                    e.Graphics.DrawString("票数:" + dtTransportLand.Rows[0]["total_bills"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 210, 130);
                    e.Graphics.DrawString("总件数:" + dtTransportLand.Rows[0]["total_qty"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 360, 130);
                    e.Graphics.DrawString("总重量:" + dtTransportLand.Rows[0]["total_weight"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 510, 130);                e.Graphics.DrawString("司机:" + dtTransportLand.Rows[0]["driver_name1"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 60, 160);
                    e.Graphics.DrawString("手机:" + dtTransportLand.Rows[0]["driver_mobile1"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 210, 160);
                    e.Graphics.DrawString("司机:" + dtTransportLand.Rows[0]["driver_name2"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 360, 160);
                    e.Graphics.DrawString("手机:" + dtTransportLand.Rows[0]["driver_mobile2"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 510, 160);                e.Graphics.DrawString("序号", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 60, 200);
                    e.Graphics.DrawString("单号", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 120, 200);
                    e.Graphics.DrawString("目的地", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 190, 200);
                    e.Graphics.DrawString("件数", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 260, 200);
                    e.Graphics.DrawString("重量", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 320, 200);
                    e.Graphics.DrawString("体积", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 380, 200);
                    e.Graphics.DrawString("收货人", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 440, 200);
                    e.Graphics.DrawString("备注", new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 620, 200);                int y = 40;
                    foreach (DataRow dr in dtTransportLand.Rows)
                    {
                        if (intPageIndex > dtTransportLand.Rows.Count-1) return;
                        
                        e.Graphics.DrawString((intPageIndex + 1).ToString(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 65, 200 + y);
                        e.Graphics.DrawString(dtTransportLand.Rows[intPageIndex]["subbill_no"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 100, 200+y);
                        e.Graphics.DrawString(dtTransportLand.Rows[intPageIndex]["to_site"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 200, 200 + y);
                        e.Graphics.DrawString(dtTransportLand.Rows[intPageIndex]["packing_qty"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 270, 200 + y);
                        e.Graphics.DrawString(dtTransportLand.Rows[intPageIndex]["cargo_weight"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 320, 200 + y);
                        if (Convert.ToDouble(dtTransportLand.Rows[intPageIndex]["cargo_size"]) > 0)
                        {
                            e.Graphics.DrawString(dtTransportLand.Rows[intPageIndex]["cargo_size"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 380, 200 + y);
                        }
                        e.Graphics.DrawString(dtTransportLand.Rows[intPageIndex]["receive_name"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 440, 200 + y);
                        e.Graphics.DrawString(dtTransportLand.Rows[intPageIndex]["transport_re"].ToString().Trim(), new Font("宋体", 10, FontStyle.Bold), Brushes.Black, 620, 200 + y);
                       
                        if (intPageIndex%40==0&&(intPageIndex+40)%40==0&&intPageIndex!=0)
                        {
                            intPageIndex++;
                            e.HasMorePages = true;
                            return;
                        }
                        y += 20;
                        intPageIndex++;
                    }
                }
                catch (Exception ex)
                { 
                
                }
            }
      

  6.   

    虽然没有找到解决办法,不过最终还是通过ESC和制表符做的(偷懒是不行滴!~~~),所以还是谢谢大家的回答,由于ruanwei1987提到了ESC,分没多少,还是给他吧。希望大家再找到能偷懒的打印控制方法,能够教我一下,谢谢了。QQ40072379