using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 账单打印
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ChangeRows(Convert.ToInt32 (numericUpDown1.Value));
        }
        private void ChangeRows(int num)
        {
            //构建打印数据
            Random myrnd;
            myrnd = new Random();
            d1.AllowUserToAddRows = false;
            d1.RowCount = 0;
            d1.RowCount = num;
            for (int i = 0; i < num; i++)
            {
                d1.Rows[i].Cells[0].Value = "第" + Convert.ToString(i) + "行";
                d1.Rows[i].Cells[3].Value = System.Math.Round(myrnd.NextDouble()*10000,2);
                d1.Rows[i].Cells[5].Value = "第" + Convert.ToString(i) + "行的摘要";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //构建初始数据
            ChangeRows(100);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            myp.PageUnits = VBprinter.VB2008Print.PageExportUnit.CentiMeter;
            myp.IsImmediatePrintNotPreview = false;
            myp.IsUseDGVPadding = false;
            myp.CellMargin = new System.Drawing.Printing.Margins(10, 20, 10, 10);

            myp.InvalidatePreview();
            tabControl1.SelectedIndex = 1;


         
        }

        private void myp_PrintDocument()
        {
            DataGridView newdgv = new DataGridView();
            myp.CopyDataGridView(d1, newdgv, false);
            newdgv.GridColor = Color.Black;
            //打印处理过程
            myp.IsNeedCheckNewPage = false;
            System.Drawing.Printing.Margins mymargin;
            mymargin =new System.Drawing .Printing .Margins();
            mymargin .Left =Convert.ToInt32 (myp.ConvertInchToCm (45));
            mymargin.Right=Convert.ToInt32 (myp.ConvertInchToCm (45));
            mymargin .Top  =Convert.ToInt32 (myp.ConvertInchToCm (80));
            mymargin .Bottom=Convert.ToInt32 (myp.ConvertInchToCm (45));
            myp.NewPage(System.Drawing.Printing.PaperKind.A4, mymargin, false);
            mymargin.Top = 20;
            //先打印表头内容
            Font f1=new Font("黑体",18,FontStyle .Bold );
            Font f2 = new Font("黑体", 14, FontStyle.Bold);
            Font f3=new Font("宋体",12);
            Font f4 = new Font("华文行楷", 12);
            string ss;
            ss = "账单打印测试";
            myp.DrawTitle (ss,f1,Color.Black,0);
            ss = "Charge Bill";
            myp.DrawTitle(ss, f2, Color.Black, 0);

              myp.NewRow(60);
            float rowheight;
            rowheight=70;
            float mywidth;
            mywidth=myp.MeasureText ("卡内余额：",f3).Width ;
            ss="卡    号：";
            myp.DrawText(ss, mywidth, rowheight,f3, StringAlignment.Near, StringAlignment.Center, false, true, false, false, 0, 0);
            ss = textBox1.Text ;
            myp.DrawText(ss,myp.PaperPrintWidth - mywidth, rowheight,f4, StringAlignment.Near, StringAlignment.Center, false, true, false, false, 6, 0);

            myp.NewRow(rowheight);
            ss = "主 卡 号：";
            myp.DrawText(ss, mywidth, rowheight, f3, StringAlignment.Near, StringAlignment.Center, false, true, false, false, 0, 0);
            ss = textBox2.Text;
            myp.DrawText(ss, myp.PaperPrintWidth -mywidth, rowheight, f4, StringAlignment.Near, StringAlignment.Center, false, true, false, false, 6, 0);

            myp.NewRow(rowheight);
            ss = "宾客姓名：";
            myp.DrawText(ss, mywidth, rowheight, f3, StringAlignment.Near, StringAlignment.Center, false, true, false, false, 0, 0);
            ss = textBox3.Text;
            myp.DrawText(ss,myp.PaperPrintWidth -mywidth, rowheight, f4, StringAlignment.Near, StringAlignment.Center, false, true, false, false, 6, 0);

            myp.NewRow(rowheight);
            ss = "卡内余额：";
            myp.DrawText(ss, mywidth, rowheight, f3, StringAlignment.Near, StringAlignment.Center, false, true, false, false, 0, 0);
            ss = textBox4.Text;
            myp.DrawText(ss, myp.PaperPrintWidth - mywidth, rowheight, f4, StringAlignment.Near, StringAlignment.Center, false, true, false, false, 6, 0);

            myp.NewRow(rowheight);
            ss = "卡有效期：";
            myp.DrawText(ss, mywidth, rowheight, f3, StringAlignment.Near, StringAlignment.Center, false, true, false, false, 0, 0);
            ss = textBox5.Text;
            myp.DrawText(ss, myp.PaperPrintWidth - mywidth, rowheight, f4, StringAlignment.Near, StringAlignment.Center, false, true, false, false, 6, 0);


            myp.NewRow(rowheight);
            ss = "制表时间："+System.String .Format ("{0:f}",System.DateTime.Now);
            
           myp.DrawText(ss, myp.PaperPrintWidth , rowheight, f4, StringAlignment.Far, StringAlignment.Center, false, true, false, false, 6, 0);

            myp.NewRow(rowheight);
            //正式开始打印表格
            rowheight = 60;//表格要打印的行高
            bool needprintheader =true;
            for (int i = 0; i < d1.RowCount ; i++)
            {
                if (needprintheader ==true )
                {
                    //打印表格头
                    if (i!=0) 
                    {
                        //不是第一页，需要打印标题行                   
                          ss = "账单打印测试";
                myp.DrawTitle (ss,f1,Color.Black,0);
                ss = "Charge Bill" + "\n" + "\n";
                myp.DrawTitle(ss, f2, Color.Black, 0);
                myp.Currenty = myp.Currenty + 30;//留一点空白

                    }
                    //再打印表格列标题 行
                    myp.DrawDGVHeader(newdgv, rowheight, true, true, "1111", 6);
                    needprintheader = false;

                }

                if (myp.IsNewPage(rowheight) == true)
                {
                    //需要换页
                    myp.NewPage(System.Drawing.Printing.PaperKind.A4, mymargin, false);
                    needprintheader = true;
                    i = i - 1;

                }
                else
                {
                    //打印当前行
                    myp.DrawDGVRow(newdgv, i, rowheight, true, false, "1111", 6);
               
                }
            }


            //以下开始打印表尾
            float footerheight = 700;//表尾要打印的高度，下面打印的内容不能超过这个高度
            if (myp.IsNewPage(footerheight) == true)
            {
                //说明表尾要不能全部打印下去，先换页
                myp.NewPage(System.Drawing.Printing.PaperKind.A4, mymargin, false);
            }

            //然后定位表尾要打印的开始位置
            myp.Currenty = myp.PaperPrintHeight - footerheight;
            //画一横线
            myp.DrawLine(new Pen(Color.Black,3),new PointF(0,myp.Currenty ),new PointF(myp.PaperPrintWidth ,myp.Currenty ));
            float sumheight ;
            myp.Currenty = myp.Currenty + 30;
            sumheight = 30;
            ss = "请您核对本次充值及余额信息" + "\n" + "Check please";
            rowheight = myp.MeasureTextExHeight(ss, f3, myp.PaperPrintWidth,(float) -0.5,0);
            float tempy;
            tempy = myp.Currenty;
            myp.DrawTextEx(ss, myp.PaperPrintWidth, f3, Brushes.Black,(float) -0.5);
            myp.Currenty = tempy + rowheight;
            sumheight = sumheight + rowheight;

            myp.Currenty = myp.Currenty + 150;
            myp.Currentx = 0;
            sumheight = sumheight + 150;

            float tempwidth;
            float tempheight;
            ss = "签名/Signature：";
            tempwidth = myp.MeasureText(ss, f3).Width;
            tempheight=myp.MeasureText(ss, f3).Height;
            myp.DrawText(ss, tempwidth, f3,Color.Black , 0);
            float linewidth;
            linewidth = 300;
            myp.DrawLine(new Pen(Color.Black, 3), new PointF(myp.Currentx, myp.Currenty + tempheight), new PointF(myp.Currentx + linewidth, myp.Currenty + tempheight));

            ss = "日期/Date：";
            tempwidth = myp.MeasureText(ss, f3).Width;
            tempheight = myp.MeasureText(ss, f3).Height;
            myp.Currentx = myp.PaperPrintWidth - linewidth - tempwidth;
            myp.DrawText(ss, tempwidth, f3, Color.Black, 0);           
            myp.DrawLine(new Pen(Color.Black, 3), new PointF(myp.Currentx, myp.Currenty + tempheight), new PointF(myp.Currentx + linewidth, myp.Currenty + tempheight));



            myp.Currenty = myp.Currenty + 150;
            myp.Currentx = 0;
            sumheight = sumheight + 150;

            ss = "湖南省株洲市" + "\n" + " No.1558 adkfjkdfaldkfdfjdkfjdk " + "\n" + "电话/Tel：245141454545454" + "\n" +"电子邮箱/Email：adadkfadfadfadff " + "\n" + "网址/WebSite：adkfkdadfkadfjadkfakdfjadkf";

            myp.DrawText(ss, myp.PaperPrintWidth, footerheight - sumheight,f3, StringAlignment.Center, StringAlignment.Center, true, false, false, false,6, 0);

            newdgv.Dispose();
            newdgv = null;
        }

        private void myp_HeaderFooterOut(int pages, int curpage)
        {
            //页眉与页脚打印
            if (curpage == 1)
            {
                myp.PrintHeader(pictureBox1.Image, null, null);
            }
            myp.PrintFooter("", "第" +Convert.ToString ( curpage) + "页 共" +Convert.ToString ( pages) + "页","",0);
        }

        private void d1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
