Print Blank Page while Print Preview shows the page correct

Jul 18 2016 6:00 PM

 Hi,
I am trying to print data from the printer button in the top left of Print Preview window from c#, but it gives blank page.

Below is the code  
  1. // Result of the Event 'PrintPage'  
  2.         private void prnDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)  
  3.         {  
  4.         leftMargin = (int)e.MarginBounds.Left;  
  5.         rightMargin = (int)e.MarginBounds.Right;  
  6.         topMargin = (int)e.MarginBounds.Top;  
  7.         bottomMargin = (int)e.MarginBounds.Bottom;  
  8.         InvoiceWidth = (int)e.MarginBounds.Width;  
  9.         InvoiceHeight = (int)e.MarginBounds.Height;  
  10.   
  11.             if (!ReadInvoice)  
  12.             {  
  13.                 ReadInvoiceData();  
  14.   
  15.               
  16.                 SetInvoiceData(e.Graphics, e); // Draw Invoice Data  
  17.             }  
  18.                
  19.             ReadInvoice = true;  
  20.               
  21.         }  
  1. private void DisplayInvoice()  
  2.     {  
  3.             prnPreview.Size = new System.Drawing.Size(700, 800);  
  4.             try  
  5.             {  
  6.                 prnPreview.Document = prnDocument;  
  7.                prnPreview.ShowDialog();  
  8.                   
  9.             }  
  10.             catch (Exception e)  
  11.             {  
  12.                 MessageBox.Show(e.ToString());  
  13.             }  
  14.         }  
  15.   
  16. private void btnPreview_Click(object sender, System.EventArgs e)  
  17.     {  
  18.             ReadInvoice = false;  
  19.             DisplayInvoice(); // Print Preview  
  20.          }  
  21.   
  22. public frmOrder()  
  23.     {  
  24.         InitializeComponent();  
  25.         this.prnDialog = new System.Windows.Forms.PrintDialog();  
  26.         this.prnPreview = new System.Windows.Forms.PrintPreviewDialog();  
  27.         this.prnDocument = new System.Drawing.Printing.PrintDocument();  
  28.         // The Event of 'PrintPage'  
  29.         prnDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prnDocument_PrintPage);  
  30.               
  31.     }  
 
  1. private void SetInvoiceData(Graphics g, System.Drawing.Printing.PrintPageEventArgs e)  
  2.         {// Set Invoice Table:  
  3.             string FieldValue = "";  
  4.             int CurrentRecord = 0;  
  5.             int RecordsPerPage = 5; // twenty items in a page  
  6.             decimal Amount = 0;  
  7.             bool StopReading = false;  
  8.   
  9.             // Set Table Head:  
  10.             int xProductID = leftMargin;  
  11.             CurrentY = CurrentY + InvoiceFontHeight-10;  
  12.             g.DrawString("Τιμολόγιο", InvoiceFontBold, BlackBrush, xProductID, CurrentY);  
  13.   
  14.             int xProductName = xProductID+ (int)g.MeasureString("Invoice", InvoiceFont).Width + 40;  
  15.             g.DrawString("Ημέρες", InvoiceFontBold, BlackBrush, xProductName, CurrentY);  
  16.   
  17.             int xUnitPrice = xProductName  + (int)g.MeasureString("Days", InvoiceFont).Width + 10;  
  18.             g.DrawString("Σύνολο", InvoiceFontBold, BlackBrush, xUnitPrice, CurrentY);  
  19.   
  20.             int xQuantity = xUnitPrice  + (int)g.MeasureString("Total", InvoiceFont).Width + 30;  
  21.             g.DrawString("Αιτιολογία", InvoiceFontBold, BlackBrush, xQuantity, CurrentY);  
  22.             AmountPosition= xQuantity + (int)g.MeasureString("Σύνολο", InvoiceFont).Width + 170;  
  23.             // Set Invoice Table:  
  24.             CurrentY = CurrentY + InvoiceFontHeight + 8;  
  25.                while (CurrentRecord < RecordsPerPage)  
  26.                     {  
  27.                         FieldValue = rdr["InvoiceNo"].ToString();  
  28.                         g.DrawString(FieldValue, InvoiceFont, BlackBrush, xProductID, CurrentY);  
  29.                         FieldValue = rdr["Days Rented"].ToString();  
  30.                         g.DrawString(FieldValue, InvoiceFont, BlackBrush, xProductName, CurrentY);  
  31.                         FieldValue = String.Format("{0:0.00}", rdr["Period amount"]);  
  32.                         g.DrawString(FieldValue, InvoiceFont, BlackBrush, xUnitPrice, CurrentY);  
  33.                         FieldValue = rdr["Reason"].ToString();  
  34.                         g.DrawString(FieldValue, InvoiceFont, BlackBrush, xQuantity, CurrentY);  
  35.                         CurrentY = CurrentY + InvoiceFontHeight;  
  36.   
  37.                         if (!rdr.Read())  
  38.                         {  
  39.                             StopReading = true;  
  40.                             break;  
  41.                         }  
  42.   
  43.                         CurrentRecord++;  
  44.                     }  
  45.            if (CurrentRecord < RecordsPerPage)  
  46.             { e.HasMorePages = false; }  
  47.             else  
  48.             { e.HasMorePages = true;}  
  49.   
  50.   
  51.             if (StopReading)  
  52.             {   rdr .Close();  
  53.                 con.Close();  
  54.                 SetInvoiceTotal(g);  
  55.             }  
  56.              
  57.             g.Dispose();  
  58.         }  


 Thank you,
John 



Answers (2)