Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Windows Application That Showsn Data in 2 Pages in PrintPreview, but It Is Printing Blank Pages When I Click The Print Icon Button In PrintPreview ?

Below Is My Code :
C#
// Print the document's pages.
        private int NextPageNum = 0;

// Result of the Event 'PrintPage'
 private void prnDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
               
            float pageWidth = e.PageSettings.PrintableArea.Width;
            float pageHeight = e.PageSettings.PrintableArea.Height;

            leftMargin = (int)e.MarginBounds.Left;
            rightMargin = (int)e.MarginBounds.Right;
            topMargin = (int)e.MarginBounds.Top;
            bottomMargin = (int)e.MarginBounds.Bottom;
            InvoiceWidth = (int)e.MarginBounds.Width;
            InvoiceHeight = (int)e.MarginBounds.Height;

            switch (NextPageNum)
            {
                case 0:
                    RectangleF rectF1 = new RectangleF(90, 45, pageWidth-85, pageHeight-35);
                    Pen rectPen = new Pen(System.Drawing.Color.Black, 1);
                    e.Graphics.DrawRectangle(rectPen, Rectangle.Round(rectF1));

                    RectangleF rectF2 = new RectangleF(95, 230, pageWidth - 95, pageHeight - 225);
                    //Pen rectPen = new Pen(System.Drawing.Color.Black, 2);
                    e.Graphics.DrawRectangle(rectPen, Rectangle.Round(rectF2));

                    SetInvoiceHead(e.Graphics); // Draw Invoice Head
                    SetOrderData(e.Graphics); // Draw Order Data
                    SetInvoiceData(e.Graphics, e); // Draw Invoice Data
                    break;
                case 1:
                    ImageShower(e.Graphics);
                    break;
            }
            // Next time print the next page.
            NextPageNum += 1;

            // We have more pages if we have not yet printed page 2.
            e.HasMorePages = (NextPageNum <2);
      }

C#
private void prnDocument_BeginPrint(object sender, PrintEventArgs e)
        {
            // Start with page 0.
            NextPageNum = 0;
        }

C#
private void btnPreview_Click(object sender, EventArgs e)
        {
           
            NextPageNum = 0;
            retriveData();
            foreBrush = FontforeColor();
            prnPreview.Document = prnDocument;
            ((Form)prnPreview).WindowState = FormWindowState.Maximized;
            // To Display 100% Preview
            prnPreview.PrintPreviewControl.StartPage = 0;
            prnPreview.PrintPreviewControl.Zoom = 1.0;
            prnPreview.PrintPreviewControl.Columns = 1;
            prnPreview.ShowDialog();
       }

C#
private void btnPrint_Click(object sender, EventArgs e)
       {
          
           try
           {
               prnDocument.Print();
           }
           catch (Exception e)
           {
               MessageBox.Show(e.ToString());
           }
       }
Posted
Updated 16-Mar-15 3:17am
v4
Comments
Sinisa Hajnal 16-Mar-15 7:15am    
Please show some code, you're maybe doing something wrong, but we cannot help without seeing what you're doing. Thank you.
CHAITANYA KIRAN KASANI 16-Mar-15 9:22am    
hi Sinisa, i had updated my code now, please help me..
Sinisa Hajnal 16-Mar-15 10:46am    
Did you check that each method is called in order? Does prnDocument_PrintPage gets called? What happens if you comment out all that rectangles and just put some fixed text? Check that your SetInvoiceXXX methods work by commenting two and leaving one to draw...in general debug the program. What you've shown looks fine.
CHAITANYA KIRAN KASANI 24-Mar-15 3:27am    
i had found it...i had mentioned as below in the prnDocument_PrintPage, then it is printing data as in PrintPreview..
// If we have no more pages, reset for the next time we print.
if (NextPageNum > 1) NextPageNum = 0;

1 solution

SQL
i had found it...i had mentioned as below at the end of prnDocument_PrintPage, then it is printing data as in PrintPreview..
// If we have no more pages, reset for the next time we print.
if (NextPageNum > 1) NextPageNum = 0;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900