Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Quick step by step solution</h2> <p>Because code was not provided, I make it work with help of provided links, and here is the code: </p> <h3>Code</h3> <pre><code>using System; using System.Runtime.InteropServices; using System.Windows; [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); } private void Print(String printerAddress, String text, String documentName) { IntPtr printer = new IntPtr(); // A pointer to a value that receives the number of bytes of data that were written to the printer. int pcWritten = 0; DOCINFO docInfo = new DOCINFO(); docInfo.pDocName = documentName; docInfo.pDataType = "RAW"; PrintDirect.OpenPrinter(printerAddress, ref printer, 0); PrintDirect.StartDocPrinter(printer, 1, ref docInfo); PrintDirect.StartPagePrinter(printer); try { PrintDirect.WritePrinter(printer, text, text.Length, ref pcWritten); } catch (Exception e) { Console.WriteLine(e.Message); } PrintDirect.EndPagePrinter(printer); PrintDirect.EndDocPrinter(printer); PrintDirect.ClosePrinter(printer); } </code></pre> <h3>Usage</h3> <pre><code>String printerAddress = "\\\\ComputerName\\PrinterName"; String documentName = "My document"; String documentText = "This is an example of printing directly to a printer."; </code></pre> <p>this.Print(printerAddress, documentText, documentName); </p> <h3>Sources:</h3> <ul> <li><s><a href="http://support.microsoft.com/kb/322091" rel="nofollow noreferrer">How to send raw data to a printer by using Visual C# .NET</a></s> (link is no longer active)</li> <li><a href="https://support.microsoft.com/en-us/help/138594/howto-send-raw-data-to-a-printer-by-using-the-win32-api" rel="nofollow noreferrer">HOWTO: Send Raw Data to a Printer by Using the Win32 API </a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload