* 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]>
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 |
|
|
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:
- Open a print dialog window that prompts the user to select a printer and start a print job.
- Instantiate an xref:System.Windows.Xps.Packaging.XpsDocument object with the content of the XPS document.
- Use the
XpsDocumentobject to generate a xref:System.Windows.Documents.DocumentPaginator object that holds all pages of the XPS document. - Call the xref:System.Windows.Controls.PrintDialog.PrintDocument%2A method, passing in the
DocumentPaginatorobject, 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:
- Open a print dialog window that prompts the user to select a printer, specify a range of pages, and start a print job.
- Instantiate an xref:System.Windows.Xps.Packaging.XpsDocument object with the content of the XPS document.
- Use the
XpsDocumentobject to generate a default xref:System.Windows.Documents.DocumentPaginator object that holds all pages of the XPS document. - Create an instance of an extended
DocumentPaginatorclass that supports page ranges, passing in the defaultDocumentPaginatorobject and the xref:System.Windows.Controls.PrintDialog.PageRange struct returned by the xref:System.Windows.Controls.PrintDialog. - Call the xref:System.Windows.Controls.PrintDialog.PrintDocument%2A method, passing in the instance of the extended
DocumentPaginatorclass, 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.