2014-02-18 25 views
6

我目前工作的一個打印應用。此應用程序要求某些頁面需要來自打印機上的特定紙盤。下面是我到目前爲止已經鼓起勇氣:XPSDocumentWriter - 打印特定頁面的具體托盤

foreach (var dto in dispensersToPrint) 
    { 
     var documents = FilterDocumentSections(DispenserDocumentsToPrint.RetrieveByDispenserId(dto.DispenserId)); 
     var groupedDocs = documents.GroupBy(t => t.DocumentTypeId); 
     var queueName = Properties.Settings.Default.PrinterName; 
     var queue = RawPrinterHelper.GetPrintQueue(queueName); 
     var seq = new FixedDocumentSequence(); 
     var xpsWriter = PrintQueue.CreateXpsDocumentWriter(queue); 

     foreach (var docGroup in groupedDocs) 
     { 
      var printTicket = queue.DefaultPrintTicket.Clone(); 
      var printTray = MapPrintTray((DocumentSectionType)docGroup.Key); 
      if (!printTray.IsNullOrEmpty()) 
      { 
       printTicket = RawPrinterHelper.ModifyPrintTicket(printTicket, "psk:JobInputBin", printTray); 
      } 
      var fixedDoc = new FixedDocument(); 
      fixedDoc.PrintTicket = printTicket; 

      foreach (var doc in docGroup) 
      { 
       var pageContent = new PageContent(); 
       var fixedPage = new FixedPage(); 

       var localFileName = string.Empty; 
       var unzippedFileName = string.Empty; 
       //copy files locally 
       localFileName = CopyFileToLocalMachine(doc.FileName); 
       //unzip file 
       unzippedFileName = EmfPrintingHelper.UnzipEmfFile(localFileName); 
       var itemToPrint = new PrintableEmfImage 
              { 
               DataContext = new EmfImageViewModel { FileName = unzippedFileName } 
              }; 
       fixedPage.Children.Add(itemToPrint); 
       pageContent.Child = fixedPage; 
       fixedDoc.Pages.Add(pageContent); 
      } 
      var docRef = new DocumentReference(); 
      docRef.SetDocument(fixedDoc); 
      seq.References.Add(docRef); 
     } 
     xpsWriter.Write(seq); 
    } 

在一個真正的高水平:

  • 對於每個分配器(工單)我需要打印;我首先通過DocumentType(即打印類型A到紙盒1)進行分組開始。
  • 然後我創建一個新的FixedDocumentSequence
  • 對於每個DocumentType;然後我創建一個固定的文檔。然後我修改打印票看看適當的托盤。
  • 然後我爲每個文檔類型構建每個單獨的頁面;並將它們添加到固定文檔
  • 一旦固定文檔的建設完成;我將它附加到DocumentSequence。
  • 然後我發送FixedDocumentSequence到xpsWriter。

但出於某種原因;這些設置不被尊重。我將所有打印出來的文檔都放在同一個紙盒中。

下面是我的一些意見至今:

  • 打印票據的修改做工作;我已經通過發送修改後的printTicket到xpsWriter來驗證這一點;但是這將設置應用於整個作業;這對我來說是不行的。
  • 查詢我的打印功能時;我注意到我只有JobInputBin。我不完全認爲這意味着這款打印機不支持該功能;作爲一個類似的WindowsForms應用程序(它使用PageSettings.PaperSource)的多托盤打印作品

關於我可以嘗試下一步的任何想法?有沒有人成功做過這樣的事情?

回答

3

,我會說,我沒有使用帶托盤的打印機開始,讓我遺憾的是沒有能夠測試該解決方案。也就是說,我將把你的注意力引向MSDN論壇帖子,here,其中原始海報追求的是每頁相同的托盤行爲。

根據您發佈的代碼,你可能已經看到了一些什麼在這篇文章中,通過至少具有ModifyPrintTicket()一些實現您發佈的代碼判斷。

在帖子中,有幾種不同的用戶,每一個理由是他們的問題的特定版本的解決方案。然而,在這種情況下最相關的一個是關於名稱空間在ModifyPrintTicket()中未被正確解釋的解決方案(由 Jo0815發佈)。我說'最相關的',因爲海報談到打印托盤被忽視。他們(wittersworld)提供了一個替代實施來糾正這個問題。在MSDN的帖子中,到完整源代碼的鏈接被破壞,但可以找到here

的主旨在於,在ModifyPrintTicket(),他們增加一個namespaceUri參數,然後withing改變了這一點:

if (node != null) 
{ 
    node.Attributes["name"].Value = newValue; 
} 

這樣的:使用

if (node != null) 
{ 
    if (newValue.StartsWith("ns0000")) 
    { 
     // add namespace to xml doc 
     XmlAttribute namespaceAttribute = xmlDoc.CreateAttribute("xmlns:ns0000");     
     namespaceAttribute.Value = namespaceUri;     
     xmlDoc.DocumentElement.Attributes.Append(namespaceAttribute); 
    } 
    node.Attributes["name"].Value = newValue; 
} 

允許用戶指定特定打印機的命名空間。

我希望這是有幫助的。

相關問題