使用printdocument 实现换行打印

     

 button的单机事件中 new 一个printdocuent  如果你想显示预览  也可以先new 一个printdialog

 printdocument  document = new  printdocument();

document.print()//document.benginPrint();//调用printdocument的print方法就会就会执行 printdocumen的printdocument的printpage方法

 如果需要设置一写参数

比如每一分的打印张数

 documnet.PrinterSettings.Copies = short.Parse(this.updown_text.Value.ToString());

比如逐页打印

documnet.PrinterSettings.Collate = true;

//定义一个长度 每行显示数据的长度

        int strleng = 0;
        private void printDocument_PrintPage(object sender, System.Drawing.Printing.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 = new Font("宋体", 9, FontStyle.Regular);//正文文字           


            //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++;

            //    }}
            //    }

//通过输入的长度确定换行打印,数据长度相同的数据 
            String file;
            foreach (ListViewItem lt in GuanDataList.Items)
            {
              
                file = string.Format("序号:" + lt.SubItems[0].Text + "日期时间:" + lt.SubItems[1].Text + "冠字号:" + lt.SubItems[2].Text + "行名:" + lt.SubItems[3].Text + "行号:" + lt.SubItems[4].Text + "币种:" + lt.SubItems[5].Text + "面额:" + lt.SubItems[6].Text + "版本:" + lt.SubItems[7].Text + "真假:" + lt.SubItems[8].Text + "设备机械码:" + lt.SubItems[9].Text + "捆号:" + lt.SubItems[10].Text + "流通类型:" + lt.SubItems[11].Text);
                strleng = file.Length;
                SB.Append(file);
            }
            string str = "";
            int y = SB.ToString().Length / strleng;
            for (int i = 0; i <= SB.ToString().Length / strleng; i++)//接受strleng长度的字符就换行
            {
                if (i < y)
                {
                    str += SB.ToString().Substring(i * strleng, strleng) + Environment.NewLine;
                }
                else
                {
                    str += SB.ToString().Substring(i * strleng);
                }
            }
            e.Graphics.DrawString(str, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, 80, 80);

        }





展开阅读全文

没有更多推荐了,返回首页