在WPF中使用System.Drawing.Printing.PrintDocument

时间:2009-11-01 17:31:19

标签: c# wpf printing

我有一个WPF应用程序,我使用外部库来生成文档。该库将文档作为System.Drawing.Printing.PrintDocument返回。如何在WPF中打印此文档?我可以直接使用Print()方法,但我需要允许用户选择打印机和设置。如果我使用WPF PrintDocument对话框,我无法像在WinForms对话框中那样将文档设置为它。文档。有没有办法将旧的PrintDocument转换为某种WPF友好形式?

WinForms方式:

// 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方式:

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

我也尝试在WPF中打开WinForms对话框,但这是不可能的。对话框没有显示。

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

我找到了答案。您必须将UseDialogEx对话框属性设置为true

答案 1 :(得分:-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();
}