简体   繁体   中英

Using System.Drawing.Printing.PrintDocument in WPF

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

WinForms way:

// 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 way:

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 WinForms dialog in WPF but it is not possible. Dialog is just not shown.

Thanks for any help.

I found an answer. You have to set UseDialogEx dialog property to true .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM