110,538
社区成员
发帖
与我相关
我的任务
分享
private int currentPageIndex = 0;
private int rowCount=0;
private int pageCount=0;
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font fntTxt = new Font("宋体", 11, FontStyle.Regular); //正文文字
Brush brush = new SolidBrush(Color.Black);//画刷
Pen pen = new Pen(Color.Black); //线条颜色
pageCount = 3 //定义页数
if (currentPageIndex == 0) //当为第一页时
{
e.Graphics.DrawString("111111111111111111111111111", fntTxt, brush, new Point(0,0));
}
else if (currentPageIndex == 1) //当为第二页时
{
e.Graphics.DrawString("222222222222222222222222222", fntTxt, brush, new Point(0,0));
}
else if (currentPageIndex == 2) //当为第三页时
{
e.Graphics.DrawString("333333333333333333333333333", fntTxt, brush, new Point(0,0));
}
currentPageIndex++; //加新页
if (currentPageIndex < pageCount)
{
e.HasMorePages = true; //如果小于定义页 那么增加新的页数
}
else
{
e.HasMorePages = false; //停止增加新的页数
currentPageIndex = 0;
}
}
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int width=e.PageBounds.Width;
int height=e.PageBounds.Height;
if(this.isAutoPageRowCount)
pageRowCount=(int)((height-this.topMargin-titleSize-this.headerFont.Height-this.headerHeight-this.buttomMargin)/this.rowGap);
pageCount=(int)(rowCount/pageRowCount);
if(rowCount%pageRowCount>0)
pageCount++;
int xoffset=(int)((width-e.Graphics.MeasureString(this.title,this.titleFont).Width)/2);
int colCount = 0;
int x = 0;
int y =topMargin;
string cellValue = "";
int startRow=currentPageIndex*pageRowCount;
int endRow=startRow+this.pageRowCount<rowCount?startRow+pageRowCount:rowCount;
int currentPageRowCount=endRow-startRow;
if(this.currentPageIndex==0 || this.isEveryPagePrintTitle)
{
e.Graphics.DrawString(this.title,titleFont,brush,xoffset,y);
y+=titleSize;
}
colCount = dataGrid.TableStyles[0].GridColumnStyles.Count;
y += rowGap;
x = leftMargin;
DrawLine(new Point(x,y),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);//最左边的竖线
int lastIndex=-1;
int lastLength=0;
int indexj=-1;
for(int j = 0; j < colCount; j++)
{
int colWidth=dataGrid.TableStyles[0].GridColumnStyles[j].Width;
if( colWidth> 0)
{
indexj++;
if(this.header==null || this.header[indexj]=="")
cellValue = dataGrid.TableStyles[0].GridColumnStyles[j].HeaderText;
else
cellValue=header[indexj];
if(this.isCustomHeader)
{
if(this.upLineHeaderIndex[indexj]!=lastIndex)
{
if(lastLength>0 && lastIndex>-1)//开始画上一个upline
{
string upLineStr=this.uplineHeader[lastIndex];
int upXOffset=(int)((lastLength-e.Graphics.MeasureString(upLineStr,this.upLineFont).Width)/2);
if(upXOffset<0)
upXOffset=0;
e.Graphics.DrawString(upLineStr,this.upLineFont,brush,x-lastLength+upXOffset,y+(int)(this.cellTopMargin/2));
DrawLine(new Point(x-lastLength,y+(int)(this.headerHeight/2)),new Point(x,y+(int)(this.headerHeight/2)),e.Graphics);//中线
DrawLine(new Point(x,y),new Point(x,y+(int)(this.headerHeight/2)),e.Graphics);
}
lastIndex=this.upLineHeaderIndex[indexj];
lastLength=colWidth+colGap;
}
else
{
lastLength+=colWidth+colGap;
}
}
//int currentY=y+cellTopMargin;
int Xoffset=10;
int Yoffset=20;
int leftWordIndex=cellValue.IndexOf(this.splitChar);
if(this.upLineHeaderIndex!=null && this.upLineHeaderIndex[indexj]>-1)
{
if(leftWordIndex>0)
{
string leftWord=cellValue.Substring(0,leftWordIndex);
string rightWord=cellValue.Substring(leftWordIndex+1,cellValue.Length-leftWordIndex-1);
//上面的字
Xoffset=(int)(colWidth+colGap-e.Graphics.MeasureString(rightWord,this.upLineFont).Width);
Yoffset=(int)(this.headerHeight/2-e.Graphics.MeasureString("a",this.underLineFont).Height);
Xoffset=6;
Yoffset=10;
e.Graphics.DrawString(rightWord,this.underLineFont,brush,x+Xoffset-4,y+(int)(this.headerHeight/2));
e.Graphics.DrawString(leftWord,this.underLineFont,brush,x+2,y+(int)(this.headerHeight/2)+(int)(this.cellTopMargin/2)+Yoffset-2);
DrawLine(new Point(x,y+(int)(this.headerHeight/2)),new Point(x+colWidth+colGap,y+headerHeight),e.Graphics);
x += colWidth + colGap;
DrawLine(new Point(x,y+(int)(this.headerHeight/2)),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);
}
else
{
e.Graphics.DrawString(cellValue, headerFont, brush, x, y+(int)(this.headerHeight/2)+(int)(this.cellTopMargin/2));
x += colWidth + colGap;
DrawLine(new Point(x,y+(int)(this.headerHeight/2)),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);
}
}
else
{
if(leftWordIndex>0)
{
string leftWord=cellValue.Substring(0,leftWordIndex);
string rightWord=cellValue.Substring(leftWordIndex+1,cellValue.Length-leftWordIndex-1);
//上面的字
Xoffset=(int)(colWidth+colGap-e.Graphics.MeasureString(rightWord,this.upLineFont).Width);
Yoffset=(int)(this.headerHeight-e.Graphics.MeasureString("a",this.underLineFont).Height);
e.Graphics.DrawString(rightWord,this.headerFont,brush,x+Xoffset-4,y+2);
e.Graphics.DrawString(leftWord,this.headerFont,brush,x+2,y+Yoffset-4);
DrawLine(new Point(x,y),new Point(x+colWidth+colGap,y+headerHeight),e.Graphics);
x += colWidth + colGap;
DrawLine(new Point(x,y),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);
}
else
{
e.Graphics.DrawString(cellValue, headerFont, brush, x, y+cellTopMargin);
x += colWidth + colGap;
DrawLine(new Point(x,y),new Point(x,y+currentPageRowCount*rowGap+this.headerHeight),e.Graphics);
}
}
}
}
////循环结束,画最后一个的upLine
if(this.isCustomHeader)
{
if(lastLength>0 && lastIndex>-1)//开始画上一个upline
{
string upLineStr=this.uplineHeader[lastIndex];
int upXOffset=(int)((lastLength-e.Graphics.MeasureString(upLineStr,this.upLineFont).Width)/2);
if(upXOffset<0)
upXOffset=0;
e.Graphics.DrawString(upLineStr,this.upLineFont,brush,x-lastLength+upXOffset,y+(int)(this.cellTopMargin/2));
DrawLine(new Point(x-lastLength,y+(int)(this.headerHeight/2)),new Point(x,y+(int)(this.headerHeight/2)),e.Graphics);//中线
DrawLine(new Point(x,y),new Point(x,y+(int)(this.headerHeight/2)),e.Graphics);
}
}
int rightBound=x;
DrawLine(new Point(leftMargin,y),new Point(rightBound,y),e.Graphics); //最上面的线
//DrawLine(new Point(leftMargin,y+this.headerHeight),new Point(rightBound,y+this.headerHeight),e.Graphics);//列名的下面的线
y+=this.headerHeight;
//print all rows
for(int i = startRow; i < endRow; i++)
{
x = leftMargin;
for(int j = 0; j < colCount; j++)
{
if(dataGrid.TableStyles[0].GridColumnStyles[j].Width > 0)
{
cellValue = dataGrid[i,j].ToString();
if(cellValue=="False")
cellValue=falseStr;
if(cellValue=="True")
cellValue=trueStr;
e.Graphics.DrawString(cellValue, font, brush, x+this.cellLeftMargin, y+cellTopMargin);
x += dataGrid.TableStyles[0].GridColumnStyles[j].Width + colGap;
y = y + rowGap * (cellValue.Split(new char[] {'\r', '\n'}).Length - 1);
}
}
DrawLine(new Point(leftMargin,y),new Point(rightBound,y),e.Graphics);
y += rowGap;
}
DrawLine(new Point(leftMargin,y),new Point(rightBound,y),e.Graphics);
currentPageIndex++;
if(this.needPrintPageIndex)
e.Graphics.DrawString("共 "+pageCount.ToString()+" 页,当前第 "+this.currentPageIndex.ToString()+" 页",this.footerFont,brush,width-200,(int)(height-this.buttomMargin/2-this.footerFont.Height));
string s = cellValue;
string f3 = cellValue;
if(currentPageIndex<pageCount)
{
e.HasMorePages=true;
}
else
{
e.HasMorePages=false;
this.currentPageIndex=0;
}
//iPageNumber+=1;