using System; using System.IO; using System.Runtime.InteropServices; using System.Collections.Generic; using System.Text; namespace Tanshu.Accounts.Print { internal class RawPrinterHelper { // Cut Code for the printer Alt-29, Alt-86, Alt-49 #region API Declarations // Structure and API declarions: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] internal 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)] internal 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)] internal static extern bool ClosePrinter(IntPtr hPrinter); [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] internal 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)] internal static extern bool EndDocPrinter(IntPtr hPrinter); [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] internal static extern bool StartPagePrinter(IntPtr hPrinter); [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] internal static extern bool EndPagePrinter(IntPtr hPrinter); [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] internal static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten); #endregion internal static bool PrintString(string printerName, string documentName, string printingText, char[] lineBreakChars) { string[] PageText; IntPtr hPrinter = new IntPtr(0); // The printer handle. Int32 dwError; // Last error -incase there was trouble. DOCINFOA di = new DOCINFOA(); // Describes your document(name, port, data type). bool bSuccess = false; // Your success code. // Set up the DOCINFO structure. di.pDocName = documentName; di.pDataType = "RAW"; PageText = printingText.Split(lineBreakChars ); if (OpenPrinter(printerName.Normalize(), out hPrinter, System.IntPtr.Zero)) { if (StartDocPrinter(hPrinter, 1, di)) { for (int i = 0; i < PageText.Length; i++) { if (StartPagePrinter(hPrinter)) { //Write your printer - specific bytes to the printer. bSuccess = SendLine(PageText[i], hPrinter); 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; }// SendBytesToPrinter() internal static bool SendLine(string szString, IntPtr hPrinter) { bool bSuccess = false; Int32 dwWritten = 0; Int32 dwCount = szString.Length; // How many characters are in the string? IntPtr pBytes = Marshal.StringToCoTaskMemAnsi(szString); // Assume that the printer is expecting ANSI text, and then convert // the string to ANSI text. // Write your bytes. bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); return bSuccess; } } }