我正在尝试使用以下代码将xps文档打印到打印机(网络打印机,某些虚拟本地打印机,基于xps和非xps)。
C#来源:
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);
}
}
}
}
打印到Microsoft XPS Document Writer,Microsoft Print to PDF等时,它可以正常工作。
我发现它适用于所有 XPS based printers
。我甚至安装了一个XPS样本打印机驱动程序并添加了一个虚拟本地打印机来确认这个声明,并且正如预期的那样有效。
对于非基于xps的打印机,它实际上会卡在AddJob函数中。它既不抛出任何异常,也不会转移到下一个语句。
我基于this msdn资源开发了代码。
原因和解决方案是什么?
欢迎所有的想法。
答案 0 :(得分:6)
我似乎无法找到问题所在。但是这是一种更有前途的打印XPS文件的方法:
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);
}
}
您应该像使用过一样在STA模式下调用它:
static void Main(string[] args)
{
Thread printingThread = new Thread(() => PrintXPSToDefaultPrinter(@"D:\Assist\RemotePrint\Spool\Donalduck.xps"));
printingThread.SetApartmentState(ApartmentState.STA);
printingThread.Start();
}
您还可以在pDialog.PrintQueue
属性中提及您想要的任何印刷品。
希望这会有所帮助。享受男人!
答案 1 :(得分:1)
我也被困在AddJob()方法中。随着Windows 10平台的参与,这个问题似乎更加普遍。
在调试中断开执行,堆栈跟踪显示STA线程在调用
时被阻止<强> MS.Internal.PrintWin32Thunk.XpsCompatiblePrinter.JobIdentifier.get 强>
这在一些未知同步对象上的低级WaitOne()上进一步阻塞。
即使这个问题的细节很薄(这是我在这个主题上找到的唯一一篇文章),但是感谢所接受的解决方案非常好(我已经尝试了多年来在WPF中打印各种各样的东西)。
AddJob()在所有方面都完全替换。甚至可以通过PrintDialog更轻松地控制PrintTicket(设置纸张尺寸,方向,单面/双面打印等)