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
- // Result of the Event 'PrintPage'
- private void prnDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
- {
- 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;
- if (!ReadInvoice)
- {
- ReadInvoiceData();
- SetInvoiceData(e.Graphics, e); // Draw Invoice Data
- }
- ReadInvoice = true;
- }
- private void DisplayInvoice()
- {
- prnPreview.Size = new System.Drawing.Size(700, 800);
- try
- {
- prnPreview.Document = prnDocument;
- prnPreview.ShowDialog();
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
- private void btnPreview_Click(object sender, System.EventArgs e)
- {
- ReadInvoice = false;
- DisplayInvoice(); // Print Preview
- }
- public frmOrder()
- {
- InitializeComponent();
- this.prnDialog = new System.Windows.Forms.PrintDialog();
- this.prnPreview = new System.Windows.Forms.PrintPreviewDialog();
- this.prnDocument = new System.Drawing.Printing.PrintDocument();
- // The Event of 'PrintPage'
- prnDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prnDocument_PrintPage);
- }
- private void SetInvoiceData(Graphics g, System.Drawing.Printing.PrintPageEventArgs e)
- {// Set Invoice Table:
- string FieldValue = "";
- int CurrentRecord = 0;
- int RecordsPerPage = 5; // twenty items in a page
- decimal Amount = 0;
- bool StopReading = false;
- // Set Table Head:
- int xProductID = leftMargin;
- CurrentY = CurrentY + InvoiceFontHeight-10;
- g.DrawString("Τιμολόγιο", InvoiceFontBold, BlackBrush, xProductID, CurrentY);
- int xProductName = xProductID+ (int)g.MeasureString("Invoice", InvoiceFont).Width + 40;
- g.DrawString("Ημέρες", InvoiceFontBold, BlackBrush, xProductName, CurrentY);
- int xUnitPrice = xProductName + (int)g.MeasureString("Days", InvoiceFont).Width + 10;
- g.DrawString("Σύνολο", InvoiceFontBold, BlackBrush, xUnitPrice, CurrentY);
- int xQuantity = xUnitPrice + (int)g.MeasureString("Total", InvoiceFont).Width + 30;
- g.DrawString("Αιτιολογία", InvoiceFontBold, BlackBrush, xQuantity, CurrentY);
- AmountPosition= xQuantity + (int)g.MeasureString("Σύνολο", InvoiceFont).Width + 170;
- // Set Invoice Table:
- CurrentY = CurrentY + InvoiceFontHeight + 8;
- while (CurrentRecord < RecordsPerPage)
- {
- FieldValue = rdr["InvoiceNo"].ToString();
- g.DrawString(FieldValue, InvoiceFont, BlackBrush, xProductID, CurrentY);
- FieldValue = rdr["Days Rented"].ToString();
- g.DrawString(FieldValue, InvoiceFont, BlackBrush, xProductName, CurrentY);
- FieldValue = String.Format("{0:0.00}", rdr["Period amount"]);
- g.DrawString(FieldValue, InvoiceFont, BlackBrush, xUnitPrice, CurrentY);
- FieldValue = rdr["Reason"].ToString();
- g.DrawString(FieldValue, InvoiceFont, BlackBrush, xQuantity, CurrentY);
- CurrentY = CurrentY + InvoiceFontHeight;
- if (!rdr.Read())
- {
- StopReading = true;
- break;
- }
- CurrentRecord++;
- }
- if (CurrentRecord < RecordsPerPage)
- { e.HasMorePages = false; }
- else
- { e.HasMorePages = true;}
- if (StopReading)
- { rdr .Close();
- con.Close();
- SetInvoiceTotal(g);
- }
- g.Dispose();
- }
Thank you,
John
Kumar BhimsenPosted Jul 19, 2018, 8:40 AM
Kumar BhimsenPosted Jul 29, 2016, 11:25 AM