Click here to Skip to main content
16,015,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
        }
    }

    // Get list of printers from Personnel table
    var getPrinters = GlobalConfig.PersonnelTable.GetByPersonCodeForPrinting(_userId);

    if (getPrinters != null)
    {
        // Verify Reports printer name is not null
        if (getPrinters.PrinterNameReports != null)
        {
            printTo = getPrinters.PrinterPathReports;

            //Create a PrintDocument object
            PrintDialog printDialog1 = new PrintDialog();

            //Add print dialog to document
            printDialog1.Document = printDocument1;

            //Add print-page event handler
            printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);

            //Print to selected printer
            printDocument1.PrinterSettings.PrinterName = printTo;

            //Print the document
            printDocument1.Print();

        }
    }

    // Clear the variables
    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)
{
    // Add title to top of printed page for report chosen 
    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));
    }

    // Line to seperate report name and report data
    // Create pen.
    Pen ControlTextPen = new Pen(System.Drawing.Color.Black, 3);

    // Create points that define line.
    PointF point1 = new PointF(10.0F, 50.0F);
    PointF point2 = new PointF(840.0F, 50.0F);

    // Draw line to screen.
    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.
Posted

Setting the HasMorePages property of e to true should be enough to trigger a new page. The printDocument1_PrintPage will be called again for the next page.

Every call to printDocument1_PrintPage will produce a single page. You will need to make sure you limit where you draw to that page.

Just remember to leave e.HasMorePages as false when you are finished.
 
Share this answer
 
Comments
MIDCOPC 8-Nov-23 7:56am    
RainHat, thank you for the reply. I got it working with the code below. Not sure if this is the correct way but it is working.

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;
}

// Get list of printers from Personnel table
var getPrinters = GlobalConfig.PersonnelTable.GetByPersonCodeForPrinting(_userId);

if (getPrinters != null && getPrinters.PrinterNameReports != null)
{
printTo = getPrinters.PrinterPathReports;

// Create a PrintDocument object
PrintDocument printDocument1 = new PrintDocument();

// Set up print settings
printDocument1.PrinterSettings.PrinterName = printTo;
printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Letter", 850, 1100);

// Set up event handler for printing
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);

// Calculate the number of items per page
int itemsPerPage = 30; // Adjust this value as needed

// Calculate the total number of pages
int totalPages = (int)Math.Ceiling((double)invoicemainresults.Count / itemsPerPage);

// Print each page
for (int currentPage = 1; currentPage <= totalPages; currentPage++)
{
// Set the starting and ending index for the current page
int startIndex = (currentPage - 1) * itemsPerPage;
int endIndex = Math.Min(startIndex + itemsPerPage - 1, invoicemainresults.Count - 1);

// Clear the variables
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;

// Populate the variables with data for the current page
for (int i = startIndex; i <= endIndex; i++)
{
var result = invoicemainresults[i];
_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;
}

// Print the current page
printDocument1.Print();
}
}
}
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;
    }

    // Get list of printers from Personnel table
    var getPrinters = GlobalConfig.PersonnelTable.GetByPersonCodeForPrinting(_userId);

    if (getPrinters != null && getPrinters.PrinterNameReports != null)
    {
        printTo = getPrinters.PrinterPathReports;

        // Create a PrintDocument object
        PrintDocument printDocument1 = new PrintDocument();

        // Set up print settings
        printDocument1.PrinterSettings.PrinterName = printTo;
        printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Letter", 850, 1100);

        // Set up event handler for printing
        printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);

        // Calculate the number of items per page
        int itemsPerPage = 30; // Adjust this value as needed

        // Calculate the total number of pages
        int totalPages = (int)Math.Ceiling((double)invoicemainresults.Count / itemsPerPage);

        // Print each page
        for (int currentPage = 1; currentPage <= totalPages; currentPage++)
        {
            // Set the starting and ending index for the current page
            int startIndex = (currentPage - 1) * itemsPerPage;
            int endIndex = Math.Min(startIndex + itemsPerPage - 1, invoicemainresults.Count - 1);

            // Clear the variables
            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;

            // Populate the variables with data for the current page
            for (int i = startIndex; i <= endIndex; i++)
            {
                var result = invoicemainresults[i];
                _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;
            }

            // Print the current page
            printDocument1.Print();
        }
    }
}
 
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