我想出了如何将基本文本打印到我们的POS打印机,但我无法弄清楚如何使转义字符工作(粗体,文本对齐等)。现在我只是使用Microsoft PosPrinter模拟器进行测试。
这是我正在尝试的事情
_printer.PrintNormal(PrinterStation.Receipt, (char)27 + "|bC" + printText + (char)13 + (char)10);
我希望以粗体打印我的printText
,然后换行。当我取出(char)27 + "|bC"
时,它可以正常工作。
documentation for the escape codes is here
我得到的错误是
发生了'System.FormatException'类型的第一次机会异常 Microsoft.PointOfService.ControlBase.dll输入字符串不在 格式正确。
我尝试了很多变化,但似乎无法绕过它。
修改即可。这是堆栈跟踪..
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s, IFormatProvider provider)
at Microsoft.PointOfService.DeviceSimulators.PosPrinterSimulatorWindow.ProcessEscapes(String str)
at Microsoft.PointOfService.DeviceSimulators.PosPrinterSimulatorWindow.DisplayText(String str)
at Microsoft.PointOfService.DeviceSimulators.PosPrinterSimulator.PrintNormalImpl(PrinterStation station, PrinterState printerState, String data)
at Microsoft.PointOfService.BaseServiceObjects.PosPrinterBase.OutputRequestHandler(OutputRequest Request)
at Microsoft.PointOfService.Internal.PosCommonInternal.ProcessOutputRequest(OutputRequest request, Boolean asyncOperation)
at Microsoft.PointOfService.BaseServiceObjects.PosPrinterBase.ProcessRequest(PrintOperation operation)
at Microsoft.PointOfService.BaseServiceObjects.PosPrinterBase.PrintNormal(PrinterStation station, String data)
at MyProjectNamespace) in MyFile.cs:line 74
答案 0 :(得分:3)
您刚刚找到了这个答案,这对我有用。试试这个。
string Bold = System.Text.ASCIIEncoding.ASCII.GetString(new byte[] { 27, (byte)'|', (byte)'b', (byte)'C' });
或者您可以简单地声明:
string ESC = System.Text.ASCIIEncoding.ASCII.GetString(new byte[] {27});
然后以您的格式或文字形式使用它:
ESC +" | cA" - 这是中心。 ESC +" | bC" - 大胆。
ESC +" | bC" +"你好世界" - 这会加粗字符串。
答案 1 :(得分:1)
对我而言,它可以像这样逃避字符串:
_printer.PrintNormal(PrinterStation.Receipt, "\x1B|bCYour Bold line\r\n");
也许你将来仍然可以使用它。
答案 2 :(得分:1)
从我的测试中我认为打印机模拟器不会接受ESC字符。
我使用许多常量替代品创建要发送到打印机的文本,如:
string escAlignCenter = String.Format("{0}|cA", ((char)27));
string escAlignRight = String.Format("{0}|rA", ((char)27));
string escBoldOn = String.Format("{0}|bC", ((char)27));
string escNewLine = String.Format("{0}|1lF", ((char)27));
string escPaperCut = String.Format("{0}|75P", ((char)27));
string escReset = String.Format("{0}|N", ((char)27));
string escUnderlineOn = String.Format("{0}|uC", ((char)27));
所以我可能会将以下内容发送到打印机:
String.Format("{0}Hellow world!", escAlignCenter)"
为避免在使用打印机模拟器时出现错误,我需要执行以下操作
if(useSimulator)
{
string xxx = String.Format("{0}", ((char)27));
string testString = testPrint.ToString();
testString = testString.Replace(xxx, "\\");
logger.Debug("text to print is [{0}]", testString);
posPrinter.PrintNormal(PrinterStation.Receipt, testString);
}
else
posPrinter.PrintNormal(PrinterStation.Receipt, testPrint.ToString());
删除错误但是打印到模拟器的文本包含所有ESC格式。
答案 3 :(得分:0)
我最终确定每行可以打印多少个字符并创建一些对齐功能。在这一点上,我认为POS没有内置于.Net。
仍无法弄清楚粗体,斜体等格式化。
答案 4 :(得分:0)
您是否检查过您的打印机是否支持Bold,Italic等?即。
if (_printer.CapRecBold)
// printer supports bold
if (_printer.CapRecItalic)
// printer supports italic
答案 5 :(得分:0)
Androidz的回答是正确的。你也可以做下面的事情。
假设您要打印粗体。
string text = "ESC|bC" + "BOLD Text";
// Now replace ESC as below.
string textToPrint = text.Replace("ESC", ((char)27).ToString());
就是这样。
答案 6 :(得分:0)
此字符串对我有用"\u001B"