2017-09-10 85 views
5

मैं निम्नलिखित कोड के साथ प्रिंटर (नेटवर्क प्रिंटर, कुछ वर्चुअल स्थानीय प्रिंटर, एक्सपीएस और गैर एक्सपीएस आधारित) पर एक एक्सपीएस दस्तावेज़ मुद्रित करने की कोशिश कर रहा हूं।प्रिंटक्यूयू.एडजोब गैर-एक्सपीएस आधारित प्रिंटर पर प्रिंट करते समय लटकता है

सी # स्रोत:

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 दस्तावेज़ लेखक पर प्रिंट करते हैं, माइक्रोसॉफ्ट PDF में प्रिंट, आदि यह ठीक काम करता है।

मुझे पता चला कि यह सभी XPS based printers के साथ ठीक काम कर रहा है। मैंने एक एक्सपीएस नमूना प्रिंटर ड्राइवर भी स्थापित किया और इस दावे की पुष्टि करने के लिए वर्चुअल लोकल प्रिंटर जोड़ा और उम्मीद की कि यह काम करता है।

गैर-एक्सपीएस आधारित प्रिंटर, यह वास्तव में AddJob फ़ंक्शन में फंस जाता है। यह न तो कोई अपवाद फेंकता है, न ही यह अगले बयान में चला जाता है।

मैंने this msdn संसाधन के आधार पर कोड विकसित किया।

कारण और समाधान क्या है?

सभी विचारों का स्वागत है।

उत्तर

4

मुझे समस्या नहीं लग रही है।

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

आप एसटीए मोड में इस फोन करना चाहिए, जैसा कि आपने इस्तेमाल किया है::

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

आप किसी भी printqueue यू pDialog.PrintQueue में चाहते उल्लेख कर सकते हैं लेकिन यहाँ एक अधिक आशाजनक तरीका एक्सपीएस फ़ाइलों को मुद्रित करने के लिए है संपत्ति।

उम्मीद है कि इससे मदद मिलती है। आदमी का आनंद लें !!!

+0

Thanx! मुझे अभी भी मूल कारण जानने की जरूरत है। मैं अभी इस जवाब को स्वीकार करूंगा। जब कोई अन्य मूल कारण के बारे में पोस्ट करता है, तो मैं उनका स्वीकार करूंगा। –

संबंधित मुद्दे