Using System.Drawing.Printing.PrintDocument in WPF

I have a WPF application and I use an external library to generate documents. This library returns the document as System.Drawing.Printing.PrintDocument. How to print this document in WPF? I can directly use the Print () method, but I need to allow the user to select the printer and settings. If I use the WPF PrintDocument dialog, I cannot set my document for it, as in the WinForms.Document dialog box. Is there a way to convert an old PrintDocument into some friendly WPF form?

WinForms method:

// get document for printing
PrintDocument document = exporter.GetPrintDocument();
System.Windows.Forms.PrintDialog dialog = new System.Windows.Forms.PrintDialog();
dialog.Document = document;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    document.Print();
}

WPF method:

System.Windows.Controls.PrintDialog dialog = new System.Windows.Controls.PrintDialog();
if (dialog.ShowDialog() == true)
{
    // how to print old PrintDocument???
    dialog.PrintDocument(...);
}

I also tried to open the WinForms dialog in WPF, but that is not possible. The dialog is simply not shown.

Thanks for any help.

+3
2

. UseDialogEx true.

+1
MessageBox.Show(printDialog1.PrinterSettings.PrinterName);
printDialog1.PrinterSettings.PrintFileName = "A.txt"; 
MessageBox.Show(printDialog1.PrinterSettings.PrintFileName);   

printDialog1.ShowDialog();
printDocument1.DocumentName = "A.txt";
if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    printDocument1.Print();
} 
-1

Source: https://habr.com/ru/post/1721630/


All Articles