* Reset branch for WPF changes * Convert BMP to PNG; fix link-out-of-scope err * Add snippets for WPF... 6794 files!!!! * Add missing snippets * update file updated between migration * Fix paths to include * update breadcrumb and toc * fix index links * fix index links * fix index links * fix markdown
5.2 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| How to: Validate and Merge PrintTickets | 03/30/2017 |
|
|
4fe2d501-d0b0-4fef-86af-6ffe6c162532 |
How to: Validate and Merge PrintTickets
The Microsoft Windows Print Schema includes the flexible and extensible xref:System.Printing.PrintCapabilities and xref:System.Printing.PrintTicket elements. The former itemizes the capabilities of a print device and the latter specifies how the device should use those capabilities with respect to a particular sequence of documents, individual document, or individual page.
A typical sequence of tasks for an application that supports printing would be as follows.
-
Determine a printer's capabilities.
-
Configure a xref:System.Printing.PrintTicket to use those capabilities.
-
Validate the xref:System.Printing.PrintTicket.
This article shows how to do this.
Example
In the simple example below, we are interested only in whether a printer can support duplexing — two-sided printing. The major steps are as follows.
-
Get a xref:System.Printing.PrintCapabilities object with the xref:System.Printing.PrintQueue.GetPrintCapabilities%2A method.
-
Test for the presence of the capability you want. In the example below, we test the xref:System.Printing.PrintCapabilities.DuplexingCapability%2A property of the xref:System.Printing.PrintCapabilities object for the presence of the capability of printing on both sides of a sheet of paper with the "page turning" along the long side of the sheet. Since xref:System.Printing.PrintCapabilities.DuplexingCapability%2A is a collection, we use the
Containsmethod of xref:System.Collections.ObjectModel.ReadOnlyCollection%601.Note
This step is not strictly necessary. The xref:System.Printing.PrintQueue.MergeAndValidatePrintTicket%2A method used below will check each request in the xref:System.Printing.PrintTicket against the capabilities of the printer. If the requested capability is not supported by printer, the printer driver will substitute an alternative request in the xref:System.Printing.PrintTicket returned by the method.
-
If the printer supports duplexing, the sample code creates a xref:System.Printing.PrintTicket that asks for duplexing. But the application does not specify every possible printer setting available in the xref:System.Printing.PrintTicket element. That would be wasteful of both programmer and program time. Instead, the code sets only the duplexing request and then merges this xref:System.Printing.PrintTicket with an existing, fully configured and validated, xref:System.Printing.PrintTicket, in this case, the user's default xref:System.Printing.PrintTicket.
-
Accordingly, the sample calls the xref:System.Printing.PrintQueue.MergeAndValidatePrintTicket%2A method to merge the new, minimal, xref:System.Printing.PrintTicket with the user's default xref:System.Printing.PrintTicket. This returns a xref:System.Printing.ValidationResult that includes the new xref:System.Printing.PrintTicket as one of its properties.
-
The sample then tests that the new xref:System.Printing.PrintTicket requests duplexing. If it does, then the sample makes it the new default print ticket for the user. If step 2 above had been left out and the printer did not support duplexing along the long side, then the test would have resulted in
false. (See the note above.) -
The last significant step is to commit the change to the xref:System.Printing.PrintQueue.UserPrintTicket%2A property of the xref:System.Printing.PrintQueue with the xref:System.Printing.PrintQueue.Commit%2A method.
[!code-csharpPrintTicketManagment#UsingMergeAndValidate] [!code-vbPrintTicketManagment#UsingMergeAndValidate]
So that you can quickly test this example, the remainder of it is presented below. Create a project and a namespace and then paste both the code snippets in this article into the namespace block.
[!code-csharpPrintTicketManagment#UIForMergeAndValidatePTUtility] [!code-vbPrintTicketManagment#UIForMergeAndValidatePTUtility]
See also
- xref:System.Printing.PrintCapabilities
- xref:System.Printing.PrintTicket
- xref:System.Printing.PrintServer.GetPrintQueues%2A
- xref:System.Printing.PrintServer
- xref:System.Printing.EnumeratedPrintQueueTypes
- xref:System.Printing.PrintQueue
- xref:System.Printing.PrintQueue.GetPrintCapabilities%2A
- Documents in WPF
- Printing Overview
- Print Schema