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

后端 未结 7 2054
不思量自难忘°
不思量自难忘° 2021-01-26 04:03

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 tes

相关标签:
7条回答
  • 2021-01-26 04:12

    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.

    0 讨论(0)
  • 2021-01-26 04:13

    Did you check if your printer supports Bold, Italic etc? i.e.

    if (_printer.CapRecBold)
       // printer supports bold
    
    if (_printer.CapRecItalic)
       // printer supports italic
    
    0 讨论(0)
  • 2021-01-26 04:18

    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.

    0 讨论(0)
  • 2021-01-26 04: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.

    0 讨论(0)
  • 2021-01-26 04:26

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题