Set the default printer for printing programatically

You need to print the document but you don’t want to go to control panel or printer and faxes options and just want to get it changed automatically. You can even set the shared local network printer with this code.

Just with the code you can override the system settings and change the default printer for your system.

You need to use the namespace so in order to use the DLLImport attribute.

using System.Runtime.Interopservices;
//Set default printer to the one available with your system or network.
[DllImport("Winspool.drv")]
private static extern bool SetDefaultPrinter(string printerName);

Now on button click or on form load just change the printer you want to use while printing and then change it back to the one before.

//Set the default printer. I have set it as my Adobe PDF printer
SetDefaultPrinter("Adobe PDF");
comments powered by Disqus