c#用Epson热敏打印机打印长收据

时间:2015-08-03 10:41:22

标签: c# printing thermal-printer

我有一台爱普生热敏打印机,现在我必须打印一些足够长的收据,我使用爱普生提供的代码示例。下面的一些代码,问题是当打印机超过一定(约30厘米)长度时,打印机将停止并剪切收据,如下图所示。如何在不自动剪切的情况下打印长收据。

// Constant variable holding the printer name.
    private const string PRINTER_NAME = "PosPrinter";

    // Variables/Objects.
    private StatusAPI m_objAPI;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
        Application.Run(new Form1());
    }

    // The executed function when the Print button is clicked.
    private void cmdPrint_Click(object sender, System.EventArgs e)
    {
        Boolean isFinish;
        PrintDocument pdPrint = new PrintDocument();
        pdPrint.PrintPage += new PrintPageEventHandler(pdPrint_PrintPage);
        // Change the printer to the indicated printer.
        pdPrint.PrinterSettings.PrinterName = PRINTER_NAME;

        try 
        {
            // Open a printer status monitor for the selected printer.
            if (m_objAPI.OpenMonPrinter(OpenType.TYPE_PRINTER, pdPrint.PrinterSettings.PrinterName) == ErrorCode.SUCCESS)
            {
                if (pdPrint.PrinterSettings.IsValid)
                {
                    pdPrint.DocumentName = "Testing";
                    // Start printing.
                    pdPrint.Print();

                    // Check printing status.
                    isFinish = false;

                    // Perform the status check as long as the status is not ASB_PRINT_SUCCESS.
                    do
                    {
                        if (m_objAPI.Status.ToString().Contains(ASB.ASB_PRINT_SUCCESS.ToString()))
                            isFinish = true;

                    } while (!isFinish);

                    // Notify printing completion.
                    MessageBox.Show("Printing complete.", "Program06", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                }
                else
                    MessageBox.Show("Printer is not available.", "Program06", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                // Always close the Status Monitor after using the Status API.
                if(m_objAPI.CloseMonPrinter() != ErrorCode.SUCCESS)
                    MessageBox.Show("Failed to close printer status monitor.", "Program06", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
            else
                MessageBox.Show("Failed to open printer status monitor.", "Program06", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        catch
        {
            MessageBox.Show("Failed to open StatusAPI.", "Program06", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

    }

    // The event handler function when pdPrint.Print is called.
    // This is where the actual printing of sample data to the printer.
    private void pdPrint_PrintPage(object sender, PrintPageEventArgs e)
    {
        float x, y, lineOffset;

        // Instantiate font objects used in printing.
        Font printFont = new Font("Microsoft Sans Serif", (float)10, FontStyle.Regular, GraphicsUnit.Point); // Substituted to FontA Font

        e.Graphics.PageUnit = GraphicsUnit.Point;

        // Draw the bitmap
        x = 79;
        y = 0;
        e.Graphics.DrawImage(pbImage.Image, x, y, pbImage.Image.Width - 13, pbImage.Image.Height - 10);

        // Print the receipt text
        lineOffset = printFont.GetHeight(e.Graphics) - (float)3.5;
        x = 10;
        y = 24 + lineOffset;
        for (int i = 0; i < 500; i++)
        {
            e.Graphics.DrawString("123xxstreet,xxxcity,xxxxstate", printFont, Brushes.Black, x, y);
            y += lineOffset;
        }

        e.Graphics.DrawString("              TEL   9999-99-9999       C#2", printFont, Brushes.Black, x, y);
        y += lineOffset;
        e.Graphics.DrawString("       November.23, 2007     PM 4:24", printFont, Brushes.Black, x, y);
        y = y + (lineOffset * (float)2.5) ;
        e.Graphics.DrawString("apples                       $20.00", printFont, Brushes.Black, x, y);
        y += lineOffset;
        e.Graphics.DrawString("grapes                       $30.00", printFont, Brushes.Black, x, y);
        y += lineOffset;
        e.Graphics.DrawString("bananas                      $40.00", printFont, Brushes.Black, x, y);
        y += lineOffset;
        e.Graphics.DrawString("lemons                       $50.00", printFont, Brushes.Black, x, y);
        y += lineOffset;
        e.Graphics.DrawString("oranges                      $60.00", printFont, Brushes.Black, x, y);
        y += (lineOffset * (float)2.3);
        e.Graphics.DrawString("Tax excluded.               $200.00", printFont, Brushes.Black, x, y);
        y += lineOffset;
        e.Graphics.DrawString("Tax     5.0%                 $10.00", printFont, Brushes.Black, x, y);
        y += lineOffset;
        e.Graphics.DrawString("___________________________________", printFont, Brushes.Black, x, y);

        printFont = new Font("Microsoft Sans Serif", 20, FontStyle.Regular, GraphicsUnit.Point);
        lineOffset = printFont.GetHeight(e.Graphics) - 3;
        y += lineOffset;
        e.Graphics.DrawString("Total     $210.00", printFont, Brushes.Black, x - 1, y);

        printFont = new Font("Microsoft Sans Serif", (float)10, FontStyle.Regular, GraphicsUnit.Point);
        lineOffset = printFont.GetHeight(e.Graphics);
        y = y + lineOffset + 1;
        e.Graphics.DrawString("Customer's payment         $250.00", printFont, Brushes.Black, x, y);
        y += lineOffset;
        e.Graphics.DrawString("Change                      $40.00", printFont, Brushes.Black, x, y - 2);

        // Indicate that no more data to print, and the Print Document can now send the print data to the spooler.
        e.HasMorePages = false;
    }

    // The executed function when the Close button is clicked.
    private void cmdClose_Click(object sender, System.EventArgs e)
    {
        Close();
    }

one-to-many with join table

1 个答案:

答案 0 :(得分:2)

大多数热敏打印机都有一个设置,指示是否剪切收据。

在这种情况下,如果您绘制的内容超出默认边距(e.MarginBounds),您至少可以尝试将e.HasMorePages设置为true。 它取决于打印机驱动程序,如果它将在所有页面的末尾切割,或仅在最后一页切割。 当然,您需要进行自己的分页,例如在第一页上只绘制应该在第一页上的项目,依此类推。