public static bool PrintByTxt(string txt) { bool ret = false; try { if (PrinterHelper.TxOpenPrinter(1, 0)) { int status = PrinterHelper.TxGetStatus(); if (status == 88) { //无故障情况下才执行打印 PrinterHelper.TxInit(); PrinterHelper.TxResetFont(); PrinterHelper.TxOutputStringLn(txt); //string urlimg = Application.StartupPath + "\\File\\zhangyu.jpg"; //bool retImg = PrinterHelper.TxPrintImage(urlimg); PrinterHelper.TxDoFunction(10, 240, 0); //走纸30毫米 PrinterHelper.TxDoFunction(12, 2, 40); //走纸30毫米 //Thread.Sleep(1000); bool isSuccess = PrinterHelper.CheckIsPrintSuccess(); if (isSuccess) { ret = true; } else { //MessageBox.Show("打印失败,有可能是打印机内纸不够、打印机断电或其他异常,请确保打印机接上电源并且其内有足够的纸,然后执行一次关闭打印机后再打开打印机。"); ret = false; } } else if (status == 56) { //MessageBox.Show("检测到打印机内没有纸,如果有纸,请执行一次关闭打印机后再打开打印机。"); ret = false; } else { //MessageBox.Show("打印机繁忙或异常,请尝试执行一次关闭打印机后再打开打印机,可能能解决问题。"); ret = false; } } else { //MessageBox.Show("无法连接打印机,请确保打印机电源打开并且正常连接到电脑"); ret = false; } } catch (Exception ex) { ret = false; //MessageBox.Show("打印时出现异常"); } finally { PrinterHelper.TxClosePrinter(); } return(ret); }