调用代码:
//不同的打印机需要不同的参数,这个参数应该可以在打印机的编程文档中找到 string str = ((char)29).ToString() + ((char)86).ToString() + ((char)0).ToString(); byte[] p_Byte = System.Text.Encoding.Default.GetBytes(str); string p_PrintName = "打印机名称";
WinApiPrintByte(p_PrintName, p_Byte);
方法:
/// <summary>
/// 发送数据到打印机
/// </summary>
/// <param name="p_PrintName">打印机名称</param>
/// <param name="p_Byte">切纸数据</param>
public static void WinApiPrintByte(string p_PrintName, byte[] p_Byte)
{
if (p_PrintName != null && p_PrintName.Length > 0)
{
IntPtr _PrintHandle;
IntPtr _JobHandle = Marshal.AllocHGlobal(100);
if (OpenPrinter(p_PrintName, out _PrintHandle, IntPtr.Zero))
{
ADDJOB_INFO_1 _JobInfo = new ADDJOB_INFO_1();
int _Size;
AddJob(_PrintHandle, 1, _JobHandle, 100, out _Size);
_JobInfo = (ADDJOB_INFO_1)Marshal.PtrToStructure(_JobHandle, typeof(ADDJOB_INFO_1));
//System.IO.File.WriteAllBytes(p_PrintName, p_Byte);
System.IO.File.WriteAllBytes(_JobInfo.lpPath, p_Byte);
ScheduleJob(_PrintHandle, _JobInfo.JobID);
ClosePrinter(_PrintHandle);
Marshal.FreeHGlobal(_JobHandle);
}
}
}
[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPTStr)]string strPrinterName, out IntPtr ptrPrinter, IntPtr ptrDefalut);
[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern bool ClosePrinter(IntPtr ptrPrinter);
[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern bool AddJob(IntPtr ptrPrinter, Int32 iLevel, IntPtr ptrJob, Int32 iSize, out Int32 iCpSize);
[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern bool ScheduleJob(IntPtr ptrPrinter, Int32 JobID);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct ADDJOB_INFO_1
{
[MarshalAs(UnmanagedType.LPTStr)]
public string lpPath;
public Int32 JobID;
}
说明:本段代码来源于网络。
在热敏小票打印机打印机上测试可用
看原文介绍,应该也可用于其他带有切纸刀的打印机
浙公网安备 33010602011771号