POS for .Net print formatting - can't use escape character (char)27

♀尐吖头ヾ 提交于 2019-12-02 10:27:20

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
Dzero

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"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!