8

How do you set PrintDocument.PrinterSettings.PrinterName to be the default printer?

I am not talking about setting the default printer in the operating system. Rather, I am talking about setting the PrintDocument object so that it prints to the default printer.

oberfreak
  • 1,761
  • 13
  • 20
Craig Johnston
  • 7,037
  • 15
  • 37
  • 47
  • possible duplicate of [How do I set the windows default printer in C#?](http://stackoverflow.com/questions/971604/how-do-i-set-the-windows-default-printer-in-c) – Doc Brown Mar 02 '11 at 06:57
  • 2
    @Doc Brown: I don't think this is a duplicate. The question you linked to is about setting the default printer for the entire operating system. This question *appears* to be about setting the application to print to the system's default printer. @Craig: Care to weigh in about what you're actually asking? – Cody Gray Mar 02 '11 at 06:59
  • @Cody: I don't like questions where I have to guess what the OP could have meant. – Doc Brown Mar 02 '11 at 07:02
  • @Doc: Agreed, but that's a different close option. ;-) – Cody Gray Mar 02 '11 at 07:03
  • @Craig - I believe your question is answered? - please mark the answer as appropriate – Matt Wilko Sep 27 '11 at 11:32
  • @Matt: Matt, is not answered because no one has told me how to set the PrintDocument object back to the default printer. I don't want to have to create a new PrintDocument object. – CJ7 Dec 01 '11 at 05:13
  • @DocBrown: how could you not understand what the OP meant? The question asks how to set PrintDocument.PrinterSettings.PrinterName to the default printer. What could be clearer than that? – CJ7 Dec 01 '11 at 05:18
  • @CraigJ don't worry, the intentions here are good and simply to make sure your question was different from existing ones. I have tried to clarify your intentions in my answer below by listing the conditions under which this is a valid concern. Hopefully my code snippet addresses your question more specifically. – BenSwayne Dec 01 '11 at 06:09

6 Answers6

12

If I'm understanding correctly, you would like to be able to reset the PrinterName to the default printer (1) without recreating your PrintDocument and, (2) after you may have already set it to something else or, (3) when the default printer may have changed since the time when the PrintDocument was first created (so you can't rely on simply caching the defaults provided by the target instance after initial construction).

In this case a search for "C# get default printer name" turns up the following excellent post on stackoverflow: What's the best way to get the default printer in .NET

Building on the sample provided in top voted answer and considering that you will already have a pre-existing PrintDocument with some settings you don't want to recreate; you could create a new instance of the PrinterSettings class, for the sole purposes of copying out the default printer name.

// Create a new instance of the PrinterSettings class, which 
// we will only use to fetch the default printer name
System.Drawing.Printing.PrinterSettings newSettings = new System.Drawing.Printing.PrinterSettings();

// Copy the default printer name from our newSettings instance into our 
// pre-existing PrintDocument instance without recreating the 
// PrintDocument or the PrintDocument's PrinterSettings classes.
existingPrintDocumentInstance.PrinterSettings.PrinterName = newSettings.PrinterName;

You can review the linked post for alternative techniques such as WMI, but I think this is the simplest and cleanest solution for you.

Community
  • 1
  • 1
BenSwayne
  • 16,524
  • 3
  • 57
  • 75
  • thanks, but I was after a solution that didn't require the creation of a new PrintDocument object. – CJ7 Dec 01 '11 at 07:35
  • @Craigj I very explicitly showed that I am **NOT** creating another PrintDocument object. That is why I named it 'existingPrintDocumentInstance' please replace that with your existing instance of PrintDocument. – BenSwayne Dec 01 '11 at 16:15
  • @CraigJ did you re-check the answer provided? Your comment suggested you did not quite understand what was happening in my code sample. If this doesn't meet your needs, you need to edit and further clarify your requirements. – BenSwayne Dec 06 '11 at 00:33
5

It is automatically initialized to the default printer. Do nothing.

Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
2
GetDefaultPrinter() 

{ PrinterSettings settings = new PrinterSettings(); 

foreach (string printer in PrinterSettings.InstalledPrinters) 

{ settings.PrinterName = printer; 

if (settings.IsDefaultPrinter) 

return printer; 

} 

return string.Empty; 

}
Chief
  • 874
  • 9
  • 26
  • I used something like this to find the name of the default printer, but then I found this remark in the MSDN documentation: "IsDefaultPrinter always returns false when you explicitly set the PrinterName property to a string value other than null." So it seems the above code is soon to become incorrect, even though it also seems to work right now. – Peter Jul 24 '12 at 11:05
0

I assume you have set the default printer at the OS level. When you initiate a print from your code, it by defualt goes to Default Printer. You don't have to set it explicitly.

This happend for each print request. I mean if you have set the print to another printer and now you want to go to the default printer, just remove the explicit setting and it will again go to the default printer.

HTH

sajoshi
  • 2,653
  • 1
  • 15
  • 22
0

Correct me if I am wrong but you are looking to get the name of the default printer and then setting PrintDocument.PrinterSettings.PrinterName to this.

When you use PrintDocument.PrinterSettings.PrinterName this uses the default printer by default.

matino
  • 15,709
  • 6
  • 44
  • 55
Nollaig
  • 1,087
  • 1
  • 11
  • 26
  • PrintDocument.PrinterSettings.PrinterName can be changed in the program. How do you set it back to the default printer? – CJ7 Dec 01 '11 at 11:10
  • One way is to create method shown below and call it. using System.Drawing.Printing; string GetDefaultPrinter() { PrinterSettings settings = new PrinterSettings(); foreach (string printer in PrinterSettings.InstalledPrinters) { settings.PrinterName = printer; if (settings.IsDefaultPrinter) return printer; } return string.Empty; } – Nollaig Dec 01 '11 at 11:58
  • @Marijn - who's that comment for. – Nollaig Dec 01 '11 at 16:06
  • @CraigJ - The most efficient option is to get the default printer name using the PrinterSettings. What you can also do if you don't want to do that is to save the default printer when you create the PrintDocument initially and use it later when needed. – Nollaig Dec 01 '11 at 16:13
  • @nollaig That comment was for you :) – Marijn Dec 01 '11 at 18:03
0

By default you would be landing on default printer if you do not set anything on your object. Here is the official source you were looking for: MSDN Link to PrintDocument Class

Mark the sentence written just above the example: "The following code example prints the file named C:\My Documents\MyFile.txt on the default printer."

HTH

Community
  • 1
  • 1
Rahul
  • 310
  • 1
  • 3
  • 14
  • What about if I change the printer to something else. How do I set it back to the default printer? – CJ7 Dec 05 '11 at 11:16
  • I think you are getting confused with what is default printer. Only the OS controls your default printer (the one you can view from Control Panel -> Printer Settings). Even if you switch printers while printing documents, you do not actually change the default printer. So the next time you need to print a document, you would again be hitting the default printer for your document. – Rahul Dec 06 '11 at 06:28
  • In case you need your document to be sent to a very specific printer (bad design decision). You can loop through InstalledPrinters and save the printer object you want to use for printing. – Rahul Dec 06 '11 at 06:41