2017-09-10 15 views
5

xpsドキュメントをプリンタ(ネットワークプリンタ、いくつかの仮想ローカルプリンタ、xpsベースおよび非xpsベース)に次のコードで印刷しようとしています。PrintQueue.AddJobが非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ドキュメントライターに印刷する、Microsoftはそれが正常に動作など、PDFに印刷します。

すべてで問題なく動作しています。XPS based printers私はさらにXPSサンプルプリンタドライバをインストールし、この主張を確認する仮想ローカルプリンタを追加し、それが期待通りに機能するようにしました。

非xpsベースのプリンタの場合、実際にはAddJob機能でスタックされます。どちらの例外もスローされず、次のステートメントに移動もしません。

this msdnリソースに基づいてコードを作成しました。

原因と解決方法は何ですか?

すべての考えを歓迎します。

答えて

4

私は問題を見つけることができません。しかし、ここで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にuがしたい任意の印刷キューを挙げることができますプロパティ。

これが役に立ちます。人をお楽しみください!

+0

Thanx!私はまだ根本的な原因を知る必要があります。私はこの答えを今受け入れます。根本的な原因について他の誰かが投稿したら、私はそれらを受け入れます。 –

関連する問題