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); }
private void resumeAllJobs(PrintQueue queue) { queue.Refresh(); foreach (var job in queue.GetPrintJobInfoCollection()) { job.Refresh(); job.Resume(); } queue.Commit(); }
/// <summary> /// Sets up the printer as a share /// </summary> public void EnableSharedQueue() { using (LocalPrintServer server = new LocalPrintServer(PrintSystemDesiredAccess.AdministrateServer)) { using (PrintQueue queue = new PrintQueue(server, QueueName, PrintSystemDesiredAccess.AdministratePrinter)) { queue.ShareName = QueueName; queue.Commit(); PrintQueueController.ChangeAttributes(queue, PrintQueueAttributes.Shared, true); } } }
/* 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); }
/// <summary> /// Pauses all of the current user's jobs in the queue, provided printing is disabled /// </summary> /// <param name="queue"></param> private void pauseAllJobs(PrintQueue queue) { queue.Refresh(); foreach (PrintSystemJobInfo job in queue.GetPrintJobInfoCollection()) { job.Refresh(); logJob(job.JobIdentifier, job.NumberOfPages, job.TimeJobSubmitted, job.Submitter); pauseJob(job); } queue.Commit(); }
//<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(); }