How to use esc/pos command with c#?

38,839

Solution 1

You can check this link. I now using that and It's Works!!

http://www.codeproject.com/Tips/704989/Print-Direct-To-Windows-Printer-EPOS-Receipt Use with careful, you must know ESC/POS command for thermal printer. If I'm not mistaken, the manual command must exit on the CD that came with the printer.

Kind Regards

Bonus: http://www.developerfusion.com/tools/convert/vb-to-csharp/

Solution 2

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Runtime.InteropServices;

        namespace Apteka.Printer
        {
            public class RawPrinterHelper
            {
                [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
                public class DOCINFOA
                {
                    [MarshalAs(UnmanagedType.LPStr)]
                    public string pDocName;
                    [MarshalAs(UnmanagedType.LPStr)]
                    public string pOutputFile;
                    [MarshalAs(UnmanagedType.LPStr)]
                    public string pDataType;
                }

                [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
                public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, ref IntPtr hPrinter, IntPtr pd);

                [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
                public static extern bool ClosePrinter(IntPtr hPrinter);

                [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
                public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In()][MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

                [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
                public static extern bool EndDocPrinter(IntPtr hPrinter);

                [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
                public static extern bool StartPagePrinter(IntPtr hPrinter);

                [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
                public static extern bool EndPagePrinter(IntPtr hPrinter);

                [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
                public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, ref Int32 dwWritten);

                private IntPtr hPrinter = new IntPtr(0);
                private DOCINFOA di = new DOCINFOA();
                private bool PrinterOpen = false;

                public bool PrinterIsOpen
                {
                    get
                    {
                        return PrinterOpen;
                    }
                }

                public bool OpenPrint(string szPrinterName)
                {
                    if (PrinterOpen == false)
                    {
                        di.pDocName = ".NET RAW Document";
                        di.pDataType = "RAW";

                        if (OpenPrinter(szPrinterName.Normalize(), ref hPrinter, IntPtr.Zero))
                        {
                            if (StartDocPrinter(hPrinter, 1, di))
                            {
                                if (StartPagePrinter(hPrinter))
                                    PrinterOpen = true;
                            }
                        }
                    }

                   return PrinterOpen;
                }

                public void ClosePrint()
                {
                    if (PrinterOpen)
                    {
                        EndPagePrinter(hPrinter);
                        EndDocPrinter(hPrinter);
                        ClosePrinter(hPrinter);
                        PrinterOpen = false;
                    }
                }

                public bool SendStringToPrinter(string szPrinterName, string szString)
                {
                    if (PrinterOpen)
                    {
                        IntPtr pBytes;
                        Int32 dwCount;
                        Int32 dwWritten = 0;

                        dwCount = szString.Length;

                        pBytes = Marshal.StringToCoTaskMemAnsi(szString);

                        var res= WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten);
                        Marshal.FreeCoTaskMem(pBytes);
                        return res;
                    }
                    else
                        return false;
                }
            }
        }

Solution 3

I know that this is not exactly an answer to the question of how to use escape codes, but it would be much better to create a PDF file (P for Portable). You'll have a better chance to have it rendered exactly as intended on most printers.

You can generate a PDF with PDFsharp. It's open source and free and it's quite simple to use.

Solution 4

I was working on thermal printer and I came across to ESC/POS commands like you do.After Googling I found ThermalDotNet very useful for me.This class also supports image printing as well as letter printing. I hope this helps ;)

Share:
38,839
Vicky
Author by

Vicky

Big Data Analyst

Updated on July 09, 2022

Comments

  • Vicky
    Vicky almost 2 years

    How to use ESC/POS command with C#? I need format like this enter image description here

    but I cannot achieve this format. I tried some codes ,but no use.

    using (var ms = new MemoryStream())
        using (var bw = new BinaryWriter(ms))
        {
            // Reset the printer bws (NV images are not cleared)
            bw.Write(AsciiControlChars.Escape);
            bw.Write('@');
            bw.Write(AsciiControlChars.Newline);
            bw.Write(AsciiControlChars.Escape);
            bw.Write("_______________________________________________");
            bw.Write(AsciiControlChars.Newline);
    
            bw.Write("Service           Price         Qty       Total");
            bw.Write("------------------------------------------------");
    
            bw.Write(AsciiControlChars.GroupSeparator);
            bw.Write('V');
            bw.Write((byte)66);
            bw.Write((byte)3);
            bw.Flush();
            // Send the converted ANSI string to the printer.
        }
    
  • Muhammad Saqib
    Muhammad Saqib almost 10 years
    Thank you for your link, But This link you mention is only for string, Asker also want to print logo.
  • egyware
    egyware almost 10 years
    Ohh... You must learn ESC/POS commands for your thermal printer. For example check code: PrintDashes() //that function call Print(eLeft + eNmlText + "- ".PadRight(42, "-")) But what is eLeft? Public Const eLeft As String = Chr(27) + Chr(97) + "0" Now I check the manual for TM-T81 Thermal Printer manualslib.com/manual/543472/Epson-Tm-T81.html?page=80#manua‌​l It says Select justification Well sorry my english is really bad, but If you check the manual you can find the command for print logo.
  • egyware
    egyware almost 10 years
    Maybe... <Function 69> GS ( L p L p H m fn kc1 kc2 x y (fn = 69) Print the specified NV graphics data manualslib.com/manual/543472/Epson-Tm-T81.html?page=97#manua‌​l