Reputation: 974
I use
printDialog.PrintVisual(document, "doc");
to open a print dialog for the default printer when user clicks on a print button in my WPF application. The problem is that I want to know whether user clicked on the "Print" button in the external print dialog or "Cancel" button. I want to display respective messages based on user actions.
Is there a callback function implemented in the WPF printing process?
Thanks
Upvotes: 0
Views: 1105
Reputation: 13484
1. Create a PrintDialog
The following code creates a PrintDialog in WPF code.
// Create a PrintDialog
PrintDialog printDlg = new PrintDialog();
2. Create a FlowDocument
The FlowDocument object is used to create a FlowDocument. It has items such as a paragraph, run, line, image and so on. The following code snippet creates a FlowDocument and adds a line of text to the document.
// Create a FlowDocument dynamically.
FlowDocument doc = new FlowDocument(new Paragraph(new Run("Some text goes here")));
doc.Name = "FlowDoc";
3. Create an IDocumentPaginatorSource
The third step is to create an IDocumentPaginatorSource object from a FlowDocument that is pretty simple as listed here. You directly convert a FlowDocument to an IDocumentPaginatorSource.
// Create IDocumentPaginatorSource from FlowDocument
IDocumentPaginatorSource idpSource = doc;
4. Call PrintDialog.PrintDocument method
The last step is to call PrintDialog.PrintDocument method to call the print dialog that will allow you to select a printer and send document to the printer to print it. The PrintDocument method of PrintDialog takes a DocumentPaginator object that you can get from IDocumentPaginatorSource.DocumentPaginator property as listed in the following code:
// Call PrintDocument method to send document to printer
printDlg.PrintDocument(idpSource.DocumentPaginator, "Hello WPF Printing.");
Upvotes: 0
Reputation: 658
The ShowDialog method on the PrintDialog class returns a boolean indicating if the print button was pressed or not.
PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog())
printDialog.PrintVisual(document, "doc");
Upvotes: 1