How to: Invoke a Print Dialog

To provide the ability to print from you application, you can simply create and open a PrintDialog object.

Example

The PrintDialog control provides a single entry point for UI, configuration, and XPS job submission. The control is easy to use and can be instantiated by using Extensible Application Markup Language (XAML) markup or code. The following example demonstrates how to instantiate and open the control in code and how to print from it. It also shows how to ensure that the dialog will give the user the option of setting a specific range of pages. The example code assumes that there is a file FixedDocumentSequence.xps in the root of the C: drive.

        Private Sub InvokePrint(ByVal sender As Object, ByVal e As RoutedEventArgs)
                ' Create the print dialog object and set options
                Dim pDialog As New PrintDialog()
                pDialog.PageRangeSelection = PageRangeSelection.AllPages
                pDialog.UserPageRangeEnabled = True

                ' Display the dialog. This returns true if the user presses the Print button.
                Dim print? As Boolean = pDialog.ShowDialog()
                If print = True Then
                    Dim xpsDocument As New XpsDocument("C:\FixedDocumentSequence.xps", FileAccess.ReadWrite)
                    Dim fixedDocSeq As FixedDocumentSequence = xpsDocument.GetFixedDocumentSequence()
                    pDialog.PrintDocument(fixedDocSeq.DocumentPaginator, "Test print job")
                End If
        End Sub
private void InvokePrint(object sender, RoutedEventArgs e)
    {
        // Create the print dialog object and set options
        PrintDialog pDialog = new PrintDialog();
        pDialog.PageRangeSelection = PageRangeSelection.AllPages;
        pDialog.UserPageRangeEnabled = true;

        // Display the dialog. This returns true if the user presses the Print button.
        Nullable<Boolean> print = pDialog.ShowDialog();
        if (print == true)
        {
            XpsDocument xpsDocument = new XpsDocument("C:\\FixedDocumentSequence.xps", FileAccess.ReadWrite);
            FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();
            pDialog.PrintDocument(fixedDocSeq.DocumentPaginator, "Test print job");
        }
    }

Once the dialog is open, users will be able to select from the printers installed on their computer. They will also have the option of selecting the Microsoft XPS Document Writer to create an XML Paper Specification (XPS) file instead of printing.

Note

The System.Windows.Controls.PrintDialog control of WPF, which is discussed in this topic, should not be confused with the System.Windows.Forms.PrintDialog component of Windows Forms.

Strictly speaking, you can use the PrintDocument method without ever opening the dialog. In that sense, the control can be used as an unseen printing component. But for performance reasons, it would be better to use either the AddJob method or one of the many Write and WriteAsync methods of the XpsDocumentWriter. For more about this, see How to: Programmatically Print XPS Files and .

See Also

Reference

PrintDialog

Concepts

Documents in WPF

Printing Overview

Other Resources

Microsoft XPS Document Writer