Files
docs-desktop/dotnet-desktop-guide/net/wpf/documents/how-to-display-print-dialog.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

4.6 KiB

title, description, ms.date, dev_langs, helpviewer_keywords
title description ms.date dev_langs helpviewer_keywords
How to display a print dialog Learn how to print from your application by using the System.Windows.Controls.PrintDialog class to open a standard Microsoft Windows print dialog box. 08/20/2021
csharp
vb
invoking print dialogs [WPF]
print dialogs [WPF], invoking

How to display a print dialog box (WPF .NET)

Want to print from your application? You can use the xref:System.Windows.Controls.PrintDialog class to open a standard Microsoft Windows print dialog box. Here's how.

[!INCLUDE desktop guide under construction]

Note

The xref:System.Windows.Controls.PrintDialog?displayProperty=nameWithType control used for WPF and discussed here, should not be confused with the xref:System.Windows.Forms.PrintDialog?displayProperty=nameWithType component of Windows Forms.

The xref:System.Windows.Controls.PrintDialog class provides a single control for print configuration and print job submission. The control is easy to use and can be instantiated by using XAML markup or code. The following examples create and display a PrintDialog instance using code.

You can use the print dialog to configure print options, such as:

  • Print only a specific range of pages.
  • Select from printers installed on your computer. You can use the Microsoft XPS Document Writer option to create these document types:
    • XML Paper Specification (XPS)
    • Open XML Paper Specification (OpenXPS)

Print the whole document

This example prints all pages of an XPS document. By default, the code will:

  1. Open a print dialog window that prompts the user to select a printer and start a print job.
  2. Instantiate an xref:System.Windows.Xps.Packaging.XpsDocument object with the content of the XPS document.
  3. Use the XpsDocument object to generate a xref:System.Windows.Documents.DocumentPaginator object that holds all pages of the XPS document.
  4. Call the xref:System.Windows.Controls.PrintDialog.PrintDocument%2A method, passing in the DocumentPaginator object, to send all pages to the specified printer.

:::code language="csharp" source="./snippets/how-to-display-print-dialog/csharp/MainWindow.xaml.cs" id="SampleCode1"::: :::code language="vb" source="./snippets/how-to-display-print-dialog/vb/MainWindow.xaml.vb" id="SampleCode1":::

Print a page range

Sometimes you'll only want to print a specific range of pages within an XPS document. To do this, we extend the abstract xref:System.Windows.Documents.DocumentPaginator class to add support for page ranges. By default, the code will:

  1. Open a print dialog window that prompts the user to select a printer, specify a range of pages, and start a print job.
  2. Instantiate an xref:System.Windows.Xps.Packaging.XpsDocument object with the content of the XPS document.
  3. Use the XpsDocument object to generate a default xref:System.Windows.Documents.DocumentPaginator object that holds all pages of the XPS document.
  4. Create an instance of an extended DocumentPaginator class that supports page ranges, passing in the default DocumentPaginator object and the xref:System.Windows.Controls.PrintDialog.PageRange struct returned by the xref:System.Windows.Controls.PrintDialog.
  5. Call the xref:System.Windows.Controls.PrintDialog.PrintDocument%2A method, passing in the instance of the extended DocumentPaginator class, to send the specified page range to the specified printer.

:::code language="csharp" source="./snippets/how-to-display-print-dialog/csharp/MainWindow.xaml.cs" id="SampleCode2"::: :::code language="vb" source="./snippets/how-to-display-print-dialog/vb/MainWindow.xaml.vb" id="SampleCode2":::

Tip

Although you can use the xref:System.Windows.Controls.PrintDialog.PrintDocument%2A method to print without opening the print dialog, for performance reasons, it's better to use the xref:System.Printing.PrintQueue.AddJob%2A method, or one of the many xref:System.Windows.Xps.XpsDocumentWriter.Write%2A and xref:System.Windows.Xps.XpsDocumentWriter.WriteAsync%2A methods of the xref:System.Windows.Xps.XpsDocumentWriter. For more about this, see How to print an XPS file and Printing documents overview.

See also