简体   繁体   English

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

[英]Using System.Drawing.Printing.PrintDocument in WPF

I have a WPF application and I use external library for generating documents. 我有一个WPF应用程序,我使用外部库来生成文档。 This library returns document as System.Drawing.Printing.PrintDocument. 该库将文档作为System.Drawing.Printing.PrintDocument返回。 How can I print this document in WPF? 如何在WPF中打印此文档? I can use Print() method directly, but I need to allow user to select printer and settings. 我可以直接使用Print()方法,但我需要允许用户选择打印机和设置。 If I use WPF PrintDocument dialog, I can't set my document to it as in WinForms dialog.Document. 如果我使用WPF PrintDocument对话框,我无法像在WinForms对话框中那样将文档设置为它。文档。 Is there a way to convert old PrintDocument to some WPF friendly form? 有没有办法将旧的PrintDocument转换为WPF友好形式?

WinForms way: 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 way: WPF方式:

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. 我也尝试在WPF中打开WinForms对话框,但这是不可能的。 Dialog is just not shown. 对话框没有显示。

Thanks for any help. 谢谢你的帮助。

I found an answer. 我找到了答案。 You have to set UseDialogEx dialog property to true . 您必须将UseDialogEx对话框属性设置为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();
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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