帮助
我们的应用程序一直使用ProcessStart逻辑打印PDF文档,但现在它已停止工作。
所以我被赋予了寻找替代方案的任务,我认为我有。 以下代码适用于我们所有的网络打印机,但是当我将PDF文档发送到本地USB打印机时,它不想打印。
文档位于打印机队列中,用于本地打印机并说打印,但打印机不想打印它。如果我将另一个文档发送到同一台打印机,它将打印新文档(在代码之外,使用记事本),然后PDF打印项目消失。
我已经走遍了网络,我似乎四处走动,但我似乎无法找到一个适合我们问题的答案。
示例堆栈流量修复,不起作用 How to send a raw ZPL to zebra printer using C# via USB
我正在使用的代码
public class RawPrinterHelper
{
// Structure and API declarions:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public 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)]
public 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)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public 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)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
private static string documentName = "HO Document";
// 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 = documentName;
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 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 void trackPrint(string printerName)
{
dlgProgressStatus statusBar = new dlgProgressStatus();
statusBar.SetDefault("Document Printing Tracking ","", 4);
statusBar.SetStatus("", false);
statusBar.Show();
statusBar.Refresh();
Stopwatch timeTaken = new Stopwatch();
timeTaken.Start();
Utilities.DebugSolution.SetMessageUniqueFile("Printing", "Tracking printing");
while (!Utilities.Printer.JobOnQueue(printerName, documentName, ref statusBar))
{
Utilities.DebugSolution.SetMessageUniqueFile("Printing", "Waiting for Job on Queue");
System.Threading.Thread.Sleep(100);
if (timeTaken.Elapsed.Minutes > 1)
{
MessageBox.Show(LanguageHelper.GetString("dlgPDFViewer.PrintingQueueTimeOut"), "Health Options®", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
}
statusBar.Close();
statusBar.Dispose();
}
public static bool SendFileToPrinter(string szPrinterName, string szFileName, string printingDocumentName)
{
Utilities.DebugSolution.SetMessageUniqueFile("Printing", "Sending to Printer");
Utilities.DebugSolution.SetMessageUniqueFile("Printing", "File path: " + szFileName);
Utilities.DebugSolution.SetMessageUniqueFile("Printing", "Exisits: " + File.Exists(szFileName).ToString());
Utilities.DebugSolution.SetMessageUniqueFile("Printing", "Printer Name: " + szPrinterName);
if (!String.IsNullOrEmpty(printingDocumentName))
documentName = printingDocumentName;
// Open the file.
using (FileStream fs = new FileStream(szFileName, FileMode.Open))
{
// Create a BinaryReader on the file.
BinaryReader br = new BinaryReader(fs);
// Dim an array of bytes big enough to hold the file's contents.
Byte[] bytes = new Byte[fs.Length];
bool bSuccess = false;
// Your unmanaged pointer.
IntPtr pUnmanagedBytes = new IntPtr(0);
int nLength;
nLength = Convert.ToInt32(fs.Length);
// Read the contents of the file into the array.
bytes = br.ReadBytes(nLength);
// Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes);
trackPrint(szPrinterName);
//Remove temp file
System.GC.Collect();
return bSuccess;
}
}
public static bool SendStringToPrinter(string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
// How many characters are in the string?
dwCount = szString.Length;
// Assume that the printer is expecting ANSI text, and then convert
// the string to ANSI text.
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
// Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}