witters:World

see martin code

About the author

Martin Witters is a software developer at Xpress Technologies in Jacksonville, FL.
E-mail me Send mail

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009

Selecting the input tray when printing XPS documents

There are many examples on the web of how to print an XPS document, and for the most part they are relatively simple.  But when my application needed to do something crazy (like...I don't know...tell the document which tray to print to), the myriads of how-to's proved pretty much worthless.  In fact, the only useful information I found on the subject was from a post on MSDN forums.  This post proved extremely helpful, even containing a c# code sample that I could use almost as-is.

But when I got to the point of implementing that code, I hit a snag.  It seems that the document would always print to the last tray on the printer, even though I was telling it to print to a specific tray.  It turns out, there was a little bit of detail missing from the sample code, namely a namespace attribute that needs to be added to the xml document that comprises the PrintTicket.  The URI for that namespace is likely to be different for each printer your application prints to, so you need to discover it at runtime.

Here's the updated sample, with my slight modifications:

public static class XpsPrinterUtils
{
private static string GetInputBinName(string printerName, int binIndex, out string nameSpaceURI)
{
string binName = string.Empty;
// get PrintQueue of Printer from the PrintServer
LocalPrintServer printServer = new LocalPrintServer();
PrintQueue printQueue = printServer.GetPrintQueue(printerName);
// get PrintCapabilities of the printer
MemoryStream printerCapXmlStream = printQueue.GetPrintCapabilitiesAsXml();
// read the JobInputBins out of the PrintCapabilities
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(printerCapXmlStream);
// create NamespaceManager and add PrintSchemaFrameWork-Namespace (should be on DocumentElement of the PrintTicket)
// Prefix: psf  NameSpace: xmlDoc.DocumentElement.NamespaceURI = "http://schemas.microsoft.com/windows/2003/08/printing/printschemaframework"
XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace(xmlDoc.DocumentElement.Prefix, xmlDoc.DocumentElement.NamespaceURI);
nameSpaceURI = xmlDoc.ChildNodes[1].GetNamespaceOfPrefix("ns0000");
// and select all nodes of the bins
XmlNodeList nodeList = xmlDoc.SelectNodes("//psf:Feature[@name='psk:JobInputBin']/psf:Option", manager);
// fill Dictionary with the bin-names and values
if (nodeList.Count > binIndex)
{
binName = nodeList[binIndex].Attributes["name"].Value;
}    
return binName;
}
public static PrintTicket ModifyPrintTicket(PrintTicket ticket, string featureName, string newValue, string nameSpaceURI)
{
if (ticket == null)
{
throw new ArgumentNullException("ticket");
}
// read Xml of the PrintTicket
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ticket.GetXmlStream());
// create NamespaceManager and add PrintSchemaFrameWork-Namespace hinzufügen (should be on DocumentElement of the PrintTicket)
// Prefix: psf  NameSpace: xmlDoc.DocumentElement.NamespaceURI = "http://schemas.microsoft.com/windows/2003/08/printing/printschemaframework"
XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace(xmlDoc.DocumentElement.Prefix, xmlDoc.DocumentElement.NamespaceURI);
// search node with desired feature we're looking for and set newValue for it
string xpath = string.Format("//psf:Feature[@name='{0}']/psf:Option", featureName);
XmlNode node = xmlDoc.SelectSingleNode(xpath, manager);
if (node != null)
{
if (newValue.StartsWith("ns0000"))
{
// add namespace to xml doc
XmlAttribute namespaceAttribute = xmlDoc.CreateAttribute("xmlns:ns0000");                
namespaceAttribute.Value = nsNameSpaceURI;
xmlDoc.DocumentElement.Attributes.Append(namespaceAttribute);
}
node.Attributes["name"].Value = newValue;
}
// create a new PrintTicket out of the XML
MemoryStream printTicketStream = new MemoryStream();
xmlDoc.Save(printTicketStream);
printTicketStream.Position = 0;
PrintTicket modifiedPrintTicket = new PrintTicket(printTicketStream);
// for testing purpose save the printticket to file
//FileStream stream = new FileStream("modPrintticket.xml", FileMode.CreateNew, FileAccess.ReadWrite);
//modifiedPrintTicket.GetXmlStream().WriteTo(stream);
return modifiedPrintTicket;
}
}
// example of calling above code
string nameSpaceURI = string.Empty;
string selectedBin = XpsPrinterUtils.GetInputBinName(PrinterName, PaperBin, out nameSpaceURI); 
PrintTicket myPrintTicket = XpsPrinterUtils.ModifyPrintTicket(defaultPrintTicket, "psk:JobInputBin", selectedBin, nameSpaceURI); 

Currently rated 4.3 by 3 people

  • Currently 4.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Posted by martin on Friday, May 23, 2008 2:48 PM
Permalink | Comments (2) | Post RSSRSS comment feed

Related posts

Comments

Jo

Saturday, May 24, 2008 7:34 AM

Jo

Hi Martin,

glad you liked my sample code and it was helpful for you... =) however this was just a quick hack of my test-applications when dealing with XPS files. At the time I only did some checks by having a look at the produced PCL code of my HP LaserJet 4050 printer and it looked fine, but thanx for pointing out this "problem" and posting the complete sample on your blog. Hopefully it's helpful for others as well... (c;

regards from Germany,
JO

Allen

Thursday, July 03, 2008 11:49 AM

Allen

What/where is nsNameSpaceURI defined at? Am I forgetting to add in a reference? Do you have a simple little app that you could send me to demonstrate using this class?

Thanks,
Allen

Add comment


(Will show your Gravatar icon)  

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Wednesday, January 07, 2009 3:43 AM