WPF 打印 - 在 WPF PrintDialog 上自动设置打印机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15880175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:31:21  来源:igfitidea点击:

WPF Printing - Set the Printer Automataically on WPF PrintDialog

wpfprintingflowdocument

提问by BrokeMyLegBiking

I am trying to print a WPF FlowDocument to a particular printer, without prompting the user. The printer is a PDF converter.

我正在尝试将 WPF FlowDocument 打印到特定打印机,而不提示用户。打印机是 PDF 转换器。

This works great except that it prints to the default printer:

这很好用,除了它打印到默认打印机:

   PrintDialog pd = new PrintDialog();
   var doc = ((IDocumentPaginatorSource) RTB.Document).DocumentPaginator;
   // I would like to explicitly set the printer to print to here.
   pd.PrintDocument(doc, "Print Document");

In WinForms there is a System.Drawing.Printing.PrinterSettings object on document which has a PrinterName property which can be set to the printer I want, but I don't see that in WPF.

在 WinForms 中,文档上有一个 System.Drawing.Printing.PrinterSettings 对象,它有一个 PrinterName 属性,可以将其设置为我想要的打印机,但我在 WPF 中没有看到。

回答by Stewbob

You first need a reference in your project to System.Printing. Then you can use the following code right after you declare your PrintDialogobject.

您首先需要在项目中引用System.Printing. 然后,您可以在声明PrintDialog对象后立即使用以下代码。

pd.PrintQueue = new PrintQueue(new PrintServer(), "The exact name of my printer");

The PrintQueueis an object that represents the printer and everything else about that print queue.

PrintQueue是表示关于打印队列的打印机和其他一切的对象。

回答by Ladybugf8

This worked for me, when I used a shared network printer:

当我使用共享网络打印机时,这对我有用:

xPrintDialog.PrintQueue = New PrintQueue(New PrintServer("\computer name"), "printer name")