用C#打印多页打印文档

时间:2015-05-25 14:09:14

标签: c# printdocument receipt

我尝试遵循基于Printing multiple document in c# window application

的代码

当项目很少用于打印时,它会在热敏收据打印机上打印。但是,只要打印的项目显示超过1,就会花费无限循环。

这是我的代码

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

活动代码

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

1 个答案:

答案 0 :(得分:1)

我不会在同一个程序中混合查询和打印指令...无论如何,当HasMorePages为true时,再次为下一页调用printDocument1_PrintPage并从头开始查询,这样你就可以了永远打印第一页。 在printDocument1_PrintPage中,您应该打印一般的第i页。因此,也许在第一页中,您将打印50行(表单1到50),然后使用成员变量,第二次调用该过程,您将从51到100继续打印,依此类推。因此,您需要在开始打印之前运行查询。

查看http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/MultiPagePrint.htm,了解&#39;偏移&#39;在打印过程中总是递增,而不是每次都初始化(因此,您可以逐页打印所有文档)。