共用方式為


如何:叫用列印對話方塊

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

範例

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

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

開啟對話方塊之後,使用者將能夠從其電腦上所安裝的印表機進行選取。 他們也可以選擇選取 Microsoft XPS Document Writer 來建立 XML 文件規格 (XPS) 檔案,而不是列印。

注意

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

嚴格來說,您可以使用 PrintDocument 方法,而不需要開啟對話方塊。 從這個意義上說,控制項可以用來用作看不見的列印元件。 但基於效能考量,最好使用 AddJob 方法或是 Write 的許多 WriteAsyncXpsDocumentWriter 方法中的其中一個。 如需此作業的詳細資訊,請參閱以程式設計方式列印 XPS 檔

另請參閱