using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace PrintLabel
{
public class RawPrinterHelper
{
// Structure and API declarions:
[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, out 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, out Int32 dwWritten);

// SendBytesToPrinter()
// When the function is given a printer name and an unmanaged array
// of bytes, the function sends those bytes to the print queue.
// Returns true on success, false on failure.
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false; // Assume failure unless you specifically succeed.

di.pDocName = "My C#.NET RAW Document";
di.pDataType = "RAW";

// Open the printer.
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
// Start a document.
if (StartDocPrinter(hPrinter, 1, di))
{
// Start a page.
if (StartPagePrinter(hPrinter))
{
// Write your bytes.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
// If you did not succeed, GetLastError may give more information
// about why not.
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}

/// <summary>
/// 发送文件到打印机
/// </summary>
/// <param name="szPrinterName">打印机名称</param>
/// <param name="szFileName">文件</param>
/// <returns></returns>
public static bool SendFileToPrinter(string szPrinterName, string szFileName)
{
// Open the file.
FileStream fs = new FileStream(szFileName, FileMode.Open);
// Create a BinaryReader on the file.
BinaryReader br = new BinaryReader(fs);
// Dim an array of bytes big enough to hold the file's contents.
Byte[] bytes = new Byte[fs.Length];
bool bSuccess = false;
// Your unmanaged pointer.
IntPtr pUnmanagedBytes = new IntPtr(0);
int nLength;

nLength = Convert.ToInt32(fs.Length);
// Read the contents of the file into the array.
bytes = br.ReadBytes(nLength);
// Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes);
return bSuccess;
}

/// <summary>
/// 发送字符串到打印机
/// </summary>
/// <param name="szPrinterName">打印机名称</param>
/// <param name="szString">指令</param>
/// <returns></returns>
public static bool SendStringToPrinter1(string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
// How many characters are in the string?
dwCount = szString.Length;
// Assume that the printer is expecting ANSI text, and then convert
// the string to ANSI text.
//Encoding.GetEncoding("GB2312").GetBytes(szString);
pBytes = Marshal.StringToCoTaskMemAnsi(szString);

// Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
/// <summary>
/// 打印标签带有中文字符的ZPL指令
/// </summary>
/// <param name="printerName">打印机名称</param>
/// <param name="szString">指令</param>
/// <returns></returns>
public static string SendStringToPrinter(string printerName, string szString)
{
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(szString); //转换格式
IntPtr ptr = Marshal.AllocHGlobal(bytes.Length + 2);
try
{
Marshal.Copy(bytes, 0, ptr, bytes.Length);
SendBytesToPrinter(printerName, ptr, bytes.Length);
return "success";
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
Marshal.FreeCoTaskMem(ptr);
}
}

/// <summary>
/// 网络打印
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <param name="szString"></param>
/// <returns></returns>
public static string SendStringToNetPrinter(string ip, int port, string szString)
{
try
{
//打开连接
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
client.Connect(ip, port);

//写入zpl命令
System.IO.StreamWriter writer = new System.IO.StreamWriter(client.GetStream());
writer.Write(szString);
writer.Flush();

//关闭连接
writer.Close();
client.Close();

return "success";
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}

ZPL通用打印类的更多相关文章

  1. ZPL条码打印类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  2. [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类

    [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...

  3. [.net 面向对象程序设计进阶] (11) 序列化(Serialization)(三) 通过接口 IXmlSerializable 实现XML序列化 及 通用XML类

    [.net 面向对象程序设计进阶] (11) 序列化(Serialization)(三) 通过接口 IXmlSerializable 实现XML序列化 及 通用XML类 本节导读:本节主要介绍通过序列 ...

  4. [asp.net]c# winform打印类

    using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using ...

  5. -XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息

    -XX:+PrintClassHistogram –按下Ctrl+Break后,打印类的信息: num     #instances         #bytes  class name ------ ...

  6. 通用窗口类 Inventory Pro 2.1.2 Demo1(下续篇 ),物品消耗扇形显示功能

    本篇想总结的是Inventory Pro中通用窗口的具体实现,但还是要强调下该插件的重点还是装备系统而不是通用窗口系统,所以这里提到的通用窗口类其实是通用装备窗口类(其实该插件中也有非装备窗口比如No ...

  7. 通用窗口类 Inventory Pro 2.1.2 Demo1(下)

    本篇想总结的是Inventory Pro中通用窗口的具体实现,但还是要强调下该插件的重点还是装备系统而不是通用窗口系统,所以这里提到的通用窗口类其实是通用装备窗口类(其实该插件中也有非装备窗口比如No ...

  8. 通用窗口类 Inventory Pro 2.1.2 Demo1(中)

    本篇想总结的是Inventory Pro中通用窗口的具体实现,但还是要强调下该插件的重点还是装备系统而不是通用窗口系统,所以这里提到的通用窗口类其实是通用装备窗口类(其实该插件中也有非装备窗口比如No ...

  9. Xml通用操作类

    using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml ...

随机推荐

  1. phonegap创建项目

    cordova create LynApp com.LynApp "LynApp"cd LynAppcordova platform add androidcordova buil ...

  2. day17 13.滚动结果集介绍

    滚动 一般结果集只能是向下的,不是滚动的,你要是想让它滚动你得设置才行. 类名或者接口里面有静态的可以.接口里面的属性全部都是public static final,类名/接口名.是属性,这些都是常量 ...

  3. JavaScript基础笔记集合(转)

    JavaScript基础笔记集合   JavaScript基础笔记集合   js简介 js是脚本语言.浏览器是逐行的读取代码,而传统编程会在执行前进行编译   js存放的位置 html脚本必须放在&l ...

  4. css自动换行 word-break:break-all和word-wrap:break-word(转)

    css自动换行 word-break:break-all和word-wrap:break-word 2012-12-31 17:30 by greenal, 159 阅读, 0 评论, 收藏, 编辑 ...

  5. jquery.pagination.js使用

    直接上代码: <script type="text/javascript"> var pageIndex = 1; //页面索引初始值 var pageSize = 1 ...

  6. C++面向对象类的实例题目十

    题目描述: 编写一个程序,其中有一个汽车类vehicle,它具有一个需要传递参数的构造函数,类中的数据成员:车轮个数wheels和车重weight放在保护段中:小车类car是它的私有派生类,其中包含载 ...

  7. (转)嵌入式C开发人员的最好笔试题目

    约定:   1) 下面的测试题中,认为所有必须的头文件都已经正确的包含了    2)数据类型             char 一个字节 1 byte        int 两个字节 2 byte ( ...

  8. spring第二篇

    上次写到spring是什么,说了很多的废话,那么从现在起 来看看spring如何使用  写几个例子 1 如何使用 spring 1.1导包 在导入四个包的基础上再导入日志包总共六个包 如下图 1.2 ...

  9. 【Arcgis for android】Error inflating class com.esri.android.map.MapView【已解决】

    解决方案:如果你是一个项目之前调试是好的,突然调试报这个错,听我的,直接卸载手机上调试的这个程序,重新调试,你会发现ok了 环境:arcgis android 10.2 错误:E/AndroidRun ...

  10. 初探webapi

    在网上看了小牛之路的webapi那篇文章,所以自己也想偿试一下 一,webapi简介 目前使用Web服务的三种主流的方式是:远程过程调用(RPC),面向服务架构(SOA)以及表征性状态转移(REST) ...