搜索文章:

首页  |  Java技术  |  Asp.net  |  Asp编程  |  VC/C++  |  Delphi  |  VB编程

作者: unknow
        //Override the OnPrintPage to provide the printing logic for the document
        protected override void OnPrintPage(PrintPageEventArgs ev)
        {

            base.OnPrintPage(ev) ;
            int hh=this.currentLineNumber ;
            bool hasMorePage=false;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;

            Graphics g=ev.Graphics;
            float y=topMargin;
            float x=leftMargin;
            
            DataTable dt=(DataTable)printDataGrid.DataSource;
            //打印头标题一
            if(this.headtitle1!=null)
            {
                float headleft=(ev.MarginBounds.Width-g.MeasureString(this.headtitle1,new Font("宋体",16,FontStyle.Bold)).Width)/2+leftMargin;
                g.DrawString(this.headtitle1,new Font("宋体",16,FontStyle.Bold),Brushes.Black,headleft,ev.MarginBounds.Top);
            }
            //打印头标题二
            if(this.headtitle2!=null)
            {
                y+=new Font("宋体",16,FontStyle.Bold).GetHeight(ev.Graphics);
                float headleft=(ev.MarginBounds.Width-g.MeasureString(this.headtitle2,new Font("宋体",16)).Width)/2+leftMargin;
                g.DrawString(this.headtitle2,new Font("宋体",16,FontStyle.Bold),Brushes.Black,headleft,y);

            }
            //打印头标题三
            if(this.headtitle3!=null)
            {
                float headleft=ev.MarginBounds.Width-g.MeasureString(this.headtitle3,new Font("宋体",12)).Width+leftMargin;
                y+=printFont.GetHeight(ev.Graphics)*2;
                g.DrawString(this.headtitle3,new Font("宋体",12),Brushes.Black,headleft,y);
            }
            
            //打印头标题四一
            if(this.headtitle41!=null)
            {
                float headleft=leftMargin;
                y+=printFont.GetHeight(ev.Graphics)*2;
                g.DrawString(this.headtitle41,new Font("宋体",12),Brushes.Black,headleft,y);
            }
            //打印头标题四二
            if(this.headtitle42!=null)
            {
                float headleft=ev.MarginBounds.Width*1/3+leftMargin;
                g.DrawString(this.headtitle42,new Font("宋体",12),Brushes.Black,headleft,y);
            }
            //打印头标题四三
            if(this.headtitle43!=null)
            {
                float headleft=ev.MarginBounds.Width*2/3+leftMargin;
                g.DrawString(this.headtitle43,new Font("宋体",12),Brushes.Black,headleft,y);
            }
            

            //使用 MarginBounds 计算出每页打印的行数
            int tailnumber=0;
            //减去页尾一算一行
            if(this.tailtitle11!=null||this.tailtitle12 !=null||this.tailtitle13!=null)
                tailnumber+=1;
            //减去页尾二一行
            if(this.tailtitle2!=null)
                tailnumber+=1;
            //减去打印页码一行
            if(this.isPrintPageNumber)
                tailnumber+=1;
            //减去标题列算一行
            if(this.colname !=null)
                tailnumber+=1;
            this.numsPerPage = Convert.ToInt32((ev.MarginBounds.Height -y+ev.MarginBounds.Top) / (printFont.GetHeight(ev.Graphics)*(LineEmpty*2-1)+printFont.GetHeight(ev.Graphics))) ;
            this.numsPerPage -=tailnumber;
            //如果用户指定了每页打印的行数,就每页打印用户指定的行数
            if((this.LinePerPage>0) && (this.LinePerPage <this.numsPerPage))
                this.numsPerPage =this.LinePerPage ;
            //打印页码
            if(this.isPrintPageNumber)
            {
                int totalPageNumber=dt.Rows.Count/this.numsPerPage ;
                if(dt.Rows.Count%this.numsPerPage>0)
                    totalPageNumber+=1;
                string strPageNumber="当前第"+this.currentPageNumber + "页/共"+ totalPageNumber +"页";
                y+=printFont.GetHeight(ev.Graphics);
                float headleft=ev.MarginBounds.Width-g.MeasureString(strPageNumber,new Font("宋体",12)).Width+leftMargin;
                g.DrawString(strPageNumber,new Font("宋体",12),Brushes.Black,headleft,y);
            }
            //打印表格线
            y+=printFont.GetHeight(ev.Graphics)*2;
            if(this.isNeedHorLine)
            {
                g.DrawLine(new Pen(Color.Black,2),x,y,x+ev.MarginBounds.Width,y);
            }
            //打印正文
            printDataGridContext(ref ev,ref x,ref y,out hasMorePage);

            //打印尾标题一一
            if(this.tailtitle11!=null)
            {
                //y=ev.MarginBounds.Top*2;
                x=ev.MarginBounds.Left ;
                g.DrawString(this.tailtitle11.ToString(),new Font("宋体",12),Brushes.Black,x,y);
            }
            //打印尾标题一二
            if(this.tailtitle12!=null)
            {
                //y=ev.MarginBounds.Top*2;
                x=ev.MarginBounds.Left+ev.MarginBounds.Width*1/3 ;
                g.DrawString(this.tailtitle12.ToString(),new Font("宋体",12),Brushes.Black,x,y);
            }
            //打印尾标题一三
            if(this.tailtitle13!=null)
            {
                //y=ev.MarginBounds.Top*2;
                x=ev.MarginBounds.Left+ev.MarginBounds.Width*2/3 ;
                g.DrawString(this.tailtitle13.ToString(),new Font("宋体",12),Brushes.Black,x,y);
            }
            //打印尾标题二
            if(this.tailtitle2!=null)
            {
                y+=new Font("宋体",12).GetHeight(ev.Graphics);
                x=ev.MarginBounds.Width-g.MeasureString(this.tailtitle2,new Font("宋体",12)).Width+leftMargin;
                g.DrawString(this.tailtitle2.ToString(),new Font("宋体",12),Brushes.Black,x,y);
            }
            if(hasMorePage)
                ev.HasMorePages =true;
            else
            {
                this.currentLineNumber =0;
                //ev.HasMorePages =false;
            }

        }
        private float GetFontSize(PrintPageEventArgs ev,DataTable dataTable,out float[] colWidth)
        {
            //单位都使用缇
            //返回字型大小及宽度数组
            //宽度数组使用1到最后列
            int m, rownumber ;
            float allWidth, curWidth ;
            float paperWidth=ev.MarginBounds.Width;
            float[] strW;
            Graphics gp=ev.Graphics ;
        
            rownumber = dataTable.Columns.Count ;
            strW=new float[rownumber];
            //指定打印字体
            Font font=new Font("宋体",9);
            //初始化宽度数组
            for(m = 0;m<rownumber;m++)
            {
                try
                {
                    if(this.colname!=null)
                    {
                        strW[m]=gp.MeasureString(this.colname[m].ToString(),font).Width;
                    }
                    else
                    {
                        strW[m] = 0;
                    }
                }
                catch(Exception Err)
                {
                }
            }
        //使用遍历,获得每列的最大宽度
            for(int row=0;row<dataTable.Rows.Count;row++)
            {
                //curWidth = strW(n);
                for(int col=0;col<dataTable.Columns.Count;col++)
                {
                    curWidth = gp.MeasureString(dataTable.Rows[row][col].ToString().Trim(),font).Width;
                    if( strW[col] < curWidth)
                         strW[col] = curWidth;
                }
            }
            //获得宽度累计
            allWidth = 0;
            for(m =0;m<dataTable.Columns.Count;m++)
            {
                allWidth  += strW[m];
            }
            //决定比例
            float bili ;
            bili = paperWidth / allWidth;
            //按比例改变数组值
            for(m=0;m<dataTable.Columns.Count;m++)
            {
                strW[m]*= bili;
            }
            //每列宽度数组
            colWidth = strW;
            //返回字体大小
            return 9 * bili;

    }
        /// <summary>
        /// 打印DataGrid正文数据
        /// </summary>
        /// <param name="ev"></param>
        private void printDataGridContext(ref PrintPageEventArgs ev,ref float xPosition,ref float yPosition,out bool hasMorePage)
        {
            hasMorePage=false;
            float smallChange=0;
            string cellText="";
            float[] colWidth;

            Graphics g=ev.Graphics;
            //打印的开始y坐标
            float y=yPosition;
            //打印的开始x坐标
            float x=xPosition;
            //获得要打印的表
        
            //获得DataGrid每列的宽度及打印字体
            float fontSize=this.GetFontSize(ev,dt,out colWidth);
            Font font =new Font("宋体",fontSize);
            Pen pen=new Pen(Color.Black,1);
            y+=font.GetHeight(ev.Graphics)*(LineEmpty-1);
            //打印列中文标题数组
            if(this.colname !=null)
            {
                for(int j=0;j<this.colname.Length ;j++)
                {
                    cellText=this.colname[j].ToString();
                    switch(this.ColStyle)
                    {
                        case 0:
                            break;
                        case 1:
                            smallChange=(colWidth[j]-g.MeasureString(cellText,font).Width )/2;
                            break;
                        case 2:
                            smallChange=colWidth[j]-g.MeasureString(cellText,font).Width ;
                            break;
                    }
                    g.DrawString(cellText,font,Brushes.Black,x+smallChange,y);
                    x+=colWidth[j];
                }
                //打印一条横线
                y+=font.GetHeight(ev.Graphics)*LineEmpty;
                if(this.isNeedHorLine)
                {
                    g.DrawLine(pen,xPosition,y,xPosition+ev.MarginBounds.Width,y);
                }
//                //减去表格头算一行
//                this.numsPerPage -=1;    
            }
            //
            int temp=currentLineNumber;
            for(currentLineNumber+=1;currentLineNumber<=temp+this.numsPerPage;currentLineNumber++)
            {
                //如果到记录集的末尾则结束
                if(currentLineNumber>dt.Rows.Count)
                {
                    goto ExitEnd;
                }
                //打印一行数据
                x=xPosition;
                y+=font.GetHeight(ev.Graphics)*(LineEmpty-1);
                for(int j=0;j<dt.Columns.Count;j++)
                {
                    cellText=printDataGrid[currentLineNumber-1,j].ToString();
                    switch(this.ColStyle)
                    {
                        case 0:
                            break;
                        case 1:
                            smallChange=(colWidth[j]-g.MeasureString(cellText,font).Width )/2;
                            break;
                        case 2:
                            smallChange=colWidth[j]-g.MeasureString(cellText,font).Width ;
                            break;
                    }
                    g.DrawString(cellText,font,Brushes.Black,x+smallChange,y);
                    x+=colWidth[j];
                }
                //打印一条横线
                y+=font.GetHeight(ev.Graphics)*LineEmpty;
                if(currentLineNumber==temp+this.numsPerPage||currentLineNumber==dt.Rows.Count)
                {
                    if(this.isNeedHorLine )
                    {
                        g.DrawLine(new Pen(Color.Black,2),xPosition,y,xPosition+ev.MarginBounds.Width,y);
                    }
                }
                else
                {
                    if(this.isNeedHorLine)
                    {
                        g.DrawLine(pen,xPosition,y,xPosition+ev.MarginBounds.Width,y);
                    }
                }

            }
            currentLineNumber-=1;
    ExitEnd:
        //打印表格竖线
        x=xPosition;
        if(this.isNeedVertLine)
        {
            for(int i=0;i<colWidth.Length;i++)
            {
                if(i==0)
                {
                    g.DrawLine(new Pen(Color.Black,2),x,yPosition,x,y);
                }
                else
                {
                    g.DrawLine(pen,x,yPosition,x,y);
                }
                x+=colWidth[i];


            }
            g.DrawLine(new Pen(Color.Black,2),x,yPosition,x,y);
        }
            yPosition= y+printFont.GetHeight(ev.Graphics);
            //当前页数加一
            currentPageNumber+=1;
            if(dt.Rows.Count>currentLineNumber)
                hasMorePage =true;
            

        }
        
    }
}
相关文章:
© 2006   www.java-asp.net