0

I tried following the codes based on Printing multiple document in c# window application

When the Items was few for printing it does print on a thermal receipt printer. But whenever the item display for printing is more than 1 it cost an infinite loop.

Here's my code

private void PrintReceipt()
{
     //Assuming my Selector is 0
     selector = 0;   
     printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
     printDocument1.Print();
}

Event Code

void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        frmLogin fl = new frmLogin();
        Graphics graphic = e.Graphics;
        Font font = new Font("FontA11", 9.0f);

        float fontHeight = font.GetHeight();
        float pageWidth = e.PageSettings.PrintableArea.Width;
        float pageHeight = e.PageSettings.PrintableArea.Height;
        int startX = 52;
        int startY = 10;
        int offset = 40;

        #region Header
        graphic.DrawString(compName, new Font("FontA11", 9.0f, FontStyle.Bold), Brushes.Black, startX, startY);
        graphic.DrawString(address1, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 5, 30);
        //graphic.DrawString(address2, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 24, 45);
        //graphic.DrawString(contact, new Font("Tahoma", 11), new SolidBrush(Color.Black), 53, 60);
        //graphic.DrawString("Owned & Operated By: " + store_op, new Font("Tahoma", 11), new SolidBrush(Color.Black), 5, 75);
        //graphic.DrawString("Permit No: " + permit_no, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 63, 60);
        graphic.DrawString("TIN: " + TIN + "" + TaxT, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 63, 45);
        //graphic.DrawString("Accreditation No: " + bir.AccreditationNo(), new Font("FontA11", 7.8f), new SolidBrush(Color.Black), 4, 92);
        //graphic.DrawString("Serial No: " + bir.SerialNo(), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 81, 105);
        graphic.DrawString("Machine Code: " + machine_no, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 63, 60);
        graphic.DrawString("-----------------------------------------------------------------------------", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 3, 75);
        graphic.DrawString(DateTime.Now.ToString("MMM dd, yyyy") + " " + "(" + DateTime.Now.ToString("ddd") + ")", font, new SolidBrush(Color.Black), 5, 90);
        graphic.DrawString(DateTime.Now.ToString("hh:mm tt"), font, new SolidBrush(Color.Black), 203, 90);
        graphic.DrawString("SI# " + OrNo, font, new SolidBrush(Color.Black), 5, 105);
        #endregion

        using (MySqlConnection con = new MySqlConnection(dbcon.getConnectionString()))
        {
            String query = "SELECT a, b, c, d, e, f FROM (SELECT pos_park.trn AS aaa, inventory_stocks.stock_name AS a, pos_park.pos_amt AS b, pos_park.pos_quantity AS c, pos_park.pos_item_amount AS d, inventory_items.item_whole_price AS e, inventory_items.item_tax_type AS f FROM inventory_items ";
            query += "INNER JOIN inventory_stocks ON inventory_items.stock_code = inventory_stocks.stock_code ";
            query += "INNER JOIN pos_park ON inventory_items.item_ean = pos_park.pos_ean ";
            query += "WHERE (pos_park.pos_orno = ?pos_orno) AND (pos_park.pos_terminal = ?pos_terminal) ";
            query += "UNION ALL ";
            query += "SELECT pos_park.trn AS aaa, inventory_items.kit_name AS a, pos_park.pos_amt AS b, pos_park.pos_quantity AS c, pos_park.pos_item_amount AS d, inventory_items.item_whole_price AS e, inventory_items.item_tax_type  AS f FROM inventory_items ";
            query += "INNER JOIN pos_park ON inventory_items.item_ean = pos_park.pos_ean ";
            query += "WHERE (pos_park.pos_orno = ?pos_orno) AND (pos_park.pos_terminal = ?pos_terminal) AND (inventory_items.is_kit = 1)) receipt ";
            query += "ORDER BY aaa";
            using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, con))
            {
                try
                {
                    DataTable dataTable = new DataTable();
                    adapter.SelectCommand.Parameters.AddWithValue("?pos_orno", OrNo);
                    adapter.SelectCommand.Parameters.AddWithValue("?pos_terminal", fl.tN);
                    adapter.Fill(dataTable);
                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        String description = Truncate(dataTable.Rows[i][0].ToString(), 27);
                        Double priceR = Convert.ToDouble(dataTable.Rows[i][3]);
                        graphic.DrawString(description + "  " + "\n" + dataTable.Rows[i][2].ToString() + " @ " + priceR.ToString("#,###,##0.00"), font, new SolidBrush(Color.Black), 10, 115 + offset);
                        graphic.DrawString(Convert.ToDouble(dataTable.Rows[i][1]).ToString("#,###,##0.00").PadLeft(10) + dataTable.Rows[i][5].ToString(), font, new SolidBrush(Color.Black), 205, 115 + offset);
                        offset = offset + (int)fontHeight + 25;

                        if (offset >= pageHeight)
                        {
                            e.HasMorePages = true;
                            offset = 0;
                            return; // The Culprit is here and it does some endless loop
                        }
                        else
                        {
                            e.HasMorePages = false;
                        }
                    }
                    offset = offset + 30;
                    graphic.DrawString("-----------------------------------------------------------------------------", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 3, 100 + offset);
                    //
                    foreach (ListViewItem lv in lviewPOS.Items)
                    {
                        counted += Int32.Parse(lv.SubItems[1].Text);
                    }
                    graphic.DrawString("Item Count: ", new Font("FontA11", 13.0f), new SolidBrush(Color.Black), 40, 115 + offset);
                    graphic.DrawString(counted.ToString(), new Font("FontA11", 13.0f), new SolidBrush(Color.Black), 160, 115 + offset);
                    //
                    graphic.DrawString("VATable ", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 105, 145 + offset);
                    graphic.DrawString(lblVatable.Text.PadLeft(10), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 215, 145 + offset);
                    graphic.DrawString("VAT-Exempt ", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 105, 160 + offset);
                    graphic.DrawString(lblVATe.Text.PadLeft(10), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 215, 160 + offset);
                    graphic.DrawString("Zero-Rated ", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 105, 175 + offset);
                    graphic.DrawString(lblVATz.Text.PadLeft(10), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 215, 175 + offset);
                    graphic.DrawString("VAT (" + lblTax.Text + ")", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 105, 190 + offset);
                    graphic.DrawString(lblTAXamt.Text.PadLeft(10), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 215, 190 + offset);
                    graphic.DrawString("TOTAL ", new Font("FontA11", 12.0f), new SolidBrush(Color.Black), 103, 205 + offset);
                    graphic.DrawString(getDTotalAmt.ToString("#,###,##0.00").PadLeft(10), new Font("FontA11", 12.0f), new SolidBrush(Color.Black), 203, 205 + offset);
                    if (selector == 0)
                    {
                        graphic.DrawString("Amount Tender  ", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 235 + offset);
                        graphic.DrawString(Tender.ToString("#,###,##0.00").PadLeft(10), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 115, 235 + offset);
                        graphic.DrawString("Change ", new Font("FontA11", 12.0f), new SolidBrush(Color.Black), 8, 250 + offset);
                        graphic.DrawString(Change.ToString("#,###,##0.00").PadLeft(10), new Font("FontA11", 12.0f), new SolidBrush(Color.Black), 98, 250 + offset);
                        //
                        graphic.DrawString("Transaction #:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 280 + offset);
                        graphic.DrawString(OrNo.ToString(), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 110, 280 + offset);
                        graphic.DrawString("CASHIER:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 295 + offset);
                        graphic.DrawString(lblUserAccount.Text, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 110, 295 + offset);
                        graphic.DrawString("Terminal #:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 310 + offset);
                        graphic.DrawString(fl.tN, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 110, 310 + offset);
                        graphic.DrawString("**************************************************************************", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 3, 325 + offset);
                        graphic.DrawString("THIS IS NOT YOUR OFFICIAL RECEIPT", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 30, 340 + offset);
                        graphic.DrawString("Thank you and Come Again ", new Font("FontA11", 8.0f), new SolidBrush(Color.Black), 70, 355 + offset);
                    }
                    if (selector == 1)
                    {
                        graphic.DrawString("Card No:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 235 + offset);
                        graphic.DrawString("XXXX-XXXX-XXXX-" + cardNo, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 85, 235 + offset);
                        graphic.DrawString("Card Type:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 250 + offset);
                        graphic.DrawString(cardType, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 85, 250 + offset);
                        graphic.DrawString("Amount:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 265 + offset);
                        graphic.DrawString(cardAmount.ToString("#,###,##0.00"), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 85, 265 + offset);

                        graphic.DrawString("Transaction #:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 295 + offset);
                        graphic.DrawString(OrNo.ToString(), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 110, 295 + offset);
                        graphic.DrawString("CASHIER:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 310 + offset);
                        graphic.DrawString(lblUserAccount.Text, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 110, 310 + offset);
                        graphic.DrawString("Terminal #:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 325 + offset);
                        graphic.DrawString(fl.tN, new Font("Tahoma", 10), new SolidBrush(Color.Black), 110, 325 + offset);
                        graphic.DrawString("**************************************************************************", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 3, 340 + offset);
                        graphic.DrawString("THIS IS NOT YOUR OFFICIAL RECEIPT", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 30, 355 + offset);
                        graphic.DrawString("Thank you and Come Again ", new Font("FontA11", 8.0f), new SolidBrush(Color.Black), 70, 370 + offset);
                    }
                    if (selector == 2)
                    {
                        graphic.DrawString("Check No:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 235 + offset);
                        graphic.DrawString(checkNo, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 105, 235 + offset);
                        graphic.DrawString("Bank & Branch:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 250 + offset);
                        graphic.DrawString(Truncate(BankNBranch, 18), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 105, 250 + offset);
                        graphic.DrawString("Amount:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 265 + offset);
                        graphic.DrawString(checkAmount.ToString("#,###,##0.00"), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 105, 265 + offset);

                        graphic.DrawString("Transaction #:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 295 + offset);
                        graphic.DrawString(OrNo.ToString(), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 110, 295 + offset);
                        graphic.DrawString("CASHIER:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 310 + offset);
                        graphic.DrawString(lblUserAccount.Text, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 110, 310 + offset);
                        graphic.DrawString("Terminal #:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 325 + offset);
                        graphic.DrawString(fl.tN, new Font("Tahoma", 10), new SolidBrush(Color.Black), 110, 325 + offset);
                        graphic.DrawString("**************************************************************************", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 3, 340 + offset);
                        graphic.DrawString("THIS IS NOT YOUR OFFICIAL RECIEPT", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 30, 355 + offset);
                        graphic.DrawString("Thank you and Come Again ", new Font("FontA11", 8.0f), new SolidBrush(Color.Black), 70, 370 + offset);
                    }
                    if (selector == 3)
                    {
                        graphic.DrawString("Code:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 235 + offset);
                        graphic.DrawString(CustCode, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 85, 235 + offset);
                        graphic.DrawString("Company:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 250 + offset);
                        graphic.DrawString(Truncate(Company, 22), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 85, 250 + offset);
                        graphic.DrawString("Amount:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 265 + offset);
                        graphic.DrawString(arAmount.ToString("#,###,##0.00") + " Cr", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 85, 265 + offset);

                        graphic.DrawString("Transaction #:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 295 + offset);
                        graphic.DrawString(OrNo.ToString(), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 110, 295 + offset);
                        graphic.DrawString("CASHIER:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 310 + offset);
                        graphic.DrawString(lblUserAccount.Text, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 110, 310 + offset);
                        graphic.DrawString("Terminal #:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 325 + offset);
                        graphic.DrawString(fl.tN, new Font("Tahoma", 10), new SolidBrush(Color.Black), 110, 325 + offset);
                        graphic.DrawString("**************************************************************************", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 3, 340 + offset);
                        graphic.DrawString("THIS IS NOT YOUR OFFICIAL RECEIPT", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 30, 355 + offset);
                        graphic.DrawString("Thank you and Come Again ", new Font("FontA11", 8.0f), new SolidBrush(Color.Black), 70, 370 + offset);
                    }
                    if (selector == 4)
                    {
                        this.getValidUntil(gCardno);
                        graphic.DrawString("Gift Card #:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 235 + offset);
                        graphic.DrawString(gCardno, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 85, 235 + offset);
                        graphic.DrawString("Valid Until:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 250 + offset);
                        graphic.DrawString(Convert.ToDateTime(validU).ToString("MM/dd/yy"), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 85, 250 + offset);
                        graphic.DrawString("Amount:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 265 + offset);
                        graphic.DrawString(gAmount.ToString("#,###,##0.00") + " Dr", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 85, 265 + offset);

                        graphic.DrawString("Transaction #:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 295 + offset);
                        graphic.DrawString(OrNo.ToString(), new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 110, 295 + offset);
                        graphic.DrawString("CASHIER:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 310 + offset);
                        graphic.DrawString(lblUserAccount.Text, new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 310, 375 + offset);
                        graphic.DrawString("Terminal #:", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 10, 325 + offset);
                        graphic.DrawString(fl.tN, new Font("Tahoma", 10), new SolidBrush(Color.Black), 110, 325 + offset);
                        graphic.DrawString("**************************************************************************", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 3, 340 + offset);
                        graphic.DrawString("THIS IS NOT YOUR OFFICIAL RECEIPT", new Font("FontA11", 9.0f), new SolidBrush(Color.Black), 30, 355 + offset);
                        graphic.DrawString("Thank you and Come Again ", new Font("FontA11", 8.0f), new SolidBrush(Color.Black), 70, 370 + offset);
                    }
                }
                catch (MySqlException)
                {
                    rdDescription.Text = "Check Printer or Check Server!";
                }
            }
        }
    }
Community
  • 1
  • 1
Bon
  • 309
  • 1
  • 5
  • 16
  • 2
    The obvious mistake is that for the 2nd page you start back from scratch. The `i` variable needs to be a field of your class. You need the BeginPrint event to create the adapter and reset `i` back to 0. – Hans Passant May 25 '15 at 14:43

1 Answers1

1

I wouldn't mix query and printing instruction in the same procedure... Anyway, when HasMorePages is true, printDocument1_PrintPage is called again for the following page and you start the query from beginning, so you'll print the first page forever. In printDocument1_PrintPage you should print the i-th page in general. So maybe in the first page you'll print 50 rows (form 1 to 50) then with member variables, the second time the procedure is called, you'll continue from 51 to 100 and so forth. So you need to run the query before starting printing.

Get a look at http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/MultiPagePrint.htm and see how 'offset' is always incremented during printing and not initialized every time (so page after page you'll print row by row all your document).

Fil
  • 1,032
  • 13
  • 29