Files
docs-desktop/dotnet-desktop-guide/net/wpf/documents/how-to-print-xps-files.md
T
Andy (Steve) De GeorgeandTris Shores bdd178c010 Printing in WPF port for .NET (#1156)
* 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]>
2021-09-24 16:16:55 -04:00

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
csharp
vb
programmatically print xps files [WPF]

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 fastCopy parameter).

When using the AddJob(String, String, Boolean) method, the value of the fastCopy parameter is a key consideration:

Add new print jobs to the queue

This example adds one or more XPS documents to the default queue. The code will:

  1. Use xref:System.Threading.Tasks.Task.Run%2A?displayProperty=nameWithType to avoid blocking the UI thread—since there's no async version of AddJob.
  2. If the fastCopy parameter value is false, run xref:System.Printing.PrintQueue.AddJob%28System.String%2CSystem.String%2CSystem.Boolean%29 on a thread with single-threaded apartment state.
  3. Get a reference to the default xref:System.Printing.PrintQueue of the xref:System.Printing.LocalPrintServer.
  4. Call AddJob(String, String, Boolean) on the print queue reference, passing in a job name, an XPS document path, and the fastCopy parameter.

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:

For more information, see How to display a print dialog box and Printing documents overview.

See also