Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Printing - Set the Printer Automataically on WPF PrintDialog

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

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.

like image 476
BrokeMyLegBiking Avatar asked Apr 08 '13 13:04

BrokeMyLegBiking


2 Answers

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

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

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

like image 179
Stewbob Avatar answered Oct 22 '22 12:10

Stewbob


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

xPrintDialog.PrintQueue = New PrintQueue(New PrintServer("\\computer name"), "printer name")
like image 35
Ladybugf8 Avatar answered Oct 22 '22 10:10

Ladybugf8