I am trying to print an xps document for printers (network printer, some virtual local printers, xps and non xps) 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 great.
I found that it works fine with all XPS based printers . I even installed the XPS sample printer driver and added a virtual local printer to confirm this statement and as expected, it worked.
For non-xps printers, it actually gets stuck in the AddJob function. It does not generate any exceptions and does not proceed to the next statement.
I developed code based on this msdn resource.
What is the reason and solution?
All thoughts are welcome.