2017-09-10 36 views
5

Tôi đang cố gắng in tài liệu xps vào máy in (máy in mạng, một số máy in cục bộ ảo, xps và xps không dựa) với mã sau.PrintQueue.AddJob treo khi in tới máy in không phải xps

C# Nguồn:

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); 
      } 
     } 
    } 
} 

Khi in cho Microsoft XPS Document Writer, Microsoft In sang PDF, vv nó hoạt động tốt.

Tôi thấy rằng nó hoạt động tốt với tất cả XPS based printers. Tôi thậm chí đã cài đặt trình điều khiển máy in mẫu XPS và thêm máy in cục bộ ảo để xác nhận xác nhận quyền sở hữu này và như mong đợi nó hoạt động.

Đối với máy in không dựa trên xps, nó thực sự bị kẹt trong hàm AddJob. Nó không ném bất kỳ ngoại lệ nào, và nó cũng không chuyển sang câu lệnh tiếp theo.

Tôi đã phát triển mã dựa trên tài nguyên this msdn.

Nguyên nhân và giải pháp là gì?

Tất cả các ý tưởng đều được chào đón.

Trả lời

4

Tôi dường như không thể tìm thấy sự cố. Nhưng đây là một cách đầy hứa hẹn hơn để in các tập tin 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); 
      } 
     } 

Bạn nên gọi này ở chế độ STA giống như bạn đã sử dụng:

static void Main(string[] args) 
{ 
     Thread printingThread = new Thread(() => PrintXPSToDefaultPrinter(@"D:\Assist\RemotePrint\Spool\Donalduck.xps")); 
     printingThread.SetApartmentState(ApartmentState.STA); 
     printingThread.Start(); 
} 

Bạn cũng có thể đề cập đến bất kỳ printqueue u muốn trong pDialog.PrintQueue bất động sản.

Hy vọng điều này sẽ hữu ích. Hãy tận hưởng người đàn ông !!!

+0

Thanx! Tôi vẫn cần phải biết nguyên nhân gốc rễ. Tôi sẽ chấp nhận câu trả lời này ngay bây giờ. Khi bất cứ ai khác đăng bài về nguyên nhân gốc rễ, tôi sẽ chấp nhận họ. –

Các vấn đề liên quan