// 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, out hPrinter, 0 ) ) { // 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; }
// 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. private 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 = "Labels"; di.pDataType = "RAW"; if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)) { if (StartDocPrinter(hPrinter, 1, di)) { if (StartPagePrinter(hPrinter)) { 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 SendBytesToPrinter(string szPrinterName, IntPtr pBytes, int dwCount) { IntPtr hPrinter = new IntPtr(0); DOCINFOA di = new DOCINFOA(); bool flag = false; di.pDocName = "My C#.NET RAW Document"; di.pDataType = "RAW"; if (OpenPrinter(szPrinterName.ToString(), out hPrinter, IntPtr.Zero)) { if (StartDocPrinter(hPrinter, 1, di)) { if (StartPagePrinter(hPrinter)) { int dwWritten = 0; flag = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } if (!flag) { Marshal.GetLastWin32Error(); } return flag; }
private static bool SendBytesToPrinter(string printerName, string documentName, IntPtr data, Int32 length) { var printerHandle = new IntPtr(0); var documentInfo = new DOCINFOA(); bool success = false; documentInfo.pDocName = documentName; documentInfo.pDataType = "RAW"; if (OpenPrinter(printerName.Normalize(), out printerHandle, IntPtr.Zero)) { if (StartDocPrinter(printerHandle, 1, documentInfo)) { if (StartPagePrinter(printerHandle)) { Int32 dwWritten = 0; success = WritePrinter(printerHandle, data, length, out dwWritten); EndPagePrinter(printerHandle); } EndDocPrinter(printerHandle); } ClosePrinter(printerHandle); } Int32 dwError = 0; if (success == false) dwError = Marshal.GetLastWin32Error(); return success; }
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, int dwCount) { int dwError = 0;int dwWritten = 0;IntPtr hPrinter = new IntPtr(0); DOCINFOA di = new DOCINFOA(); bool bSuccess = false; di.pDocName = "My C#.NET RAW Document"; di.pDataType = "RAW"; if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)) { if (StartDocPrinter(hPrinter, 1, di)) { if (StartPagePrinter(hPrinter)) { bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } if (!bSuccess) { dwError = Marshal.GetLastWin32Error(); } return bSuccess; }
public static IntPtr GetPrinter(string szPrinterName) { var di = new DOCINFOA { pDocName = "Samba POS Document", pDataType = "RAW" }; IntPtr hPrinter; if (!OpenPrinter(szPrinterName, out hPrinter, IntPtr.Zero)) BombWin32(); if (!StartDocPrinter(hPrinter, 1, di)) BombWin32(); if (!StartPagePrinter(hPrinter)) BombWin32(); return hPrinter; }
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"; if (OpenPrinter(szPrinterName, out hPrinter, 0)) { if (StartDocPrinter(hPrinter, 1, di)) { if (StartPagePrinter(hPrinter)) { bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } if (bSuccess == false) { dwError = GetLastError(); } return bSuccess; }
// 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) { IntPtr hPrinter; var di = new DOCINFOA(); var bSuccess = false; // Assume failure unless you specifically succeed. di.pDocName = Assembly.GetExecutingAssembly().GetName().Name + " Document"; //di.pDataType = "RAW"; //leaving this null means it can be specified at the printer definition... //this facilitates having RAW for the true Epson printer and TEXT for the Generic / Text Only printer //for whatever reason the Generic printer will only print to "FILE:" when the TEXT "print processor" has been chosen //(under Control Panel > Printers > your printer > Printer properties > Advanced > Print processor) //and print to file is simply a convenient way to check that the basic print logic is working w/o wasting paper //what's even more whacked out is that when you do print to file, on Win7, the "Interactive Services Detection" will popup //to provide the GUI for entering the filename to output to //references: http://blogs.msdn.com/b/patricka/archive/2010/04/27/what-is-interactive-services-detection-and-why-is-it-blinking-at-me.aspx //http://s309.codeinspot.com/q/1071364 // 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. int dwWritten; 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) { Marshal.GetLastWin32Error(); } return bSuccess; }
public static bool DoAllNoInit(string szPrinterName) { Int32 dwError = 0, dwWritten = 0, dwWritten1 = 0, dwWritten2 = 0; IntPtr hPrinter = new IntPtr(0); DOCINFOA di = new DOCINFOA(); bool bSuccess = false; bool bSuccess1 = false; bool bSuccess2 = false; di.pDocName = "DoAllNoInit"; di.pDataType = "RAW"; if (OpenPrinter(szPrinterName, out hPrinter, 0)) { if (StartDocPrinter(hPrinter, 1, di)) { if (StartPagePrinter(hPrinter)) { //int nLength; int nLength1; int nLength2; char V = '@'; char V1 = 'V'; byte[] DrawerOpen = { 0x1d, Convert.ToByte(V) }; byte[] DrawerOpen1 = { 0x1d, Convert.ToByte(V), 66, 0 }; byte[] DrawerOpen2 = { 0x1B, 0x70, 0x0, 60, 120 }; //nLength = DrawerOpen.Length; nLength1 = DrawerOpen1.Length; nLength2 = DrawerOpen2.Length; //IntPtr p = Marshal.AllocCoTaskMem(nLength); IntPtr p1 = Marshal.AllocCoTaskMem(nLength1); IntPtr p2 = Marshal.AllocCoTaskMem(nLength2); //Marshal.Copy(DrawerOpen, 0, p, nLength); Marshal.Copy(DrawerOpen1, 0, p1, nLength1); Marshal.Copy(DrawerOpen2, 0, p2, nLength2); //bSuccess = WritePrinter(hPrinter, p, DrawerOpen.Length, out dwWritten); bSuccess1 = WritePrinter(hPrinter, p1, DrawerOpen1.Length, out dwWritten1); bSuccess2 = WritePrinter(hPrinter, p2, DrawerOpen2.Length, out dwWritten2); EndPagePrinter(hPrinter); //Marshal.FreeCoTaskMem(p); Marshal.FreeCoTaskMem(p1); Marshal.FreeCoTaskMem(p2); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } if (bSuccess == false) { dwError = GetLastError(); } return(bSuccess); }
public static bool PrintMyOwn(string szPrinterName) { Int32 dwError = 0, dwWritten = 0; IntPtr hPrinter = new IntPtr(0); DOCINFOA di = new DOCINFOA(); bool bSuccess = false; di.pDocName = "OpenDrawer"; di.pDataType = "RAW"; if (OpenPrinter(szPrinterName, out hPrinter, 0)) { if (StartDocPrinter(hPrinter, 1, di)) { if (StartPagePrinter(hPrinter)) { int nLength; byte[] DrawerOpen = new byte[] { 07 }; nLength = DrawerOpen.Length; IntPtr p = Marshal.AllocCoTaskMem(nLength); Marshal.Copy(DrawerOpen, 0, p, nLength); bSuccess = WritePrinter(hPrinter, p, DrawerOpen.Length, out dwWritten); EndPagePrinter(hPrinter); Marshal.FreeCoTaskMem(p); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } if (bSuccess == false) { dwError = GetLastError(); } return(bSuccess); }
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, DOCINFOA di);
public static bool ReadFromPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount) { // Read the data from the printer. Int32 dwError = 0; var hPrinter = new IntPtr(0); Int32 dwBytesRead = 0; var di = new DOCINFOA(); int bSuccess = -1; // 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)) { // read your bytes. bSuccess = ReadPrinter(hPrinter, out pBytes, dwCount, out dwBytesRead); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } //If you did not succeed, GetLastError may give more information about why not. if (bSuccess != 0) { dwError = Marshal.GetLastWin32Error(); return false; } return true; }
public static bool FullCut(string szPrinterName, byte i) { Int32 dwError = 0, dwWritten = 0; IntPtr hPrinter = new IntPtr(0); DOCINFOA di = new DOCINFOA(); bool bSuccess = false; di.pDocName = "FullCut"; di.pDataType = "RAW"; if (OpenPrinter(szPrinterName, out hPrinter, 0)) { if (StartDocPrinter(hPrinter, 1, di)) { if (StartPagePrinter(hPrinter)) { int nLength; //byte[] DrawerOpen = new byte[] { 27, 100, 51 }; byte[] DrawerOpen = new byte[] { i }; nLength = DrawerOpen.Length; IntPtr p = Marshal.AllocCoTaskMem(nLength); Marshal.Copy(DrawerOpen, 0, p, nLength); bSuccess = WritePrinter(hPrinter, p, DrawerOpen.Length, out dwWritten); EndPagePrinter(hPrinter); Marshal.FreeCoTaskMem(p); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } if (bSuccess == false) { dwError = GetLastError(); } return bSuccess; }
private static void SendBytesToPrinter(string szPrinterName, IntPtr pBytes, int dwCount, DOCINFOA documentInfo, out int jobId) { jobId = 0; var dwWritten = 0; var success = false; if (OpenPrinter(szPrinterName.Normalize(), out var hPrinter, IntPtr.Zero)) { jobId = StartDocPrinter(hPrinter, 1, documentInfo); if (jobId > 0) { if (StartPagePrinter(hPrinter)) { success = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } // TODO: The other methods such as OpenPrinter also have return values. Check those? if (success == false) { var dwError = Marshal.GetLastWin32Error(); throw new Exception($"Printing to PDF failed. Error code: {dwError}."); } if (dwWritten != dwCount) { throw new Exception("Printing to PDF failed. Could not write all bytes."); } }
private static extern int StartDocPrinter(IntPtr hPrinter, int level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
private static int SendBytesToPrinter(string szPrinterName, IntPtr pBytes, int dwCount, DOCINFOA documentInfo, out int jobId) { jobId = 0; var dwWritten = 0; var success = false; if (OpenPrinter(szPrinterName.Normalize(), out var hPrinter, IntPtr.Zero)) { jobId = StartDocPrinter(hPrinter, 1, documentInfo); if (jobId > 0) { if (StartPagePrinter(hPrinter)) { success = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } // TODO: The other methods such as OpenPrinter also have return values. Check those? if (success == false) { return(Marshal.GetLastWin32Error()); } return(0); }
// 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, string szDocumentName, IntPtr pBytes, Int32 dwCount, string printDataType = "RAW") { 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 = szDocumentName; // List of Data Types // RAW // TEXT // RAW [FF appended] // RAW [FF auto] // NT EMF 1.00x di.pDataType = printDataType; // 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; }
private static extern bool StartDocPrinter(IntPtr hPrinter, int level, [MarshalAs(UnmanagedType.LPStruct), In] DOCINFOA di);
public static extern bool StartDocPrinter( IntPtr printer, int level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA documentInfo);
private static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, int dwCount, DOCINFOA documentInfo) { var dwWritten = 0; var success = false; if (OpenPrinter(szPrinterName.Normalize(), out var hPrinter, IntPtr.Zero)) { if (StartDocPrinter(hPrinter, 1, documentInfo)) { if (StartPagePrinter(hPrinter)) { success = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } // If print did not succeed, GetLastError may give more information about the failure. if (success == false) { var dwError = Marshal.GetLastWin32Error(); throw new Exception($"Printing to PDF failed. Error code: {dwError}."); } return(success); }
public static bool FullCut(string szPrinterName) { Int32 dwError = 0, dwWritten = 0; IntPtr hPrinter = new IntPtr(0); DOCINFOA di = new DOCINFOA(); bool bSuccess = false; di.pDocName = "FullCut"; di.pDataType = "RAW"; if (OpenPrinter(szPrinterName, out hPrinter, 0)) { if (StartDocPrinter(hPrinter, 1, di)) { if (StartPagePrinter(hPrinter)) { int nLength; //byte[] DrawerOpen = new byte[] { 27, 100, 51 }; char V = 'V'; byte[] DrawerOpen = { 0x1d, Convert.ToByte(V), 66, 0 }; nLength = DrawerOpen.Length; IntPtr p = Marshal.AllocCoTaskMem(nLength); Marshal.Copy(DrawerOpen, 0, p, nLength); bSuccess = WritePrinter(hPrinter, p, DrawerOpen.Length, out dwWritten); EndPagePrinter(hPrinter); Marshal.FreeCoTaskMem(p); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } if (bSuccess == false) { dwError = GetLastError(); } return(bSuccess); }
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
public bool Open(string DocName) { // see if printer is already open if(HandlePrinter!=IntPtr.Zero) return false; // opens the printer bool risp = OpenPrinter(ps.PrinterName,out HandlePrinter,IntPtr.Zero); if(risp==false) return false; // starts a print job DOCINFOA MyDocInfo = new DOCINFOA(); MyDocInfo.pDocName = DocName; MyDocInfo.pOutputFile = null; MyDocInfo.pDataType = "RAW"; if(StartDocPrinter(HandlePrinter, 1, MyDocInfo)) { StartPagePrinter(HandlePrinter); //starts a page return true; } else return false; }
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount, string Nom_Producto) { Int32 dwError = 0, dwWritten = 0; IntPtr hPrinter = new IntPtr(0); DOCINFOA di = new DOCINFOA(); bool bSuccess = false; di.pDocName = "Etiqueta " + Nom_Producto; di.pDataType = "RAW"; // Abra la impresora. if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)) { // Inicie un documento. if (StartDocPrinter(hPrinter, 1, di)) { // Inicia página. if (StartPagePrinter(hPrinter)) { // Escribe(Imprime). bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } if (bSuccess == false) { dwError = Marshal.GetLastWin32Error(); Util.ShowError("Error al conectar a la impresora " + szPrinterName); } return bSuccess; }
public static byte[] ReadBytesFromPrinter(string szPrinterName, byte[] statusRequest) { bool bSuccess = false; IntPtr hPrinter = new IntPtr(0); IntPtr hPrintJob = new IntPtr(0); byte[] status = new byte[0]; // Open the printer. if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)) { // Start a document. DOCINFOA di = new DOCINFOA() { pDocName = "Read RAW PrintDocument", pDataType = "RAW" }; Int32 jobId = StartDocPrinter(hPrinter, 1, di); if (jobId > 0) { // We can't read from a printer handle, but we can read from a printer job handle, // So the trick is to create a Job using StartDocPrinter, then open a handle to the printer job... string jobName = string.Format("{0}, Job {1}", szPrinterName, jobId); if (OpenPrinter(jobName.Normalize(), out hPrintJob, IntPtr.Zero)) { // Start a page and print the Status-Request Sequence if (StartPagePrinter(hPrinter)) { { Int32 dwCount = statusRequest.Length; IntPtr pStatusRequest = Marshal.AllocCoTaskMem(dwCount); Marshal.Copy(statusRequest, 0, pStatusRequest, dwCount); Int32 dwWritten = 0; bSuccess = WritePrinter(hPrinter, pStatusRequest, dwCount, out dwWritten); EndPagePrinter(hPrinter); EndDocPrinter(hPrinter); // EndPage and EndDoc here, otherwise ReadPrinter ist always null Marshal.FreeCoTaskMem(pStatusRequest); } if (bSuccess) { //read request from "Job Handle" Int32 bufLen = 32; IntPtr pStatus = Marshal.AllocCoTaskMem(bufLen); Int32 statusLen = 0; bSuccess = ReadPrinter(hPrintJob, pStatus, bufLen, out statusLen); int err = Marshal.GetLastWin32Error(); // Sometimes is error 0x3F : Your file waiting to be printed was deleted. status = new byte[statusLen]; if (statusLen > 0) { Marshal.Copy(pStatus, status, 0, statusLen); } Marshal.FreeCoTaskMem(pStatus); } } ClosePrinter(hPrintJob); } } ClosePrinter(hPrinter); } return(status); }
/// <summary> /// This function gets the printer name and an unmanaged array of bytes, the function sends those bytes to the print queue. /// </summary> /// <param name="szPrinterName">Printer Name</param> /// <param name="pBytes">No. of bytes in the pdf file</param> /// <param name="dwCount">Word count</param> /// <returns>True on success, false on failure</returns> private static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount) { try { Int32 dwError = 0, dwWritten = 0; IntPtr hPrinter = new IntPtr(0); DOCINFOA di = new DOCINFOA(); bool success = false; // Assume failure unless you specifically succeed. di.pDocName = "PDF 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 the bytes. success = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } // If print did not succeed, GetLastError may give more information about the failure. if (success == false) { dwError = Marshal.GetLastWin32Error(); } return success; } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// Sends the bytes to printer. /// </summary> /// <param name="szPrinterName">Name of the sz printer.</param> /// <param name="pBytes">The p bytes.</param> /// <param name="dwCount">The dw count.</param> /// <returns></returns> public static bool SendBytesToPrinter( string szPrinterName, IntPtr pBytes, Int32 dwCount) { Int32 dwError = 0, dwWritten = 0; var hPrinter = new IntPtr(0); var di = new DOCINFOA(); bool bSuccess = false; di.pDocName = "QWFP - Print Nomor Antrian"; di.pDataType = "RAW"; if (OpenPrinter( szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)) { if (StartDocPrinter(hPrinter, 1, di)) { if (StartPagePrinter(hPrinter)) { bSuccess = WritePrinter( hPrinter, pBytes, dwCount, out dwWritten); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } if (bSuccess == false) { dwError = Marshal.GetLastWin32Error(); } return bSuccess; }
public static void PrintXpsToPdf(byte[] bytes, string outputFilePath, string documentTitle) { // Get Microsoft Print to PDF print queue var pdfPrintQueue = GetMicrosoftPdfPrintQueue(); // Copy byte array to unmanaged pointer var ptrUnmanagedBytes = Marshal.AllocCoTaskMem(bytes.Length); Marshal.Copy(bytes, 0, ptrUnmanagedBytes, bytes.Length); // Prepare document info var di = new DOCINFOA { pDocName = documentTitle, pOutputFile = outputFilePath, pDataType = "RAW" }; // Print to PDF var errorCode = SendBytesToPrinter(pdfPrintQueue.Name, ptrUnmanagedBytes, bytes.Length, di, out var jobId); // Free unmanaged memory Marshal.FreeCoTaskMem(ptrUnmanagedBytes); // Check if job in error state (for example not enough disk space) var jobFailed = false; try { var pdfPrintJob = pdfPrintQueue.GetJob(jobId); if (pdfPrintJob.IsInError) { jobFailed = true; pdfPrintJob.Cancel(); } } catch { // If job succeeds, GetJob will throw an exception. Ignore it. } finally { pdfPrintQueue.Dispose(); } if (errorCode > 0 || jobFailed) { try { if (File.Exists(outputFilePath)) { File.Delete(outputFilePath); } } catch { // ignored } } if (errorCode > 0) { throw new Exception($"Printing to PDF failed. Error code: {errorCode}."); } if (jobFailed) { throw new Exception("PDF Print job failed."); } }
//--------------------------------------------------------------------// // M e t h o d // // s e n d D a t a // //--------------------------------------------------------------------// // // // Send data in provided stream to specified printer port. // // // //--------------------------------------------------------------------// public static Int32 sendData(BinaryReader prnReader, String printerName) { Int32 result = 0; Int32 dwError = 0, dwWritten = 0; IntPtr hPrinter = new IntPtr(0); DOCINFOA di = new DOCINFOA(); Boolean bSuccess = true; di.pDocName = "PCLParaphernalia RAW Document"; //----------------------------------------------------------------// // // // Open the printer. // // // //----------------------------------------------------------------// if (OpenPrinter(printerName.Normalize(), out hPrinter, IntPtr.Zero)) { //------------------------------------------------------------// // // // Check if XPS printer driver or not // // // //------------------------------------------------------------// if (isDriverXPS(hPrinter)) { di.pDataType = "XPS_PASS"; } else { di.pDataType = "RAW"; } //------------------------------------------------------------// // // // Start a document. // // // //------------------------------------------------------------// if (StartDocPrinter(hPrinter, 1, di)) { //--------------------------------------------------------// // // // Start a page. // // // //--------------------------------------------------------// if (StartPagePrinter(hPrinter)) { //----------------------------------------------------// // // // Read contents of supplied stream and send to // // windows printer. // // // //----------------------------------------------------// const Int32 bufLen = 512; Int32 readLen; Byte[] prnData = new Byte[bufLen]; prnReader.BaseStream.Position = 0; prnData = prnReader.ReadBytes(bufLen); while (((readLen = prnData.Length) != 0) && (bSuccess)) { IntPtr pUnmanagedBytes = new IntPtr(0); pUnmanagedBytes = Marshal.AllocCoTaskMem(readLen); Marshal.Copy(prnData, 0, pUnmanagedBytes, readLen); //------------------------------------------------// // // // Send the unmanaged bytes to the printer. // // // //------------------------------------------------// bSuccess = WritePrinter(hPrinter, pUnmanagedBytes, readLen, out dwWritten); prnData = prnReader.ReadBytes(bufLen); //------------------------------------------------// // // // Free the unmanaged memory and exit. // // // //------------------------------------------------// Marshal.FreeCoTaskMem(pUnmanagedBytes); } //----------------------------------------------------// // // // End page. // // // //----------------------------------------------------// EndPagePrinter(hPrinter); } //--------------------------------------------------------// // // // End document. // // // //--------------------------------------------------------// EndDocPrinter(hPrinter); } //------------------------------------------------------------// // // // Close the printer. // // // //------------------------------------------------------------// ClosePrinter(hPrinter); } if (bSuccess == false) { //------------------------------------------------------------// // // // If write did not succeed, GetLastError may give more // // information about the failure. // // // //------------------------------------------------------------// dwError = Marshal.GetLastWin32Error(); } return(result); }
//--------------------------------------------------------------------// // M e t h o d // // s e n d B y t e s T o P r i n t e r // //--------------------------------------------------------------------// // // // 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(); Boolean bSuccess = false; 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 supplied bytes. // // // //----------------------------------------------------// bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); //----------------------------------------------------// // // // End page. // // // //----------------------------------------------------// EndPagePrinter(hPrinter); } //--------------------------------------------------------// // // // End document. // // // //--------------------------------------------------------// EndDocPrinter(hPrinter); } //------------------------------------------------------------// // // // Close the printer. // // // //------------------------------------------------------------// ClosePrinter(hPrinter); } if (bSuccess == false) { //------------------------------------------------------------// // // // If write did not succeed, GetLastError may give more // // information about the failure. // // // //------------------------------------------------------------// dwError = Marshal.GetLastWin32Error(); } return(bSuccess); }
private static void OpenDrawerByPrinter() { //Settings settings = new Settings(); //settings.getValuesFromRegistry(); string PrinterName=""; IntPtr hPrinter = new IntPtr(0); if (OpenPrinter(PrinterName, out hPrinter, 0)) { Int32 dwWritten = 0; DOCINFOA docinfo = new DOCINFOA(); docinfo.pDocName = "CashPop"; if (StartDocPrinter(hPrinter, 1, docinfo)) { if (StartPagePrinter(hPrinter)) { IntPtr pBytesCommandOpenDrawer = Marshal.UnsafeAddrOfPinnedArrayElement(commandOpenDrawer, 0); WritePrinter(hPrinter, pBytesCommandOpenDrawer, commandOpenDrawer.Length, out dwWritten); Marshal.FreeCoTaskMem(pBytesCommandOpenDrawer); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } }