2017-09-10 76 views
5

Aşağıdaki kodla yazıcılara (ağ yazıcısı, bazı sanal yerel yazıcılar, xps ve xps olmayan) bir xps belgesini yazdırmaya çalışıyorum.PrintQueue.AddJob, xps tabanlı olmayan yazıcılara yazdırırken kilitleniyor

C# Kaynak:

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 Belge Yazıcısı için baskı Microsoft iyi çalışıyor vb PDF olarak yazdır.

Tüm XPS based printers ile çalışmakta olduğunu buldum. Hatta bir XPS örnek yazıcı sürücüsü kurdum ve bu iddiayı doğrulamak için ve beklendiği gibi sanal bir yerel yazıcı ekledim.

xps tabanlı olmayan yazıcılar için, aslında AddJob işlevinde sıkışır. Ne istisna atar ne de bir sonraki ifadeye geçer.

Kodu, this msdn kaynağına dayalı olarak geliştirdim.

Sebep ve çözüm nedir?

Tüm düşünceler açığız.

cevap

4

Sorunu bulamıyorum.

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

Sen kullanmış gibi STA modunda bu çağırmalıdır:

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

Ayrıca u pDialog.PrintQueue istediğiniz herhangi YazdırmaKuyruğu bahsedebiliriz Ama burada XPS dosyaları yazdırmak için daha ümit verici bir yoldur özelliği.

Bu yardımcı olur umarım. Adamın tadını çıkar!

+0

Thanx! Hala kök nedenini bilmem gerek. Bu cevabı şimdilik kabul edeceğim. Kök neden hakkında başka biri gönderdiğinde, onların kabulünü kabul edeceğim. –

İlgili konular