private static PrintQueue GetPrintQueue() { PrintQueue printQueue = null; bool finished = false; // window.Dispatcher.BeginInvoke(new Action(() => // { System.Windows.Controls.PrintDialog dlg = new System.Windows.Controls.PrintDialog(); bool?bPrint = dlg.ShowDialog(); if (bPrint.GetValueOrDefault()) { printQueue = dlg.PrintQueue; try { System.Printing.ValidationResult result = printQueue.MergeAndValidatePrintTicket(printQueue.UserPrintTicket, dlg.PrintTicket); printQueue.UserPrintTicket = result.ValidatedPrintTicket; printQueue.Commit(); } catch (Exception ex) { } } // finished = true; // })); // while (!finished) // Thread.Sleep(10); return(printQueue); }
/* Set up the print ticket */ private PrintTicket SetUpTicket(PrintQueue queue, Print printcontrol, FixedDocumentSequence fixdoc, bool tempfile) { PrintTicket Ticket = new PrintTicket(); PageMediaSizeName name = PaperKindToPageMediaSize(printcontrol.m_pagedetails.PaperSize.Kind); PageMediaSize mediasize = new PageMediaSize(name, printcontrol.m_pagedetails.PaperSize.Width, printcontrol.m_pagedetails.PaperSize.Height); /* Media size */ Ticket.PageMediaSize = mediasize; /* Scale to fit */ Ticket.PageScalingFactor = (int)Math.Round(printcontrol.m_page_scale * 100.0); System.Windows.Size page_size = new System.Windows.Size(mediasize.Width.Value, mediasize.Height.Value); DocumentPaginator paginator = fixdoc.DocumentPaginator; paginator.PageSize = page_size; /* Copy Count */ Ticket.CopyCount = printcontrol.m_numcopies; if (printcontrol.m_pagedetails.Landscape) { Ticket.PageOrientation = PageOrientation.Landscape; } else { Ticket.PageOrientation = PageOrientation.Portrait; } /* Orientation. If we had a tempfile, then gs did a conversion * and adjusted everything for us. Other wise we may need to * rotate the document if it was xps to start with. */ if (printcontrol.m_isrotated && !tempfile) { if (printcontrol.m_pagedetails.Landscape) { Ticket.PageOrientation = PageOrientation.Portrait; } else { Ticket.PageOrientation = PageOrientation.Landscape; } } System.Printing.ValidationResult result = queue.MergeAndValidatePrintTicket(queue.UserPrintTicket, Ticket); queue.UserPrintTicket = result.ValidatedPrintTicket; queue.Commit(); return(result.ValidatedPrintTicket); }
//<SnippetUsingMergeAndValidate> /// <summary> /// Changes the user-default PrintTicket setting of the specified print queue. /// </summary> /// <param name="queue">the printer whose user-default PrintTicket setting needs to be changed</param> static private void ChangePrintTicketSetting(PrintQueue queue) { // // Obtain the printer's PrintCapabilities so we can determine whether or not // duplexing printing is supported by the printer. // PrintCapabilities printcap = queue.GetPrintCapabilities(); // // The printer's duplexing capability is returned as a read-only collection of duplexing options // that can be supported by the printer. If the collection returned contains the duplexing // option we want to set, it means the duplexing option we want to set is supported by the printer, // so we can make the user-default PrintTicket setting change. // if (printcap.DuplexingCapability.Contains(Duplexing.TwoSidedLongEdge)) { // // To change the user-default PrintTicket, we can first create a delta PrintTicket with // the new duplexing setting. // PrintTicket deltaTicket = new PrintTicket(); deltaTicket.Duplexing = Duplexing.TwoSidedLongEdge; // // Then merge the delta PrintTicket onto the printer's current user-default PrintTicket, // and validate the merged PrintTicket to get the new PrintTicket we want to set as the // printer's new user-default PrintTicket. // ValidationResult result = queue.MergeAndValidatePrintTicket(queue.UserPrintTicket, deltaTicket); // // The duplexing option we want to set could be constrained by other PrintTicket settings // or device settings. We can check the validated merged PrintTicket to see whether the // the validation process has kept the duplexing option we want to set unchanged. // if (result.ValidatedPrintTicket.Duplexing == Duplexing.TwoSidedLongEdge) { // // Set the printer's user-default PrintTicket and commit the set operation. // queue.UserPrintTicket = result.ValidatedPrintTicket; queue.Commit(); Console.WriteLine("PrintTicket new duplexing setting is set on '{0}'.", queue.FullName); } else { // // The duplexing option we want to set has been changed by the validation process // when it was resolving setting constraints. // Console.WriteLine("PrintTicket new duplexing setting is constrained on '{0}'.", queue.FullName); } } else { // // If the printer doesn't support the duplexing option we want to set, skip it. // Console.WriteLine("PrintTicket new duplexing setting is not supported on '{0}'.", queue.FullName); } }
public void OnBeginPrint(object sender, PrintEventArgs printEventArgs) { // prepare the PrintTicket for the entire print job. PrintQueue printQueue = new PrintQueue(new LocalPrintServer(), PrinterSettings.PrinterName); PrintTicket deltaPrintTicket = new PrintTicket(); deltaPrintTicket.Duplexing = _commandLineOptions.twoPages ? Duplexing.TwoSidedLongEdge : Duplexing.OneSided; deltaPrintTicket.CopyCount = _commandLineOptions.numCopies; deltaPrintTicket.PageOrientation = _commandLineOptions.portraitFront ? PageOrientation.Portrait : PageOrientation.Landscape; ValidationResult validationResult = printQueue.MergeAndValidatePrintTicket( printQueue.UserPrintTicket, deltaPrintTicket); string xmlString = PrintTicketXml.Prefix; xmlString += _commandLineOptions.rotateFront ? PrintTicketXml.FlipFrontFlipped : PrintTicketXml.FlipFrontNone; switch (_commandLineOptions.disablePrinting) { case CommandLineOptions.DisablePrinting.All: xmlString += PrintTicketXml.DisablePrintingAll; break; case CommandLineOptions.DisablePrinting.Off: xmlString += PrintTicketXml.DisablePrintingOff; break; case CommandLineOptions.DisablePrinting.Front: xmlString += PrintTicketXml.DisablePrintingFront; break; case CommandLineOptions.DisablePrinting.Back: xmlString += PrintTicketXml.DisablePrintingBack; break; } if (_commandLineOptions.twoPages) { xmlString += _commandLineOptions.rotateBack ? PrintTicketXml.FlipBackFlipped : PrintTicketXml.FlipBackNone; } xmlString += GetTopcoatBlockingPrintTicketXml(); xmlString += PrintTicketXml.Suffix; // prepare to merge our PrintTicket xml into an actual PrintTicket: XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xmlString); MemoryStream memoryStream = new MemoryStream(); xmlDocument.Save(memoryStream); memoryStream.Position = 0; deltaPrintTicket = new PrintTicket(memoryStream); validationResult = printQueue.MergeAndValidatePrintTicket( validationResult.ValidatedPrintTicket, deltaPrintTicket); printQueue.UserPrintTicket = validationResult.ValidatedPrintTicket; if (_commandLineOptions.showXml) { Util.DisplayPrintTicket(validationResult.ValidatedPrintTicket); } // IMPORTANT: this Commit() call sets the driver's 'Printing Preferences' // on this machine: printQueue.Commit(); }
public static PrintTicket ApplyXpsInputBinDefinition([NotNull] this PrintQueue printQueue, [NotNull] PrintTicket printTicket, [NotNull] IXpsInputBinDefinition xpsInputBinDefinition) { if (printQueue == null) { throw new ArgumentNullException(nameof(printQueue)); } if (xpsInputBinDefinition == null) { throw new ArgumentNullException(nameof(xpsInputBinDefinition)); } // === result === // <?xml version="1.0" encoding="UTF-8"?> // <psf:PrintTicket xmlns:psf="http://schemas.microsoft.com/windows/2003/08/printing/printschemaframework" // xmlns:psk="http://schemas.microsoft.com/windows/2003/08/printing/printschemakeywords" // xmlns:{xsi}="http://www.w3.org/2001/XMLSchema-instance" // xmlns:xsd="http://www.w3.org/2001/XMLSchema" // xmlns:{prefix0}="{featureXName.NamespaceName}" // xmlns:{prefix1}="{inputBinXName.NamespaceName}" // xmlns:{prefix2}="{XpsServer.FeedTypeName.NamespaceName}" // xmlns:{prefix3}="{feedTypeXName.NamespaceName}" // xmlns:{prefix4}="{XpsServer.QNameName.NamespaceName}" // version="1"> // <psf:Feature name="{prefix0}:{featureXName.LocalName}"> // <psf:Option name="{prefix1}:{inputBinXName.LocalName}"> // <psf:ScoredProperty name="{prefix2}:{XpsServer.FeedTypeName.LocalName}"> // <psf:Value {xsi}:{XpsServer.TypeName.LocalName}="{prefix4}:{XpsServer.QNameName.LocalName}">{prefix3}:{feedTypeXName.LocalName}</psf:Value> // </psf:ScoredProperty> // </psf:Option> // </psf:Feature> // </psf:PrintTicket> // === === === === === var deltaPrintTicket = new PrintTicket().GetXDocument(); var feature = deltaPrintTicket.Root.AddElement(XpsServer.FeatureName); var prefix = feature.EnsurePrefixRegistrationOfNamespace(xpsInputBinDefinition.FeatureName.Namespace); feature.SetAttributeValue(XpsServer.NameName, xpsInputBinDefinition.FeatureName.ToString(prefix)); var option = feature.AddElement(XpsServer.OptionName); prefix = option.EnsurePrefixRegistrationOfNamespace(xpsInputBinDefinition.Name.Namespace); option.SetAttributeValue(XpsServer.NameName, xpsInputBinDefinition.Name.ToString(prefix)); var feedType = xpsInputBinDefinition.FeedType; if (feedType != null) { var scoredProperty = option.AddElement(XpsServer.ScoredPropertyName); prefix = scoredProperty.EnsurePrefixRegistrationOfNamespace(XpsServer.FeedTypeName.Namespace); scoredProperty.SetAttributeValue(XpsServer.NameName, XpsServer.FeedTypeName.ToString(prefix)); var value = scoredProperty.AddElement(XpsServer.ValueName); prefix = value.EnsurePrefixRegistrationOfNamespace(feedType.Namespace); value.SetValue(feedType.ToString(prefix)); value.SetAttributeValue(XpsServer.TypeName, value.ReduceName(XpsServer.QNameName)); } PrintTicket result; using (var memoryStream = new MemoryStream()) { deltaPrintTicket.Save(memoryStream); memoryStream.Seek(0L, SeekOrigin.Begin); try { result = printQueue.MergeAndValidatePrintTicket(printTicket, new PrintTicket(memoryStream)) .ValidatedPrintTicket; } catch (PrintQueueException printQueueException) { throw new InvalidOperationException("Failed to merge print ticket", printQueueException); } } return(result); }