首先,新建一个PrintDocumentSystem.Drawing.Printing.PrintDocument pd3 = new PrintDocument();
                pd3.PrintPage += new PrintPageEventHandler(pd3_PrintPage);
                PrintPreviewDialog printPreviewDialog3 = new PrintPreviewDialog();
                printPreviewDialog3.Document = pd3;
                printPreviewDialog3.ShowDialog();然后在这个pd3里绘图 private void pd3_PrintPage(object sender, PrintPageEventArgs e)
        {
    
                //我先设置要绘制文字的样式
                Font sampletitlefont = new Font("宋体", 12, FontStyle.Bold | FontStyle.Underline);
                
//第一次绘制文字位置50,450,显示正常
e.Graphics.DrawString("我要输出的文字_1", sampletitlefont, Brushes.Black, new Point(50, 450));//第二次绘制文字位置50, 1450,显然超过了PrintDocument一页的页面范围,所以没有显示     
e.Graphics.DrawString("要输出的文字_2", sampletitlefont, Brushes.Black, new Point(50, 1450));
         }我想要请教的是,如何进行PrintDocument第二页的绘制?起初我以为把文字坐标y的数字设的大点,便可以自动打印到第二页上,但是
事实证明,此方法无效...如果坐标设置超过此页范围,将不被显示.
那如何才能把图绘制到第二页上呢?第三页上呢。..?
ps:我曾经试过在pd3_PrintPage里设置e.HasMorePages = true;
但是还是不成功,这个操作会把第一页打印无数次...仍然没有我想要的第二页的内容.
还请高手指点,谢谢!                           

解决方案 »

  1.   

    参考下面代码:
    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; 
      

  2.   

    关于页数的问题
    我只看到最下面的代码
    if(currentPageIndex<pageCount) 
                    { 
                        e.HasMorePages=true; 
                    } 
                    else 
                    { 
                        e.HasMorePages=false; 
                        this.currentPageIndex=0;                 } 
    是和分页有关系的
    我试过e.HasMorePages=true; 
    但是还是不成功,这个操作会把第一页打印无数次...仍然没有我想要的第二页的内容. 
      

  3.   


    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;
        }
    }
      

  4.   

    在打印第一页的时候要判断是否存在第二页,如果存在则要添加如下代码:e.HasMorePages=true;
    还有就是要注意变量在打印的时候要初始化其值.