locked
How to Calculate Second Page or more pages or Print Second page or more pages in MFC Print? RRS feed

  • Question

  • Hi All

    i want to print datas in Table format.so i searched Net and get the following site:

    http://www.ucancode.net/Visual-C-Print-Class-Library-tables.htm

    Source code for Table format Page. i used these code in my project and i have issue if i using those code. suppose i want to print 80 rows of records.then default its shows 50 records only and it wont goes to next page that is from 51 to 80 .i cant find out how to assign those datas in sencond page. and i dont know the technique how to set the pages if the data is more than one page how to calculate the required page no.

     

    if any body well in this context please download the link and use it how to calculate the subsequent pages depending on the datas or

    please give me a explanation how to print more than one page data print in mfc with example please....

    Thanks in advance for explanation.

    Thursday, September 16, 2010 7:25 AM

All replies

  • please give me a explanation how to print more than one page data print in mfc with example please....


    CView::OnPrint() expects you to render only the portion of the document that corresponds to the page currently being printed. Which page to print is specified by the CPrintInfo::m_nCurPage.

    In a complicated multipage printout the challenge is to know where page m_nCurPage begins. One strategy is to start every call to OnPrint() from the top of the printout but only render what is on m_nCurPage. The code below is intended as a minimal illustration of this strategy. The code shows what is added to the code created by Application Wizard.

    #define LEFT_MARGIN	30
    #define RIGHT_MARGIN	10
    #define TOP_MARGIN	33
    #define BOTTOM_MARGIN	40
    #define HEADER_MARGIN	10
    
    
    class CYourView : public CView
    {
    	UINT m_nCurPage;
    	CFont m_font;
    	virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);
    	virtual void OnPrepareDC(CDC* pDC, CPrintInfo* pInfo = NULL) {};
    	int TopMargin( CDC* pDC, CPrintInfo* pInfo );
    	void SetFont(CDC* pDC, int nPointSize);
    };
    
    int mmxToPixel( CDC* pDC, int mm )
    {
     	return MulDiv(pDC->GetDeviceCaps(HORZRES), mm, pDC->GetDeviceCaps(HORZSIZE));
    }
    
    int mmyToPixel( CDC* pDC, int mm )
    {
     	return MulDiv(pDC->GetDeviceCaps(VERTRES), mm, pDC->GetDeviceCaps(VERTSIZE));
    }
    
    
    void CYourView::SetFont(CDC* pDC, int nPointSize)
    {
    	pDC->SelectStockObject(DEVICE_DEFAULT_FONT);
    	m_font.DeleteObject();
    	m_font.CreatePointFont(nPointSize, _T("Arial"), pDC);
    	pDC->SelectObject( &m_font );
    }
    
    void CYourView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
    {
    	m_nCurPage = 0;
    	int y = -1;
    	CString cSize;
    	TEXTMETRIC tm;
    	for ( int nPointSize = 80; nPointSize < 600; nPointSize += 10 )
    	{
     		if ( y < 0 || y >= pDC->GetDeviceCaps(PHYSICALHEIGHT) - 
    				mmyToPixel( pDC, TOP_MARGIN + BOTTOM_MARGIN ) )
     		{  
    			y = TopMargin( pDC, pInfo );
    			if ( m_nCurPage > pInfo->m_nCurPage)
    				return;
    		}
    		cSize.Format(_T("Pointsize: %d"), nPointSize/10 );
    		SetFont( pDC, nPointSize);
    	 	if ( m_nCurPage == pInfo->m_nCurPage )
    	 		pDC->TextOut(0, y, cSize);
    		pDC->GetTextMetrics(&tm);
    		y += tm.tmHeight;
     	}
     	pInfo->SetMaxPage(m_nCurPage);
    }
    
    int CYourView::TopMargin( CDC* pDC, CPrintInfo* pInfo )
    {  
    	m_nCurPage++;
      	pDC->SetWindowOrg( mmxToPixel( pDC, -LEFT_MARGIN) + pDC->GetDeviceCaps(PHYSICALOFFSETX), 
    					- (mmyToPixel(pDC, HEADER_MARGIN) - pDC->GetDeviceCaps(PHYSICALOFFSETY)) );
      	if ( m_nCurPage == pInfo->m_nCurPage )
      	{
    		SetFont( pDC, 120);
     		CString cPageNo;
     		cPageNo.Format(_T("Page %d"), pInfo->m_nCurPage);
    		UINT nOldAlign = pDC->SetTextAlign( TA_RIGHT ); 
    		pDC->TextOut( pDC->GetDeviceCaps(PHYSICALWIDTH) - 
    			mmxToPixel(pDC, LEFT_MARGIN + RIGHT_MARGIN), 0, cPageNo );
    		pDC->SetTextAlign( nOldAlign ); 
    	}
    	pDC->SetWindowOrg( mmxToPixel(pDC, -LEFT_MARGIN) + pDC->GetDeviceCaps(PHYSICALOFFSETX), 
    					 -(mmyToPixel(pDC, TOP_MARGIN) - pDC->GetDeviceCaps(PHYSICALOFFSETY)) );
    	return 0;
    }
    
    Thursday, September 16, 2010 6:29 PM
  • Hi hgn

            Thanks for your great help i used your code i changed as my requirement and still now i have a problem to see second page i dont know why its not generated i tried to debug also its hard to find out why not show or generate here i attached my code so please go through my code and find out the error.thanks a lot

    my code snippet is:

     

    ///////////////////Balaji 17 09 10 start

    BOOL CSoftwareView::OnPreparePrinting(CPrintInfo* pInfo)
    {
        // default preparation
        return DoPreparePrinting(pInfo);
    }

    void CSoftwareView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
        // TODO: add extra initialization before printing
    }

    void CSoftwareView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
        // TODO: add cleanup after printing
    }

    int mmxToPixel( CDC* pDC, int mm )
    {
         return MulDiv(pDC->GetDeviceCaps(HORZRES), mm, pDC->GetDeviceCaps(HORZSIZE));
    }

    int mmyToPixel( CDC* pDC, int mm )
    {
         return MulDiv(pDC->GetDeviceCaps(VERTRES), mm, pDC->GetDeviceCaps(VERTSIZE));
    }


    void CSoftwareView::SetFont(CDC* pDC, int nPointSize)
    {
        pDC->SelectStockObject(DEVICE_DEFAULT_FONT);
        m_font.DeleteObject();
        m_font.CreatePointFont(nPointSize, _T("Arial"), pDC);
        pDC->SelectObject( &m_font );
    }

    void CSoftwareView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
    {
        m_nCurPage = 0;
        int nPointSize=100;
        int y = -1;
        int nNoofRecordsToPrint=10;
        CString cSize;
        TEXTMETRIC tm;
        REPORTINFO Report;
        CString scount,act,dev,comment;
        CString Sp0="     ";
        CString Sp1="                                                        ";
        CString Sp2="                                                        ";
        CString Sp3="                                 ";



        SetFont( pDC, nPointSize);

        ////////////// Balaji 18 09 10 Start

        pDC->Rectangle(50,50,2500,3250);

        pDC->MoveTo(50,120); pDC->LineTo(2500,120);
        pDC->TextOut(130,60,"Reference");
       
        pDC->MoveTo(400,50); pDC->LineTo(400,3250);
        pDC->TextOut(480,60,"Nominal");
       
        pDC->MoveTo(750,50); pDC->LineTo(750,3250);
        pDC->TextOut(830,60,"Actual");

        pDC->MoveTo(1100,50);pDC->LineTo(1100,3250);
        pDC->TextOut(1200,60,"Tol-");

        pDC->MoveTo(1450,50);pDC->LineTo(1450,3250);
        pDC->TextOut(1550,60,"Tol+");

        pDC->MoveTo(1800,50);pDC->LineTo(1800,3250);
        pDC->TextOut(1900,60,"Dev.");

        pDC->MoveTo(2150,50);pDC->LineTo(2150,3250);
        pDC->TextOut(2230,60,"Trend");

        ////////////// Balaji 18 09 10 End



        for ( int nCurrentRecord = 1; nCurrentRecord <=nNoofRecordsToPrint; nCurrentRecord++ )
        {
             if ( y < 0 || y >= pDC->GetDeviceCaps(PHYSICALHEIGHT) -
                    mmyToPixel( pDC, TOP_MARGIN + BOTTOM_MARGIN ) )
             { 
                y = TopMargin( pDC, pInfo );
                if ( m_nCurPage > pInfo->m_nCurPage)
                    return;
            }
            if(y<=2300)
            {
                   
             if ( m_nCurPage == pInfo->m_nCurPage )
            {
                readFromDisk (0,nCurrentRecord,&Report);

                for(int i=0;i<6;i++)
                {
                    if (i==0)
                    {
                        y+=5;
                        pDC->TextOut(-250, y, Report.fName[0]);
                        comment=Report.Comments;
                        comment=Sp0+comment+Sp1+Sp2+Sp3;
                                      
                        pDC->TextOut(40, y, comment);
                        y += tm.tmHeight;
                       
                    }
                     pDC->TextOut(-250, y, Report.sym[i]);
                    act.Format("%f",Report.act[i]);
                    dev.Format("%f",Report.dev[i]);
                    pDC->TextOut(800, y, act);
                    pDC->TextOut(1000, y, Report.nom[i]);
                    pDC->TextOut(1300, y, Report.uTol[i]);
                    pDC->TextOut(1500, y, Report.lTol[i]);
                    pDC->TextOut(1900, y, dev);
                    pDC->GetTextMetrics(&tm);
                    y += tm.tmHeight;
                   
                } //for i;
            }//if ( m_nCurPage == pInfo->m_nCurPage )
            y+=30;
            pDC->MoveTo(-300,y);
            pDC->LineTo(2146,y);
        }// if (y<=2300)
            else
            {
                y=0;
                m_nCurPage++;
                pInfo->SetMaxPage(m_nCurPage);

            }//if (y<=2300)
           
        }// for nCurrentRecord
       
        pInfo->SetMaxPage(m_nCurPage);
       
     
    }

    int CSoftwareView::TopMargin( CDC* pDC, CPrintInfo* pInfo )//print new page no at the (2078,0) location

        m_nCurPage++;
          pDC->SetWindowOrg( mmxToPixel( pDC, -LEFT_MARGIN) + pDC->GetDeviceCaps(PHYSICALOFFSETX),
                        - (mmyToPixel(pDC, HEADER_MARGIN) - pDC->GetDeviceCaps(PHYSICALOFFSETY)) );
          if ( m_nCurPage == pInfo->m_nCurPage )
          {
            SetFont( pDC, 100);
             CString cPageNo;
             cPageNo.Format(_T("Page %d"), pInfo->m_nCurPage);
            UINT nOldAlign = pDC->SetTextAlign( TA_RIGHT );
            pDC->TextOut( pDC->GetDeviceCaps(PHYSICALWIDTH) -
                mmxToPixel(pDC, LEFT_MARGIN + RIGHT_MARGIN), 0, cPageNo );
            pDC->SetTextAlign( nOldAlign );
        }
        pDC->SetWindowOrg( mmxToPixel(pDC, -LEFT_MARGIN) + pDC->GetDeviceCaps(PHYSICALOFFSETX),
                         -(mmyToPixel(pDC, TOP_MARGIN) - pDC->GetDeviceCaps(PHYSICALOFFSETY)) );
        return 0;
    }

     

    Here it print record upto 7th afterwards 8 to 10 its go to second page.i just give the default record size to print function it may change at run time.please clarify the error

    Thanks in advance for help

     

    Saturday, September 18, 2010 2:06 PM
  • All increments of y must be located outside the selection  “if ( m_nCurPage == pInfo->m_nCurPage )”. Otherwise page 2 will not start where page 1 ends. All actual rendering on the other hand, must take place inside the selection.
    Monday, September 20, 2010 8:42 AM
  • Sorry i cant get in please explore briefly where is the error please locate the line and give me a solution please...
    Monday, September 20, 2010 10:06 AM