共用方式為


HOW TO:叫用列印對話方塊

若要提供從應用程式進行列印的功能,則只需要建立並開啟 PrintDialog 物件。

範例

PrintDialog 控制項可提供 UI、組態與 XPS 工作提交的單一進入點。 這個控制項十分容易使用,而且可以使用Extensible Application Markup Language (XAML) 標記或程式碼具現化 (Instantiate)。 下列範例示範如何使用程式碼具現化和開啟控制項,以及如何從中進行列印。 也會顯示如何確保對話方塊提供選項,讓使用者設定特定範圍的頁數。 這個範例程式碼假設在 C: 磁碟機的根目錄中有 FixedDocumentSequence.xps 檔案。

        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");
        }
    }

開啟對話方塊之後,使用者就可以從他們電腦上安裝的印表機中進行選取。 他們也可以選擇選取 Microsoft XPS Document Writer (英文) 來建立 XML Paper Specification (XPS) 檔案,而不是進行列印。

注意事項注意事項

本主題討論之 WPF 的 System.Windows.Controls.PrintDialog 控制項,不應該與 Windows Forms 的 System.Windows.Forms.PrintDialog 元件混淆。

嚴格來說,您可以使用 PrintDocument 方法,而不需開啟對話方塊。 就該意義而言,控制項可以用做看不見的列印元件。 但為了效能理由,最好使用 AddJob 方法或其中一個 Write,以及 XpsDocumentWriterWriteAsync 方法。 如需此方面的詳細資訊,請參閱 HOW TO:以程式設計方式列印 XPS 檔

請參閱

參考

PrintDialog

概念

WPF 中的文件

列印概觀

其他資源

Microsoft XPS Document Writer