바코드 프린터 명령을 사용하여 한자를 인쇄하는 문제
11421 단어 프린트
인터넷에서 많은 것을 찾았는데 유일하게 실행할 수 있는 것은 제3자 dll을 호출해서 완성하는 것이다. 이렇게 하면 인쇄할 한자를 제3자 dll에 따라 변환하여 바코드 프린터 ZPL이 스스로 식별하는 언어를 얻을 수 있다.됐어.
저는 지브라 GK888T 프린터를 사용합니다.fnthex32.dll
[DllImport("fnthex32.dll")]
public static extern int GETFONTHEX(
string BarcodeText,
string FontName,
string FileName,
int Orient,
int Height,
int Width,
int IsBold,
int IsItalic,
StringBuilder ReturnBarcodeCMD);
클래스 BarcodePrint가 있음
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Barcode
{
class BarcodePrint
{
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, out int lpNumberOfBytesWritten, out OVERLAPPED lpOverlapped);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool CloseHandle(int hObject);
private int iHandle;
public bool Open()
{
iHandle = CreateFile("LPT1:", (uint)FileAccess.ReadWrite, 0, 0, (int)FileMode.Open, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{
return false;
}
}
public bool Write(string Mystring)
{
if (iHandle != -1)
{
int i;
OVERLAPPED x;
byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
return WriteFile(iHandle, mybyte, mybyte.Length, out i, out x);
}
else
{
throw new Exception("LPT1 !");
}
}
public bool Close()
{
return CloseHandle(iHandle);
}
}
}
RawPrinterHelper 클래스
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ZPLPrinter
{
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;
}
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;
}
public static bool SendStringToPrinter(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.
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
// Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
}
}
인쇄를 눌렀을 때
string sBarCodeCMD = ""; //
//
StringBuilder strbd1 = new StringBuilder(10240);
StringBuilder strbd2= new StringBuilder(10240);
StringBuilder strbd3 = new StringBuilder(10240);
StringBuilder strbd4 = new StringBuilder(10240);
int i1 = GETFONTHEX(" ", " ", "temp1", 0, 30,20, 0, 0, strbd1);
int i2 = GETFONTHEX("XXX:", " ", "temp2", 0, 20,15, 0, 0, strbd2);
int i3 = GETFONTHEX("XX:", " ", "temp3", 0, 20,15, 0, 0, strbd3);
int i4 = GETFONTHEX("XX:"+DateTime.Now+"", " ", "temp4", 0, 20, 15, 0, 0, strbd4);
sBarCodeCMD = strbd1.ToString() + strbd2.ToString() + strbd3.ToString() + strbd4.ToString() + @"^XA^MD30
^LH20,20^FO80,200^XGtemp1,1,1^FS
^LH20,20^FO80,240^GB620,0,2^FS
^LH20,20^FO80,260^XGtemp2,1,1^FS
^LH20,20^FO80,290^XGtemp3,1,1^FS
^LH20,20^FO80,320^GB620,0,2^FS
^LH20,20^FO80,350^BY1.5,3,100 ^BCN,100,Y,N,N^FD370921000001013090312024^FS
^LH20,20^FO80,480^XGtemp4,1,1^FS
^XZ";
RawPrinterHelper.SendStringToPrinter("ZDesigner GK888t (EPL)", sBarCodeCMD);
인쇄 결과는 다음과 같습니다.
이것은 코드128입니다. 사실 대부분은 차이가 많지 않습니다. 단지 지령이 다를 뿐입니다.
인쇄된 한자
____________________________________
XXX
XX
타임
바코드
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
페이지에서 한 개의 기록만 인쇄하고 나머지는 모두 숨깁니다텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.