C# 调用PrintDocument对象进行打印和测试

话不多说,直接上代码

        //需要显示的行数

        int rowCount = 33;
        //每行高度
        int rowCol = 20;
        private int count = 40;//打印条数

        private int currentPrint = 0;


private void Myprinter()

{

            PrintDocument pd = new PrintDocument();//
            pd.PrintPage += new PrintPageEventHandler(printDocument_PrintA4Page);

            //打印机名称(开始->设备和打印机->查看打印机型号为Microsoft XPS Document Writer的设置名称为“XPS”,这是测试程序,在实际打印中改成实际打印机名称,次打印机不打印出来数据,在测试中会弹出窗口,让保存,然后打开保存文件,查看打印格式是否正确)

            pd.DefaultPageSettings.PrinterSettings.PrinterName = "XPS";
            pd.DefaultPageSettings.Landscape = true;//设置横向打印,不设置默认是纵向的
            pd.PrintController = new System.Drawing.Printing.StandardPrintController();
            int printCount = count / (rowCount - 1) + ((count % (rowCount - 1)) != 0 ? 1 : 0);
            currentPrint = 0;
            for (int i = 1; i <= printCount; i++)
            {
                currentPrint = i;
                pd.Print();
            }

}


private void printDocument_PrintA4Page(object sender, PrintPageEventArgs e)
        {
            Font titleFont = new Font("黑体", 14, System.Drawing.FontStyle.Bold);//标题字体           
            Font fntTxt = new Font("宋体", 10, System.Drawing.FontStyle.Regular);//正文文字           
            System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);//画刷           
            System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black);           //线条颜色           
            System.Drawing.Point po = new System.Drawing.Point(10, 10);

            try
            {
                e.Graphics.DrawString("打印标题信息", titleFont, brush, new System.Drawing.Point(400, 30));
                e.Graphics.DrawString("打印条数", fntTxt, brush, new System.Drawing.Point(20, 60));
                e.Graphics.DrawString("测试显示信息:" + "移动营业部", fntTxt, brush, new System.Drawing.Point(750, 60));
                e.Graphics.DrawString("上次打印时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss"), fntTxt, brush, new System.Drawing.Point(20, rowCount * rowCol + 85));
                e.Graphics.DrawString("本次打印时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss"), fntTxt, brush, new System.Drawing.Point(750, rowCount * rowCol + 85));
 
                //画矩形
                e.Graphics.DrawRectangle(pen, 22, 80, 1120, rowCount * rowCol);


                //划横线
                for (int i = 0; i < this.rowCount - 1; i++)
                {
                    Point[] points = { new Point(22, 80 + (i + 1) * this.rowCol), new Point(1142, 80 + (i + 1) * this.rowCol) };
                    e.Graphics.DrawLines(pen, points);
                }


                //划竖线
                Point[] points1 = { new Point(22 + 40, 80), new Point(22 + 40, 80 + this.rowCol * this.rowCount) };
                e.Graphics.DrawLines(pen, points1);
                Point[] points2 = { new Point(22 + 210, 80), new Point(22 + 210, 80 + this.rowCol * this.rowCount) };
                e.Graphics.DrawLines(pen, points2);
                Point[] points3 = { new Point(22 + 360, 80), new Point(22 + 360, 80 + this.rowCol * this.rowCount) };
                e.Graphics.DrawLines(pen, points3);
                Point[] points4 = { new Point(22 + 430, 80), new Point(22 + 430, 80 + this.rowCol * this.rowCount) };
                e.Graphics.DrawLines(pen, points4);
                Point[] points5 = { new Point(22 + 545, 80), new Point(22 + 545, 80 + this.rowCol * this.rowCount) };
                e.Graphics.DrawLines(pen, points5);
                Point[] points6 = { new Point(22 + 685, 80), new Point(22 + 685, 80 + this.rowCol * this.rowCount) };
                e.Graphics.DrawLines(pen, points6);
                Point[] points7 = { new Point(22 + 810, 80), new Point(22 + 810, 80 + this.rowCol * this.rowCount) };
                e.Graphics.DrawLines(pen, points7);
                Point[] points8 = { new Point(22 + 895, 80), new Point(22 + 895, 80 + this.rowCol * this.rowCount) };
                e.Graphics.DrawLines(pen, points8);


                //头部
                e.Graphics.DrawString("序号", fntTxt, brush, new System.Drawing.Point(25, 82));
                e.Graphics.DrawString("测试一", fntTxt, brush, new System.Drawing.Point(85, 82));
                e.Graphics.DrawString("测试二", fntTxt, brush, new System.Drawing.Point(265, 82));
                e.Graphics.DrawString("测试三, fntTxt, brush, new System.Drawing.Point(380, 82));
                e.Graphics.DrawString("测试四", fntTxt, brush, new System.Drawing.Point(455, 82));
                e.Graphics.DrawString("测试五, fntTxt, brush, new System.Drawing.Point(600, 82));
                e.Graphics.DrawString("测试六", fntTxt, brush, new System.Drawing.Point(740, 82));
                e.Graphics.DrawString("测试七, fntTxt, brush, new System.Drawing.Point(840, 82));
                e.Graphics.DrawString("测试八, fntTxt, brush, new System.Drawing.Point(920, 82));
                for (int i = 1; i < this.rowCount; i++)
                {
                    e.Graphics.DrawString(i.ToString(), fntTxt, brush, new Point(34, 85 + (i + 1) * this.rowCol - 22));//序号
                }
                int index = 0;
                for (int i = (currentPrint - 1) * (rowCount - 1); i < count; i++)
                {
                    if (index == rowCount - 1)
                    {
                        break;
                    }
                    e.Graphics.DrawString("622021600BM030047", fntTxt, brush, new Point(65, 105 + (i - (currentPrint - 1) * (rowCount - 1)) * this.rowCol));//
                    e.Graphics.DrawString(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), fntTxt, brush, new Point(235, 105 + (i - (currentPrint - 1) * (rowCount - 1)) * this.rowCol));
                    e.Graphics.DrawString("测试数据1", fntTxt, brush, new Point(385, 105 + (i - (currentPrint - 1) * (rowCount - 1)) * this.rowCol));
                    e.Graphics.DrawString("测试数据2", fntTxt, brush, new Point(452, 105 + (i - (currentPrint - 1) * (rowCount - 1)) * this.rowCol));
                    e.Graphics.DrawString("测试数据3", fntTxt, brush, new Point(570, 105 + (i - (currentPrint - 1) * (rowCount - 1)) * this.rowCol));
                    e.Graphics.DrawString("测试数据4", fntTxt, brush, new Point(706, 105 + (i - (currentPrint - 1) * (rowCount - 1)) * this.rowCol));
                    e.Graphics.DrawString("测试数据5", fntTxt, brush, new Point(830, 105 + (i - (currentPrint - 1) * (rowCount - 1)) * this.rowCol));
                    index++;
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }


展开阅读全文

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