title: XPSDocumentWriter - Printing Specific Pages to Specific Trays
body: <p>I'm currently working on a printing application.  This app has the requirement that certain pages need to come from specific trays on the printer.  Here's the guts of what I've got so far:</p>
  
  <pre><code>    foreach (var dto in dispensersToPrint)
      {
          var documents = FilterDocumentSections(DispenserDocumentsToPrint.RetrieveByDispenserId(dto.DispenserId));
          var groupedDocs = documents.GroupBy(t =&gt; 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);
      }
  </code></pre>
  
  <p>At a real high level:</p>
  
  <ul>
  <li>For each Dispenser (Work Order) i need to print; i first start by grouping by the DocumentType (i.e. Print type A to tray 1)</li>
  <li>I then create a new FixedDocumentSequence</li>
  <li>For each DocumentType; I then create a fixed document.  I then modify the print ticket to look at the appropriate tray.</li>
  <li>I then build each individual page for each document type; and add them to the FixedDocument</li>
  <li>Once the building of the FixedDocument is complete; I append it to the DocumentSequence.</li>
  <li>I then send the FixedDocumentSequence to the xpsWriter.</li>
  </ul>
  
  <p>But for some reason; these settings aren't being honored.  I get all the documents printing out of the same tray.</p>
  
  <p>Here are some of my observations so far:</p>
  
  <ul>
  <li>The modifying of the print ticket does work; I've verified this by sending a modified printTicket into the xpsWriter; but this applies the settings to the entire job; which is a no go for me.</li>
  <li>When querying my print capabilities; i noticed that i only have JobInputBin.  I don't quite think this means this printer doesn't support the functionality; as multi-tray printing works from a similar WindowsForms app (which uses PageSettings.PaperSource)</li>
  </ul>
  
  <p>Any ideas on what I could try next?  Has anyone been successful doing something like this before?</p>