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.
You may be asking yourself, what is a default printer? A default printer is a printer all print jobs are sent to unless otherwise specified. Having a default printer prevents a program from asking what printer they want to use each time they print.
Click the Windows icon ( ), click Devices and Printers. The Devices and Printers window opens. Right-click your HP product, and then click Set as default printer.
The setting is available in Windows 10 version 1511 (build 10586) or later. Go to Settings > Devices > Printers & scanners, and switch off the “Let Windows my default printer” setting. Alternatively, you can also disable the setting through Group Policy if you have a larger network domain environment.
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.
It is automatically initialized to the default printer. Do nothing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With