public static PrintTicket ModifyPrintTicket(PrintTicket ticket, string featureName, string newValue)
        {
            if (ticket == null)
            {
                throw new ArgumentNullException("ticket");
            }

            var xmlDoc = new XmlDocument();
            xmlDoc.Load(ticket.GetXmlStream());

            var manager = new XmlNamespaceManager(xmlDoc.NameTable);
            manager.AddNamespace(xmlDoc.DocumentElement.Prefix, xmlDoc.DocumentElement.NamespaceURI);

            var xpath = string.Format("//psf:Feature[contains(@name, 'InputBin')]/psf:Option", featureName);
            var node = xmlDoc.SelectSingleNode(xpath, manager);
            if (node != null)
            {
                node.Attributes["name"].Value = newValue;
            }

            var printTicketStream = new MemoryStream();
            xmlDoc.Save(printTicketStream);
            printTicketStream.Position = 0;
            var modifiedPrintTicket = new PrintTicket(printTicketStream);
            return modifiedPrintTicket;
        }
        internal static byte[] InternalConvertPrintTicketToDevMode(PTProviderBase provider,
                                                                   PrintTicket printTicket,
                                                                   BaseDevModeType baseType,
                                                                   PrintTicketScope scope)
        {
            // Input PrinTicket can't be null.
            if (printTicket == null)
            {
                throw new ArgumentNullException(nameof(printTicket));
            }

            // Validate the base type value.
            if ((baseType != BaseDevModeType.UserDefault) &&
                (baseType != BaseDevModeType.PrinterDefault))
            {
                throw new ArgumentOutOfRangeException(nameof(baseType));
            }

            // Validate scope value.
            if ((scope != PrintTicketScope.PageScope) &&
                (scope != PrintTicketScope.DocumentScope) &&
                (scope != PrintTicketScope.JobScope))
            {
                throw new ArgumentOutOfRangeException(nameof(scope));
            }

            MemoryStream ptStream = printTicket.GetXmlStream();

            return(provider.ConvertPrintTicketToDevMode(ptStream, baseType, scope));
        }
Example #3
0
        public CloudPrinterImpl(PrintQueue queue)
        {
            PrintTicket defaults = queue.DefaultPrintTicket.Clone();

            defaults.OutputColor   = OutputColor.Monochrome;
            defaults.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4);

            this.Name         = queue.FullName;
            this.Description  = queue.Description;
            this.Capabilities = XDocument.Load(queue.GetPrintCapabilitiesAsXml()).ToString();
            this.Defaults     = new StreamReader(defaults.GetXmlStream(), Encoding.UTF8, false).ReadToEnd();
            this.CapsHash     = GetMD5Hash(Encoding.UTF8.GetBytes(Capabilities));

            PrinterConfigurationSection printerconfigs = Config.PrinterConfigurationSection;

            if (printerconfigs != null)
            {
                this.PrinterConfiguration = printerconfigs.Printers.OfType <PrinterConfiguration>().SingleOrDefault(p => p.Name == this.Name);

                if (this.PrinterConfiguration != null)
                {
                    this.JobPrinterType = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes().Where(t => t.Name == this.PrinterConfiguration.JobPrinter && typeof(JobPrinter).IsAssignableFrom(t))).SingleOrDefault();
                }
                else
                {
                    this.JobPrinterType = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes().Where(t => t.Name == printerconfigs.DefaultJobPrinter && typeof(JobPrinter).IsAssignableFrom(t))).SingleOrDefault();
                }
            }

            if (this.JobPrinterType == null)
            {
                this.JobPrinterType = PrinterConfiguration.DefaultJobPrinterType;
            }
        }
        public static PrintTicket ModifyPrintTicket(PrintTicket ticket, string featureName, string newValue)
        {
            if (ticket == null)
            {
                throw new ArgumentNullException("ticket");
            }

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(ticket.GetXmlStream());

            var manager = new XmlNamespaceManager(xmlDoc.NameTable);

            manager.AddNamespace(xmlDoc.DocumentElement.Prefix, xmlDoc.DocumentElement.NamespaceURI);

            var xpath = string.Format("//psf:Feature[contains(@name, 'InputBin')]/psf:Option", featureName);
            var node  = xmlDoc.SelectSingleNode(xpath, manager);

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

            var printTicketStream = new MemoryStream();

            xmlDoc.Save(printTicketStream);
            printTicketStream.Position = 0;
            var modifiedPrintTicket = new PrintTicket(printTicketStream);

            return(modifiedPrintTicket);
        }
