I am trying to print a document from one of our Applications which is located on a servers. The application is based on C# .Net Framework 4.7.2. To perform the printing, I want to use the PrintTicket, because we need to edit some settings.
My problem concerns the Secure Printing (Locked Printing). I don't know exactly how to configure this. (https://learn.microsoft.com/en-us/windows-hardware/drivers/print/sample-printticket-file-for-pin-printing)
I have managed to edit the XML configuration so that a PIN can be set. However, nothing happens in the transmission to the printer afterwards.
My approach so far:
private (string printerName, PrintTicket printTicket) GetPrintOptions(string printerName, int tray)
{
// Get the dictionary of print queues that the print server hosts.
var printQueues = WpfPrinterUtilities.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
// Select the print queue.
var printQueue = printQueues.First(p => p.Value.FullName.Contains(printerName)).Value;
// Retrieve the dictionary of input bins from the print queue configuration.
var inputBins = WpfPrinterUtilities.GetInputBins(printQueue);
// Get the default print ticket associated with the print queue.
var printTicket = printQueue.DefaultPrintTicket;
if (inputBins.Any())
{
string trayname = string.Empty;
if (inputBins.ContainsKey($"Tray {tray}"))
{
trayname = $"Tray {tray}";
}
else
{
trayname = $"Fach {tray}";
}
// Modify a print ticket input bin value.
printTicket = WpfPrinterUtilities.ModifyPrintTicket(printTicket, "psk:JobInputBin", inputBins[trayname]);
//printTicket = WpfPrinterUtilities.ModifyPrintTicket(printTicket, "psk11:JobPasscode", "psk:On");
//printTicket = WpfPrinterUtilities.ModifyPrintTicket(printTicket, "psk11:JobPasscodeString", "123456");
}
return (printQueue.FullName, printTicket);
}
public static PrintTicket ModifyPrintTicket(PrintTicket ticket, string featureName, string newValue)
{
if (ticket == null)
throw new ArgumentNullException("ticket");
// Read Xml of the PrintTicket xml.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ticket.GetXmlStream());
// Create NamespaceManager and add PrintSchemaFrameWork-Namespace hinzufugen (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);
manager.AddNamespace("xsi", "https://www.w3.org/2001/XMLSchema-instance");
manager.AddNamespace("xsd", "https://www.w3.org/2001/XMLSchema");
manager.AddNamespace("psk", "https://schemas.microsoft.com/windows/2003/08/printing/printschemakeywords");
manager.AddNamespace("pskv11", "https://schemas.microsoft.com/windows/2013/05/printing/printschemakeywordsv11");
// 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 (node.Attributes["name"].Value != newValue)
node.Attributes["name"].Value = newValue;
}
XmlNode root = xmlDoc.DocumentElement;
CreatePasscodeStringNode().ForEach(n => root.AppendChild(root.OwnerDocument.ImportNode(n, true)));
// Create a new PrintTicket out of the XML.
PrintTicket modifiedPrintTicket = null;
using (MemoryStream stream = new MemoryStream())
{
xmlDoc.Save(stream);
stream.Position = 0;
modifiedPrintTicket = new PrintTicket(stream);
}
return modifiedPrintTicket;
}
private static List<XmlNode> CreatePasscodeStringNode()
{
XmlDocument xmlDoc = new XmlDocument();
var xml = @"<Root xmlns:psf=""https://schemas.microsoft.com/windows/2003/08/printing/printschemaframework"" " +
@"xmlns:xsi=""https://www.w3.org/2001/XMLSchema-instance"" " +
@"xmlns:xsd=""https://www.w3.org/2001/XMLSchema"" version=""1"" " +
@"xmlns:psk=""https://schemas.microsoft.com/windows/2003/08/printing/printschemakeywords"" " +
@"xmlns:pskv11=""https://schemas.microsoft.com/windows/2013/05/printing/printschemakeywordsv11""> " +
@"<psf:ParameterInit name=""pskv11:JobPasscodeString""><psf:Value xsi:type=""xsd:string"">123456</psf:Value></psf:ParameterInit><psf:Feature name=""pskv11:JobPasscode""><psf:Option name=""psk:On"" /></psf:Feature></Root>";
xmlDoc.Load(GenerateStreamFromString(xml));
//XmlNode root = xmlDoc.DocumentElement;
//XmlElement n = xmlDoc.CreateElement("ParameterInit");
//n.Prefix = "psf";
//n.SetAttribute("name", "pskv11:JobPasscodeString");
//XmlElement n2 = xmlDoc.CreateElement("Value");
//n2.Prefix = "psf";
//n2.SetAttribute("xsi:type", "xsd:string");
//n2.InnerText = "123456";
//n.AppendChild(n2);
var result = new List<XmlNode>();
foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
result.Add(node);
return result;
}
What am I missing? The XML is formated correctly (see below) and can be transmited to the printer, but nothing happens.
<psf:PrintTicket xmlns:psf="http://schemas.microsoft.com/windows/2003/08/printing/printschemaframework" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:psk="http://schemas.microsoft.com/windows/2003/08/printing/printschemakeywords" xmlns:ns0000="http://schemas.ricoh.com/2007/printing/keywords" version="1">
<psf:ParameterInit name="ns0000:PageDevmodeSnapshot">
<psf:Value xsi:type="xsd:string">Value xxx</psf:Value>
</psf:ParameterInit>
<psf:Feature name="ns0000:PageOverlayWatermark">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:JobBannerPage">
<psf:Option name="ns0000:Off"/>
<psf:Feature name="ns0000:BannerPageInputBin">
<psf:Option name="ns0000:AUTO"/>
</psf:Feature>
<psf:Feature name="ns0000:BannerPageMediaType">
<psf:Option name="ns0000:Standard_Recycled"/>
</psf:Feature>
</psf:Feature>
<psf:Feature name="psk:PageResolution">
<psf:Option name="ns0000:_2400x600">
<psf:ScoredProperty name="psk:ResolutionX">
<psf:Value xsi:type="xsd:integer">600</psf:Value>
</psf:ScoredProperty>
<psf:ScoredProperty name="psk:ResolutionY">
<psf:Value xsi:type="xsd:integer">600</psf:Value>
</psf:ScoredProperty>
</psf:Option>
</psf:Feature>
<psf:Feature name="ns0000:JobRicPrintJob">
<psf:Option name="ns0000:RicPrintNormal"/>
<psf:Feature name="ns0000:ScheduledPrint">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
</psf:Feature>
<psf:Feature name="psk:PageOutputColor">
<psf:Option name="psk:Color"/>
</psf:Feature>
<psf:Feature name="psk:DocumentCollate">
<psf:Option name="psk:Uncollated"/>
</psf:Feature>
<psf:Feature name="ns0000:JobOffset">
<psf:Option name="ns0000:Normal"/>
</psf:Feature>
<psf:Feature name="psk:JobNUpAllDocumentsContiguously">
<psf:Option>
<psf:ScoredProperty name="psk:PagesPerSheet">
<psf:Value xsi:type="xsd:integer">1</psf:Value>
</psf:ScoredProperty>
</psf:Option>
<psf:Feature name="psk:PresentationDirection">
<psf:Option name="ns0000:None"/>
</psf:Feature>
<psf:Feature name="ns0000:Frame">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
</psf:Feature>
<psf:Feature name="ns0000:JobBlackOverPrint">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:JobDuplexModeOneSidedPrint">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:JobSecretNumbering">
<psf:Option name="ns0000:Off"/>
<psf:Feature name="ns0000:SecretNumberingSize">
<psf:Option name="ns0000:Middle"/>
</psf:Feature>
<psf:Feature name="ns0000:SecretNumberingDensity">
<psf:Option name="ns0000:Middle"/>
</psf:Feature>
<psf:Feature name="ns0000:SecretNumberingColor">
<psf:Option name="ns0000:Black"/>
</psf:Feature>
</psf:Feature>
<psf:Feature name="ns0000:JobFold">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:ParameterInit name="ns0000:JobTrackID">
<psf:Value xsi:type="xsd:string"/>
</psf:ParameterInit>
<psf:Feature name="ns0000:JobWhitePaperSuppress">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:JobBooklet">
<psf:Option name="ns0000:Off"/>
<psf:Feature name="ns0000:BookletPageOrder">
<psf:Option name="ns0000:OpenToLeftOrTop"/>
</psf:Feature>
</psf:Feature>
<psf:Feature name="psk:JobStapleAllDocuments">
<psf:Option name="psk:None"/>
</psf:Feature>
<psf:Feature name="ns0000:PagePrintPaperSize">
<psf:Option name="ns0000:SameAsPageMediaSize"/>
</psf:Feature>
<psf:Feature name="ns0000:PageColorProfileForPhoto">
<psf:Option name="ns0000:Photographic"/>
</psf:Feature>
<psf:Feature name="ns0000:PageHeaderFooter">
<psf:Option name="ns0000:Off"/>
<psf:Feature name="ns0000:HeaderFooterPrintPer">
<psf:Option name="ns0000:OriginalPage"/>
</psf:Feature>
<psf:Feature name="ns0000:HeaderFooterDate">
<psf:Option name="ns0000:Off"/>
<psf:Feature name="ns0000:HeaderFooterDatePosition">
<psf:Option name="ns0000:TopLeft"/>
</psf:Feature>
<psf:Feature name="ns0000:HeaderFooterDateFormat">
<psf:Option name="ns0000:YYYYMMDD_DotFormat"/>
</psf:Feature>
</psf:Feature>
<psf:Feature name="ns0000:HeaderFooterNumber">
<psf:Option name="ns0000:Off"/>
<psf:Feature name="ns0000:HeaderFooterNumberPosition">
<psf:Option name="ns0000:BottomCenter"/>
</psf:Feature>
<psf:Feature name="ns0000:HeaderFooterNumberFormat">
<psf:Option name="ns0000:OnlyNumber"/>
</psf:Feature>
</psf:Feature>
<psf:Feature name="ns0000:HeaderFooterText">
<psf:Option name="ns0000:Off"/>
<psf:Feature name="ns0000:HeaderFooterTextPosition">
<psf:Option name="ns0000:BottomLeft"/>
</psf:Feature>
<psf:Feature name="ns0000:HeaderFooterTextFormat">
<psf:Option name="ns0000:FileName"/>
</psf:Feature>
</psf:Feature>
</psf:Feature>
<psf:Feature name="ns0000:JobColorUniversalDesign">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:JobDuplexAllDocumentsDirection">
<psf:Option name="ns0000:LongEdgeLeftOrTop"/>
</psf:Feature>
<psf:Feature name="psk:JobDuplexAllDocumentsContiguously">
<psf:Option name="psk:TwoSidedLongEdge"/>
</psf:Feature>
<psf:Feature name="ns0000:JobDitherForGraphics">
<psf:Option name="ns0000:Photographic"/>
</psf:Feature>
<psf:Feature name="ns0000:JobCopyGuard">
<psf:Feature name="ns0000:CopyGuardText">
<psf:Option>
<psf:ScoredProperty name="ns0000:CopyGuardTextString">
<psf:Value xsi:type="xsd:string">COPY</psf:Value>
</psf:ScoredProperty>
<psf:ScoredProperty name="ns0000:CopyGuardFontSize">
<psf:Value xsi:type="xsd:integer">70</psf:Value>
</psf:ScoredProperty>
</psf:Option>
<psf:Feature name="ns0000:CopyGuardTextFont">
<psf:Option name="ns0000:NimbusSans_Bold"/>
</psf:Feature>
<psf:Feature name="ns0000:CopyGuardTextPosition">
<psf:Option name="ns0000:Center">
<psf:ScoredProperty name="ns0000:CopyGuardTextAngle">
<psf:Value xsi:type="xsd:integer">30</psf:Value>
</psf:ScoredProperty>
</psf:Option>
</psf:Feature>
<psf:Feature name="ns0000:CopyGuardTextType">
<psf:Option name="ns0000:UserDefined"/>
</psf:Feature>
<psf:Feature name="ns0000:CopyGuardRepeatedText">
<psf:Option name="ns0000:Repeat">
<psf:ScoredProperty name="ns0000:CopyGuardLineSpace">
<psf:Value xsi:type="xsd:integer">70</psf:Value>
</psf:ScoredProperty>
</psf:Option>
</psf:Feature>
</psf:Feature>
<psf:Feature name="ns0000:CopyGuardPatternEffects">
<psf:Option name="ns0000:Normal"/>
</psf:Feature>
</psf:Feature>
<psf:Feature name="ns0000:JobDither">
<psf:Option name="ns0000:Auto"/>
</psf:Feature>
<psf:Feature name="ns0000:JobDitheringSelectObject">
<psf:Option name="ns0000:SetAllObject"/>
</psf:Feature>
<psf:ParameterInit name="psk:JobCopiesAllDocuments">
<psf:Value xsi:type="xsd:integer">1</psf:Value>
</psf:ParameterInit>
<psf:Feature name="psk:PageOrientation">
<psf:Option name="psk:Portrait"/>
</psf:Feature>
<psf:Feature name="psk:PagePhotoPrintingIntent">
<psf:Option name="psk:PhotoDraft"/>
</psf:Feature>
<psf:Feature name="ns0000:JobCoverFront">
<psf:Option name="ns0000:NoCover"/>
<psf:Feature name="ns0000:CoverFrontInputBin">
<psf:Option name="ns0000:Tray1"/>
</psf:Feature>
</psf:Feature>
<psf:Feature name="ns0000:PageColorProfileForGraphics">
<psf:Option name="ns0000:Presentation"/>
</psf:Feature>
<psf:Feature name="ns0000:JobChapterPrint">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:PageColorProfileForText">
<psf:Option name="ns0000:Text"/>
</psf:Feature>
<psf:Feature name="ns0000:JobLetterheadMode">
<psf:Option name="ns0000:PrinterDefault"/>
</psf:Feature>
<psf:Feature name="psk:PageScaling">
<psf:Option name="ns0000:FitToPaperSize"/>
</psf:Feature>
<psf:Feature name="psk:JobOutputBin">
<psf:Option name="ns0000:PrinterDefault"/>
</psf:Feature>
<psf:Feature name="psk:PagePoster">
<psf:Option>
<psf:ScoredProperty name="psk:SheetsPerPage">
<psf:Value xsi:type="xsd:integer">1</psf:Value>
</psf:ScoredProperty>
</psf:Option>
</psf:Feature>
<psf:Feature name="ns0000:JobCopyControlPrint">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:DocumentCollateDistribution">
<psf:Option name="ns0000:PrinterCollate"/>
</psf:Feature>
<psf:Feature name="psk:PageMediaSize">
<psf:Option name="psk:ISOA4">
<psf:ScoredProperty name="psk:MediaSizeWidth">
<psf:Value xsi:type="xsd:integer">210000</psf:Value>
</psf:ScoredProperty>
<psf:ScoredProperty name="psk:MediaSizeHeight">
<psf:Value xsi:type="xsd:integer">297000</psf:Value>
</psf:ScoredProperty>
</psf:Option>
</psf:Feature>
<psf:Feature name="ns0000:JobFixdmColor">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:PageImageWatermark">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:PageColorProfile">
<psf:Option name="ns0000:Auto"/>
</psf:Feature>
<psf:Feature name="ns0000:JobCoverBack">
<psf:Option name="ns0000:NoCover"/>
<psf:Feature name="ns0000:CoverBackInputBin">
<psf:Option name="ns0000:Tray1"/>
</psf:Feature>
</psf:Feature>
<psf:Feature name="psk:JobInputBin">
<psf:Option name="ns0000:Tray1"/>
</psf:Feature>
<psf:Feature name="ns0000:JobPrintDensity">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:JobPrintTextAsBlack">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:JobGraphicsMode">
<psf:Option name="ns0000:Vector"/>
</psf:Feature>
<psf:Feature name="psk:JobPageOrder">
<psf:Option name="psk:Standard"/>
</psf:Feature>
<psf:Feature name="ns0000:JobDitherForPhoto">
<psf:Option name="ns0000:Photographic"/>
</psf:Feature>
<psf:Feature name="ns0000:JobDitherForText">
<psf:Option name="ns0000:Text"/>
</psf:Feature>
<psf:Feature name="ns0000:JobGrayReproduction">
<psf:Option name="ns0000:BlackForAllObjects"/>
</psf:Feature>
<psf:Feature name="ns0000:JobBalanceAdjustment">
<psf:Option name="ns0000:SetAllObject">
<psf:ScoredProperty name="ns0000:BrightnessAll">
<psf:Value xsi:type="xsd:integer">0</psf:Value>
</psf:ScoredProperty>
<psf:ScoredProperty name="ns0000:ContrastAll">
<psf:Value xsi:type="xsd:integer">0</psf:Value>
</psf:ScoredProperty>
<psf:ScoredProperty name="ns0000:SaturationAll">
<psf:Value xsi:type="xsd:integer">0</psf:Value>
</psf:ScoredProperty>
<psf:ScoredProperty name="ns0000:ColorBalanceRAll">
<psf:Value xsi:type="xsd:integer">0</psf:Value>
</psf:ScoredProperty>
<psf:ScoredProperty name="ns0000:ColorBalanceGAll">
<psf:Value xsi:type="xsd:integer">0</psf:Value>
</psf:ScoredProperty>
<psf:ScoredProperty name="ns0000:ColorBalanceBAll">
<psf:Value xsi:type="xsd:integer">0</psf:Value>
</psf:ScoredProperty>
</psf:Option>
</psf:Feature>
<psf:Feature name="ns0000:JobPrintQualityAdjustment">
<psf:Option name="ns0000:Standard"/>
</psf:Feature>
<psf:Feature name="ns0000:JobCopyPrevention">
<psf:Feature name="ns0000:CopyPreventionText">
<psf:Option>
<psf:ScoredProperty name="ns0000:CopyPreventionTextString">
<psf:Value xsi:type="xsd:string">COPY</psf:Value>
</psf:ScoredProperty>
<psf:ScoredProperty name="ns0000:CopyPreventionFontSize">
<psf:Value xsi:type="xsd:integer">70</psf:Value>
</psf:ScoredProperty>
</psf:Option>
<psf:Feature name="ns0000:CopyPreventionTextFont">
<psf:Option name="ns0000:NimbusSans_Bold"/>
</psf:Feature>
<psf:Feature name="ns0000:CopyPreventionTextPosition">
<psf:Option name="ns0000:Center">
<psf:ScoredProperty name="ns0000:CopyPreventionTextAngle">
<psf:Value xsi:type="xsd:integer">30</psf:Value>
</psf:ScoredProperty>
</psf:Option>
</psf:Feature>
<psf:Feature name="ns0000:CopyPreventionTextType">
<psf:Option name="ns0000:UserDefined"/>
</psf:Feature>
<psf:Feature name="ns0000:CopyPreventionRepeatedText">
<psf:Option name="ns0000:Repeat">
<psf:ScoredProperty name="ns0000:CopyPreventionLineSpace">
<psf:Value xsi:type="xsd:integer">70</psf:Value>
</psf:ScoredProperty>
</psf:Option>
</psf:Feature>
</psf:Feature>
<psf:Feature name="ns0000:CopyPreventionMaskType">
<psf:Option name="ns0000:None"/>
</psf:Feature>
<psf:Feature name="ns0000:CopyPreventionPatternEffects">
<psf:Option name="ns0000:Normal"/>
</psf:Feature>
<psf:Feature name="ns0000:CopyPreventionColor">
<psf:Option name="ns0000:Black"/>
</psf:Feature>
<psf:Feature name="ns0000:CopyPreventionDensity">
<psf:Option name="ns0000:Middle"/>
</psf:Feature>
</psf:Feature>
<psf:Feature name="ns0000:PageWatermark">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:JobImageDirection">
<psf:Option name="ns0000:Normal"/>
</psf:Feature>
<psf:Feature name="ns0000:JobEdgeToEdgePrint">
<psf:Option name="ns0000:Off"/>
</psf:Feature>
<psf:Feature name="ns0000:JobSlipSheet">
<psf:Option name="ns0000:Off"/>
<psf:Feature name="ns0000:PrintOnSlipSheet">
<psf:Option name="ns0000:NotPrintOnSlipSheet"/>
</psf:Feature>
<psf:Feature name="ns0000:SlipSheetInputBin">
<psf:Option name="ns0000:Tray1"/>
</psf:Feature>
</psf:Feature>
<psf:Feature name="psk:PageMediaType">
<psf:Option name="ns0000:Standard_Recycled"/>
</psf:Feature>
<psf:Feature name="ns0000:JobColorProfileSelectObject">
<psf:Option name="ns0000:SetAllObject"/>
</psf:Feature>
<psf:Feature name="psk:JobHolePunch">
<psf:Option name="psk:None"/>
</psf:Feature>
<psf:ParameterInit name="pskv11:JobPasscodeString" xmlns:psf="https://schemas.microsoft.com/windows/2003/08/printing/printschemaframework">
<psf:Value xsi:type="xsd:string" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">123456</psf:Value>
</psf:ParameterInit>
<psf:Feature name="pskv11:JobPasscode" xmlns:psf="https://schemas.microsoft.com/windows/2003/08/printing/printschemaframework">
<psf:Option name="psk:On"/>
</psf:Feature>
</psf:PrintTicket>