13

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.

BrokeMyLegBiking
  • 5,898
  • 14
  • 51
  • 66

2 Answers2

22

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.

Stewbob
  • 16,759
  • 9
  • 63
  • 107
  • 1
    Brilliant! I spent a whole day and couldn't figured it out. Thanks for the help. – BrokeMyLegBiking Apr 08 '13 at 22:28
  • 1
    By the way how do you specify a network printer like \\servername\printername – Praveena Jun 04 '14 at 16:27
  • @Praveena, I'm not sure what the exact syntax is for a network printer. I always just grab the string from the Print dialog box and use that. – Stewbob Jun 04 '14 at 17:39
  • 3
    Hi I got this working printDlg.PrintQueue = new PrintQueue(new PrintServer(@"\\servername"), "printername"); Your example helped me to think about this. – Praveena Jun 10 '14 at 15:09
  • @Praveena Will it work with `(new PrintServer(@"\\server IP")` instead of server name? – Yogen Darji May 30 '18 at 11:11
4

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

xPrintDialog.PrintQueue = New PrintQueue(New PrintServer("\\computer name"), "printer name")
Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63
Ladybugf8
  • 41
  • 1