Como: Chamar um diálogo de impressão
To provide the ability to print from you application, you can simply create and open a PrintDialog object.
Exemplo
The PrintDialog control provides a single entry point for UI, configuration, and XPS job submission. O controle é fácil de usar e pode ser instanciada usando Extensible Application Markup Language (XAML) marcação ou código. 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. O código de exemplo assume que há um arquivo FixedDocumentSequence.xps na raiz da unidade c: unidade.
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. Eles também terão a opção de selecionar o Microsoft xps Document Writer para criar um XML Paper Specification (XPS) arquivo em vez de impressão.
Observação
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. Para obter mais informações sobre isso, consulte Como: Programmatically Print XPS Files e.