* Initial files * update toc * Content update - print-xps-files (user story 1861941) (#1151) * Add C# and VB sample code projects * Fix comments * Update C# and VB code * Add article * Further edits * Reviewer requested edits * Further edits * Use xref to link to API * Minor edits * Content update - invoke-print-dialog (user story 1861941) (#1141) * Content update including C# WPF project * Fix link * Remove test code * Further doc and C# sample edits * Add feature: printing with no print dialog * Separate out specific page range print functionality * Cleanup comments * UI tweaks * Add VB sample code project * Code file formatting * Update dotnet-desktop-guide/net/wpf/documents/how-to-display-print-dialog.md Co-authored-by: Andy (Steve) De George <[email protected]> * Add page range printing * Minor edits * Update dotnet-desktop-guide/net/wpf/documents/snippets/how-to-display-print-dialog/csharp/MainWindow.xaml.cs Co-authored-by: Andy (Steve) De George <[email protected]> * Update dotnet-desktop-guide/net/wpf/documents/how-to-display-print-dialog.md Co-authored-by: Andy (Steve) De George <[email protected]> * Promote example headings * Reorganize and add detail * Minor edits * Add detail * Further edits * Further edits * Add resource links * Add alternative API suggestions to a tip * Minor edit Co-authored-by: Andy (Steve) De George <[email protected]> * Content update - print-overview (user story 1861941) (#1153) * Add C# and VB sample code projects * Add article * Add redirects Co-authored-by: Tris Shores <[email protected]>
3.9 KiB
title, description, ms.date, dev_langs, helpviewer_keywords
| title | description | ms.date | dev_langs | helpviewer_keywords | |||
|---|---|---|---|---|---|---|---|
| How to print an XML Paper Specification (XPS) file | Learn how to print an XPS file from your application by using the System.Printing.PrintQueue.AddJob method. | 09/16/2021 |
|
|
How to print an XPS file (WPF .NET)
Sometimes you'll want to add a new print job to the print queue without opening a print dialog box. You can use one of the xref:System.Printing.PrintQueue.AddJob%2A?displayProperty=nameWithType methods to do that. Here's how.
[!INCLUDE desktop guide under construction]
In the following example, we use the xref:System.Printing.PrintQueue.AddJob%28System.String%2CSystem.String%2CSystem.Boolean%29 method, one of the several overloads of AddJob, to:
- Add a new print job for an XML Paper Specification (XPS) document into the default print queue.
- Name the new job.
- Specify whether the XPS document should be validated (by using the
fastCopyparameter).
When using the AddJob(String, String, Boolean) method, the value of the fastCopy parameter is a key consideration:
- If you set the
fastCopyparameter totrue, then XPS validation is skipped and the print job will spool quickly without page-by-page progress feedback. - If you set the
fastCopyparameter tofalse, then the thread that calls theAddJobmethod must have a single-threaded apartment state, otherwise an exception will be thrown. For more information, see the Remarks section for xref:System.Printing.PrintQueue.AddJob%28System.String%2CSystem.String%2CSystem.Boolean%29.
Add new print jobs to the queue
This example adds one or more XPS documents to the default queue. The code will:
- Use xref:System.Threading.Tasks.Task.Run%2A?displayProperty=nameWithType to avoid blocking the UI thread—since there's no async version of
AddJob. - If the
fastCopyparameter value isfalse, run xref:System.Printing.PrintQueue.AddJob%28System.String%2CSystem.String%2CSystem.Boolean%29 on a thread with single-threaded apartment state. - Get a reference to the default xref:System.Printing.PrintQueue of the xref:System.Printing.LocalPrintServer.
- Call
AddJob(String, String, Boolean)on the print queue reference, passing in a job name, an XPS document path, and thefastCopyparameter.
If the queue isn't paused and the printer is working, then a print job will automatically begin printing when it reaches the top of the print queue.
Tip
To avoid the Save Output File As dialog when adding a print job to the default queue, ensure that your default printer isn't Microsoft XPS Document Writer, Microsoft Print to PDF, or other print-to-file options.
:::code language="csharp" source="./snippets/how-to-print-xps-files/csharp/MainWindow.xaml.cs" id="SampleCode1"::: :::code language="vb" source="./snippets/how-to-print-xps-files/vb/MainWindow.xaml.vb" id="SampleCode1":::
Tip
You can also print XPS files by using:
- xref:System.Windows.Controls.PrintDialog.PrintDocument%2A?displayProperty=nameWithType or xref:System.Windows.Controls.PrintDialog.PrintVisual%2A?displayProperty=nameWithType methods.
- xref:System.Windows.Xps.XpsDocumentWriter.Write%2A?displayProperty=nameWithType and xref:System.Windows.Xps.XpsDocumentWriter.WriteAsync%2A?displayProperty=nameWithType methods.
For more information, see How to display a print dialog box and Printing documents overview.