Example #5
0
        public static XDocument GetXDocument([NotNull] this PrintTicket printTicket)
        {
            if (printTicket == null)
            {
                throw new ArgumentNullException(nameof(printTicket));
            }

            XDocument result;

            using (var memoryStream = printTicket.GetXmlStream())
            {
                result = XDocument.Load(memoryStream);
            }

            return(result);
        }
Example #6
0
 private static Stream CloneStream(PrintTicket ticket)
 {
     using (var xml = ticket.GetXmlStream())
         using (var reader = new StreamReader(xml))
         {
             var text = reader.ReadToEnd();
             // convert string to stream
             var newstream = new MemoryStream();
             var writer    = new StreamWriter(newstream);
             writer.Write(text);
             writer.Flush();
             // convert stream to string
             newstream.Position = 0;
             return(newstream);
         }
 }
Example #7
0
        public static PrintTicket ModifyPrintTicket(PrintTicket ticket, string featureName, string newValue)
        {
            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 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);

            // search node with desired feature we're looking for and set newValue for it
            string xpath = string.Format("//psf:Feature[@name='{0}']/psfSurpriseption", featureName);
            XmlNode node = xmlDoc.SelectSingleNode(xpath, manager);
            if (node != null)
            {
                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;
        }
        /// <summary>
        /// Modifes a print ticket xml after updating a feature value.
        ///
        /// Sample usage:
        /// Get Dictionary with Inputbins by calling the other method
        /// and get "value" for the desired inputbin you'd like to use...
        /// ...
        /// desiredTray is then something like "NS0000:SurpriseOption7" for example.
        /// defaultPrintTicket is the (Default)PrintTicket you want to modify from the PrintQueue for example
        /// PrintTicket myPrintTicket = WpfPrinterUtils.ModifyPrintTicket(defaultPrintTicket, "psk:JobInputBin", desiredTray);
        /// </summary>
        /// <param name="ticket"></param>
        /// <param name="featureName"></param>
        /// <param name="newValue"></param>
        /// <param name="printQueueName">Optional - If provided, a file is created with the print ticket xml. Useful for debugging.</param>
        /// <param name="folder">Optional - If provided, the path for a file is created with the print ticket xml. Defaults to c:\. Useful for debugging.</param>
        /// <param name="displayMessage">Optional - True to display a dialog with changes. Defaults to false. Useful for debugging.</param>
        /// <returns></returns>
        public static PrintTicket ModifyPrintTicket(PrintTicket ticket, string featureName, string newValue, string printQueueName = null, string folder = null, bool displayMessage = false)
        {
            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);

            // 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)
                {
                    if (displayMessage)
                    {
                        System.Windows.MessageBox.Show(string.Format("OldValue: {0}, NewValue: {1}", node.Attributes["name"].Value, newValue), "Input Bin");
                    }
                    node.Attributes["name"].Value = newValue;
                }
            }

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

            // For testing purpose save the print ticket to a file.
            if (!string.IsNullOrWhiteSpace(printQueueName))
            {
                if (string.IsNullOrWhiteSpace(folder))
                {
                    folder = "c:\\";
                }
                // Colons are not valid in a file name.
                newValue       = newValue.Replace(':', ';');
                printQueueName = string.Format("{0} PrintTicket {1}.xml", Path.Combine(folder, printQueueName), newValue);
                if (File.Exists(printQueueName))
                {
                    File.Delete(printQueueName);
                }
                if (!Directory.Exists(Path.GetDirectoryName(printQueueName)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(printQueueName));
                }
                using (FileStream stream = new FileStream(printQueueName, FileMode.CreateNew, FileAccess.ReadWrite))
                {
                    modifiedPrintTicket.GetXmlStream().WriteTo(stream);
                }
            }

            return(modifiedPrintTicket);
        }