如何在c#中使用esc / pos命令?

时间:2014-01-25 10:53:16

标签: c# printing

如何在C#中使用ESC / POS命令?我需要这样的格式enter image description here

但我无法实现这种格式。 我尝试了一些代码,但没用。

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.
    }

4 个答案:

答案 0 :(得分:6)

您可以查看此链接。我现在正在使用它和它的工作!!

http://www.codeproject.com/Tips/704989/Print-Direct-To-Windows-Printer-EPOS-Receipt 小心使用,必须知道热敏打印机的ESC / POS命令。如果我没弄错的话,手动命令必须退出打印机随附的CD。

亲切的问候

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

答案 1 :(得分:2)

我知道这不是对如何使用转义码的问题的答案,但是创建一个PDF文件( Portable 的P)会好得多。您将有更好的机会在大多数打印机上完全按照预期进行渲染。

您可以使用PDFsharp生成PDF。它是开源的,免费的,使用起来非常简单。

答案 2 :(得分: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;
                }
            }
        }

答案 3 :(得分:1)

我正在研究热敏打印机,我遇到了像你这样的ESC / POS命令。谷歌搜索后我发现ThermalDotNet对我非常有用。这个类还支持图像打印和字母打印。 我希望这会有所帮助;)