I figured out how to print basic text to our POS printer, but I can't figure out how to get the escape characters to work (for bold, text alignment, etc). For now I'm just testing with the Microsoft PosPrinter Simulator.
Here's what I'm trying
_printer.PrintNormal(PrinterStation.Receipt, (char)27 + "|bC" + printText + (char)13 + (char)10);
I'd expect that to print my printText
in bold followed by a line break. When I take out (char)27 + "|bC"
then it works fine.
The documentation for the escape codes is here
The error I get is
A first chance exception of type 'System.FormatException' occurred in Microsoft.PointOfService.ControlBase.dll Input string was not in a correct format.
I tried a bunch of variations but can't seem to wrap my head around it.
Edit. Here's the stack trace..
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
Hi just found out this answer and this works for me. try this one.
string Bold = System.Text.ASCIIEncoding.ASCII.GetString(new byte[] { 27, (byte)'|', (byte)'b', (byte)'C' });
or You can simply declare this:
string ESC = System.Text.ASCIIEncoding.ASCII.GetString(new byte[] {27});
then use it in your format or text like this:
ESC + "|cA" -- this is for center. ESC + "|bC" -- for bold.
ESC + "|bC" + "hello world" -- this will bold the string.
For me it worked to escape the string like this:
_printer.PrintNormal(PrinterStation.Receipt, "\x1B|bCYour Bold line\r\n");
Maybe you can still use it in the future.
From my testing I think the printer simulator will not accept the ESC character.
I create the text to send to the printer using a number of constant substitutes like:
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));
so I might send the following to the printer:
String.Format("{0}Hellow world!", escAlignCenter)"
To avoid getting the error when using the printer simulator I need to do the following
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());
That removes the error however the text printed to the simulator includes all the ESC formatting.
I ended up determining how many characters could be printed per line and created some alignment functions. At this point I think there is nothing built-in to POS for .Net.
Still can't figure out bold, italics, etc. formatting.
Did you check if your printer supports Bold, Italic etc? i.e.
if (_printer.CapRecBold)
// printer supports bold
if (_printer.CapRecItalic)
// printer supports italic
Androidz answer is correct. Also you can do some thing like below.
Assume you want to print bold.
string text = "ESC|bC" + "BOLD Text";
// Now replace ESC as below.
string textToPrint = text.Replace("ESC", ((char)27).ToString());
That's it.
This string worked for me "\u001B"
来源:https://stackoverflow.com/questions/12408057/pos-for-net-print-formatting-cant-use-escape-character-char27