I have a client that has a process which builds a text file. This text file contains commands that is used by their thermal printer, i.e.:
^XA^CF,0,0,0^PR12^MD30^PW800%^PON
^FO0,147^GB800,4,4^FS
^FO0,401^GB800,4,4^FS
^FO0,746^GB800,4,4^FS
^FO35,12^AdN,0,0^FWN^FH^FDFrom:^FS
^FO35,31^AdN,0,0^FWN^FH^FDAri Rothman^FS
^FO35,51^AdN,0,0^FWN^FH^
They want this to print directly to their printer. I have attempted using the following code to produce the results:
publicclass Printer{
[DllImport("winspool.Drv", EntryPoint="GetDefaultPrinter")]
publicstaticexternbool GetDefaultPrinter(
StringBuilder pszBuffer, // printer name buffer
refint pcchBuffer // size of name buffer
);
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
CallingConvention=CallingConvention.StdCall )]
publicstaticexternlong OpenPrinter(string pPrinterName,
ref IntPtr phPrinter, int pDefault);
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
CallingConvention=CallingConvention.StdCall )]
publicstaticexternlong StartDocPrinter(IntPtr hPrinter,
int Level, ref DOCINFO pDocInfo);
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
publicstaticexternlong StartPagePrinter(IntPtr hPrinter);
[ DllImport( "winspool.drv",CharSet=CharSet.Ansi,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
publicstaticexternlong WritePrinter(IntPtr hPrinter,string data,
int buf,refint pcWritten);
[ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
publicstaticexternlong EndPagePrinter(IntPtr hPrinter);
[ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
publicstaticexternlong EndDocPrinter(IntPtr hPrinter);
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall )]
publicstaticexternlong ClosePrinter(IntPtr hPrinter); public Printer()
{}
publicstaticvoid SendToPrinter(string jobName, string PCL5Commands, string printerName){
System.IntPtr lhPrinter=
new System.IntPtr();DOCINFO di =
new DOCINFO(); int pcWritten=0;di.pDocName=jobName;
di.pDataType="RAW";
OpenPrinter(printerName,
ref lhPrinter,0);StartDocPrinter(lhPrinter,1,
ref di);StartPagePrinter(lhPrinter);
WritePrinter(lhPrinter,PCL5Commands,PCL5Commands.Length,
ref pcWritten);EndPagePrinter(lhPrinter);
EndDocPrinter(lhPrinter);
ClosePrinter(lhPrinter);
}
}
However, this prints the commands as text. But I am using an HP PCL 6 printer for testing purposes, so it may be caused by the printer not recognizing the commands. I am trying to gain opinions on this situation. Also, is there a more direct way to issue printer commands directly, or is this the best way? I was thinking maybe opening a socket between the client and the printer and feeding this through directly to the port.
Any thoughts?