打印到基于非xps的打印机时,PrintQueue.AddJob挂起 [英] PrintQueue.AddJob hangs when printing to non-xps based printers
问题描述
我正在尝试使用以下代码将xps文档打印到打印机(网络打印机,某些虚拟本地打印机,xps和非xps)。
C#来源:
静态void Main(string [] args)
{
PrintServer printServer =新的PrintServer(@ \\printserver.csez.zohocorpin.com);
foreach(printServer.GetPrintQueues()中的PrintQueue队列)
{
Console.WriteLine(打印机:{0},端口:{1},共享名:{2},状态:{ 3},PrintingIsCancelled:{4},
queue.Name,queue.QueuePort.Name,queue.ShareName,queue.QueueStatus,queue.PrintingIsCancelled);
Program program = new Program();
线程printingThread = new Thread(()=> program.Print_XPXFile(queue,@ D:\Assist\RemotePrint\Spool\Donalduck.xps));
//将将使用PrintQueue.AddJob的线程设置为单线程。
printingThread.SetApartmentState(ApartmentState.STA);
printingThread.Start();
printingThread.Join();
}
}
public void Print_XPXFile(PrintQueue pQueue,String FilePath)
{
//创建打印服务器和打印队列。
bool fastCopy = pQueue.IsXpsDevice;
FileInfo文件= new FileInfo(FilePath);
if(!file.Exists)
{
Console.WriteLine(没有此类文件。);
}
else
{
Console.WriteLine(将{0}添加到{1}队列中。共享名:{2},FilePath,pQueue.Name,pQueue。 ShareName);
试试
{
//在提供XPS验证和进度通知的同时打印Xps文件。
PrintSystemJobInfo xpsPrintJob = pQueue.AddJob(file.Name,FilePath,fastCopy);
Console.WriteLine(完成添加。);
}
catch(PrintJobException e)
{
Console.WriteLine( \n\t {0}无法添加到打印队列。,文件。名称);
if(e.InnerException.Message ==文件包含损坏的数据。)
{
Console.WriteLine( \t这不是有效的XPS文件。); //使用isXPS一致性工具对其进行调试。
}
else
{
Console.WriteLine( \tmessage:{0},e.InnerException.Message);
}
}
}
}
何时
我发现对所有 都可以正常工作。
基于XPS的打印机 。我什至安装了XPS示例打印机驱动程序,并添加了虚拟本地打印机以确认此声明并按预期工作。
对于基于非xps的打印机,它实际上卡在了AddJob函数中。它既不会引发任何异常,也不会移至下一条语句。
我基于此 msdn资源。
原因和解决方案是什么?
欢迎所有想法。
我似乎找不到问题。但是,这是一种打印XPS文件的更有希望的方法:
public static void PrintXPSToDefaultPrinter(string FilePath)
{
try
{
//创建打印对话框对象并设置选项
PrintDialog pDialog = new PrintDialog();
pDialog.PageRangeSelection = PageRangeSelection.AllPages;
pDialog.UserPageRangeEnabled = true;
FileInfo文件= new FileInfo(FilePath);
XpsDocument xpsDocument =新的XpsDocument(FilePath,FileAccess.ReadWrite);
FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();
pDialog.PrintDocument(fixedDocSeq.DocumentPaginator,file.Name);
}
捕获(例如System.IO.IOException)
{
Console.WriteLine(文件正在由其他进程使用。);
}
catch(异常例外)
{
Console.WriteLine(发生异常:{0},例如消息。
}
}
您应该像在STA模式下那样调用它
static void Main(string [] args)
{
线程printingThread = new Thread( ()=> PrintXPSToDefaultPrinter(@ D:\Assist\RemotePrint\Spool\Donalduck.xps));
printingThread.SetApartmentState(ApartmentState.STA);
printingThread.Start();
}
您还可以在中提及您想要的任何打印队列pDialog.PrintQueue
属性。
希望这会有所帮助。享受男人!
I am trying to print an xps document to printers (network printer, some virtual local printers, xps and non xps based) with the following code.
C# Source:
static void Main(string[] args)
{
PrintServer printServer = new PrintServer(@"\\printserver.csez.zohocorpin.com");
foreach (PrintQueue queue in printServer.GetPrintQueues())
{
Console.WriteLine("Printer: {0}, Port: {1}, ShareName: {2}, status: {3}, PrintingIsCancelled: {4}",
queue.Name, queue.QueuePort.Name, queue.ShareName, queue.QueueStatus, queue.PrintingIsCancelled);
Program program = new Program();
Thread printingThread = new Thread(() => program.Print_XPXFile(queue, @"D:\Assist\RemotePrint\Spool\Donalduck.xps"));
// Set the thread that will use PrintQueue.AddJob to single threading.
printingThread.SetApartmentState(ApartmentState.STA);
printingThread.Start();
printingThread.Join();
}
}
public void Print_XPXFile(PrintQueue pQueue, String FilePath)
{
// Create print server and print queue.
bool fastCopy = pQueue.IsXpsDevice;
FileInfo file = new FileInfo(FilePath);
if (!file.Exists)
{
Console.WriteLine("There is no such file.");
}
else
{
Console.WriteLine("Adding {0} to {1} queue. share name : {2}", FilePath, pQueue.Name, pQueue.ShareName);
try
{
// Print the Xps file while providing XPS validation and progress notifications.
PrintSystemJobInfo xpsPrintJob = pQueue.AddJob(file.Name, FilePath, fastCopy);
Console.WriteLine("Done adding.");
}
catch (PrintJobException e)
{
Console.WriteLine("\n\t{0} could not be added to the print queue.", file.Name);
if (e.InnerException.Message == "File contains corrupted data.")
{
Console.WriteLine("\tIt is not a valid XPS file."); // Use the isXPS Conformance Tool to debug it.
}
else
{
Console.WriteLine("\tmessage : {0}", e.InnerException.Message);
}
}
}
}
When printing to Microsoft XPS Document Writer, Microsoft Print to PDF, etc it works fine.
I found that it is working fine with all XPS based printers
. I even installed a XPS sample printer driver and added a virtual local printer to confirm this claim and as expected it worked.
For non-xps based printers, it actually gets stuck in the AddJob function. It neither throws any exception, nor it moves to the next statement.
I developed the code based on this msdn resource.
What is the cause and solution?
All thoughts are welcome.
I can't seem to find the problem. But here is a more promising way to print XPS files:
public static void PrintXPSToDefaultPrinter(string FilePath)
{
try
{
// Create the print dialog object and set options
PrintDialog pDialog = new PrintDialog();
pDialog.PageRangeSelection = PageRangeSelection.AllPages;
pDialog.UserPageRangeEnabled = true;
FileInfo file = new FileInfo(FilePath);
XpsDocument xpsDocument = new XpsDocument(FilePath, FileAccess.ReadWrite);
FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();
pDialog.PrintDocument(fixedDocSeq.DocumentPaginator, file.Name);
}
catch (System.IO.IOException ex)
{
Console.WriteLine("The file is being used by some other process.");
}
catch (Exception ex)
{
Console.WriteLine("Exception occured : {0}", ex.Message);
}
}
You should call this in STA mode just like you have used:
static void Main(string[] args)
{
Thread printingThread = new Thread(() => PrintXPSToDefaultPrinter(@"D:\Assist\RemotePrint\Spool\Donalduck.xps"));
printingThread.SetApartmentState(ApartmentState.STA);
printingThread.Start();
}
You can also mention any printqueue u want in the pDialog.PrintQueue
property.
Hope this helps. Enjoy man !!!
这篇关于打印到基于非xps的打印机时,PrintQueue.AddJob挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!