2015-08-03 204 views
2

我有一個愛普生熱敏打印機,現在我要打印一些足夠長的收據,我使用愛普生提供的代碼示例。下面的一些代碼,現在的問題是,當打印機超過一定長度(約30釐米)時,打印機將停止並切割收據,如下圖所示。如何在不自動切割的情況下打印長期收據。C#打印收據長與愛普生熱敏打印機

// 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(); 
    } 

enter image description here

+0

你有沒有找到這個解決方案? – Markinson

回答

2

大多數熱敏打印機具有指示是否削減收到的設置。

在這種情況下,如果您繪製的內容超出默認邊距(e.MarginBounds),您至少可以嘗試將e.HasMorePages設置爲true。 它取決於打印機驅動程序,如果它將在所有頁面的末尾切割,或僅在最後一頁切割。 當然你需要做你自己的分頁,例如在第一頁上只繪製應該在第一頁上的項目等等。

-1

以下信息從Epson programming guide萃取。請參閱它的更多細節。

可以發出「部分切割」命令,可以通過它的十六進制代碼調用,即port.write("\x1B\x69");

+0

雖然此鏈接可能回答問題,但最好在此處包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/18938917) –

相關問題