Printing PCL5 codes to the printer
I would like to know how do I print HTMl file to a printer and also binary to a printer using Win32 API's in C#.
I have the following code for printing a string. Please help me.
using System;
using System.Text;
using System.Runtime.InteropServices;
[StructLayout( LayoutKind.Sequential)]
public struct DOCINFO
{
[MarshalAs(UnmanagedType.LPWStr)]public string pDocName;
[MarshalAs(UnmanagedType.LPWStr)]public string pOutputFile;
[MarshalAs(UnmanagedType.LPWStr)]public string pDataType;
}
public class PrintDirect
{
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
CallingConvention=CallingConvention.StdCall )]
public static extern long OpenPrinter(string pPrinterName,ref IntPtr phPrinter, int pDefault);
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
CallingConvention=CallingConvention.StdCall )]
public static extern long StartDocPrinter(IntPtr hPrinter, int Level, ref DOCINFO pDocInfo);
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern long StartPagePrinter(IntPtr hPrinter);
[ DllImport( "winspool.drv",CharSet=CharSet.Ansi,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern long WritePrinter(IntPtr hPrinter,string data, int buf,ref int pcWritten);
[ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern long EndPagePrinter(IntPtr hPrinter);
[ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern long EndDocPrinter(IntPtr hPrinter);
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall )]
public static extern long ClosePrinter(IntPtr hPrinter);
}
public class App1
{
public static void Main()
{
System.IntPtr lhPrinter=new System.IntPtr();
DOCINFO di = new DOCINFO();
int pcWritten=0;
string st1;
// text to print with a form feed character
st1="This is a test string to directly to a printer\f";
// Send a "\f" form feed character at the end to the printer
di.pDocName="Doc1";
di.pDataType="RAW";
//lhPrinter contains the handle for the printer opened
//If lhPrinter is 0 then an error has occured
PrintDirect.OpenPrinter("\\\\MBS-PRTS-S01\\Pokey",ref lhPrinter,0);
PrintDirect.StartDocPrinter(lhPrinter,1,ref di);
PrintDirect.StartPagePrinter(lhPrinter);
try
{
PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
PrintDirect.EndPagePrinter(lhPrinter);
PrintDirect.EndDocPrinter(lhPrinter);
PrintDirect.ClosePrinter(lhPrinter);
}
}