I cannot figure out how to get this code to print multiple pages. Is this the correct way to print in C#? Is there a better way to print? I have no problems with the style of code when printing one pages reports.
My Code is below.
public void invoicePostingReportPrintReportButton_Click(object sender, EventArgs e)
{
if (invoicemainresults == null)
{
MessageBox.Show("You must create a report before you can print it");
invoicePostingReportStartDateTimePicker.Focus();
return;
}
else
{
foreach (var result in invoicemainresults)
{
_sProNumber += result.ProNumber + Environment.NewLine + Environment.NewLine;
_sLoadNumber += result.LoadNumber + Environment.NewLine + Environment.NewLine;
_sShipmentNumber += result.ShipmentNumber + Environment.NewLine + Environment.NewLine;
_sBillDate += result.BillDate.ToShortDateString() + Environment.NewLine + Environment.NewLine;
_sInvoiceCreatedDate += result.InvoiceCreatedDate.ToShortDateString() + Environment.NewLine + Environment.NewLine;
_sInvoicePostedDate += result.InvoicePostedDate.ToShortDateString() + Environment.NewLine + Environment.NewLine;
_sTotalCharges += result.TotalCharges + Environment.NewLine + Environment.NewLine;
_sTotalPayments += result.TotalPayments + Environment.NewLine + Environment.NewLine;
_sBalanceDue += result.BalanceDue + Environment.NewLine + Environment.NewLine;
}
}
var getPrinters = GlobalConfig.PersonnelTable.GetByPersonCodeForPrinting(_userId);
if (getPrinters != null)
{
if (getPrinters.PrinterNameReports != null)
{
printTo = getPrinters.PrinterPathReports;
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = printDocument1;
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
printDocument1.PrinterSettings.PrinterName = printTo;
printDocument1.Print();
}
}
this._sProNumber = string.Empty;
this._sLoadNumber = string.Empty;
this._sShipmentNumber = string.Empty;
this._sBillDate = string.Empty;
this._sInvoiceCreatedDate = string.Empty;
this._sInvoicePostedDate = string.Empty;
this._sTotalCharges = string.Empty;
this._sTotalPayments = string.Empty;
this._sBalanceDue = string.Empty;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (createdRadioButton.Checked)
{
e.Graphics.DrawString("INVOICE CREATED DATE REPORT", new Font("Times New Roman", 24, FontStyle.Regular), Brushes.Black, new PointF(142, 10));
}
else if (postedRadioButton.Checked)
{
e.Graphics.DrawString("INVOICE POSTED DATE REPORT", new Font("Times New Roman", 24, FontStyle.Regular), Brushes.Black, new PointF(147, 10));
}
Pen ControlTextPen = new Pen(System.Drawing.Color.Black, 3);
PointF point1 = new PointF(10.0F, 50.0F);
PointF point2 = new PointF(840.0F, 50.0F);
e.Graphics.DrawLine(ControlTextPen, point1, point2);
using (Font TNR10 = new Font("Times New Roman", 10, FontStyle.Regular))
{
PointF pointF1 = new PointF(10, 70);
e.Graphics.DrawString("PRO #", TNR10, Brushes.Black, pointF1);
PointF pointF2 = new PointF(100, 70);
e.Graphics.DrawString("LOAD #", TNR10, Brushes.Black, pointF2);
PointF pointF3 = new PointF(190, 70);
e.Graphics.DrawString("SHIPMENT #", TNR10, Brushes.Black, pointF3);
PointF pointF4 = new PointF(280, 70);
e.Graphics.DrawString("BILLED", TNR10, Brushes.Black, pointF4);
PointF pointF5 = new PointF(370, 70);
e.Graphics.DrawString("CREATED", TNR10, Brushes.Black, pointF5);
PointF pointF6 = new PointF(460, 70);
e.Graphics.DrawString("POSTED", TNR10, Brushes.Black, pointF6);
PointF pointF7 = new PointF(550, 70);
e.Graphics.DrawString("CHARGES", TNR10, Brushes.Black, pointF7);
PointF pointF8 = new PointF(640, 70);
e.Graphics.DrawString("PAYMENTS", TNR10, Brushes.Black, pointF8);
PointF pointF9 = new PointF(730, 70);
e.Graphics.DrawString("DUE", TNR10, Brushes.Black, pointF9);
PointF pointF10 = new PointF(10, 100);
e.Graphics.DrawString(_sProNumber, TNR10, Brushes.Black, pointF10);
PointF pointF11 = new PointF(100, 100);
e.Graphics.DrawString(_sLoadNumber, TNR10, Brushes.Black, pointF11);
PointF pointF12 = new PointF(190, 100);
e.Graphics.DrawString(_sShipmentNumber, TNR10, Brushes.Black, pointF12);
PointF pointF13 = new PointF(280, 100);
e.Graphics.DrawString(_sBillDate, TNR10, Brushes.Black, pointF13);
PointF pointF14 = new PointF(370, 100);
e.Graphics.DrawString(_sInvoiceCreatedDate, TNR10, Brushes.Black, pointF14);
PointF pointF15 = new PointF(460, 100);
e.Graphics.DrawString(_sInvoicePostedDate, TNR10, Brushes.Black, pointF15);
PointF pointF16 = new PointF(550, 100);
e.Graphics.DrawString(_sTotalCharges, TNR10, Brushes.Black, pointF16);
PointF pointF17 = new PointF(640, 100);
e.Graphics.DrawString(_sTotalPayments, TNR10, Brushes.Black, pointF17);
PointF pointF18 = new PointF(730, 100);
e.Graphics.DrawString(_sBalanceDue, TNR10, Brushes.Black, pointF18);
}
}
What I have tried:
I have tried defining the page size and font size with HasMorePages.