How can I use c# to set printersettings?

kurasa picture kurasa · Dec 16, 2010 · Viewed 16.6k times · Source

EDIT I have tried to reconstruct code I no longer have to show. I think it is just a klimitation of the printersetting class not exposing functionality that can be selected by using a dialog. It seems I should be able to configure and assign a printerSettings object to a PrintDocument and then print that PrintDocument...??? Am I not thinking right here or??

EDIT AGAIN I think all the setters sit of the 'printerSettings.DefaultPageSettings'. This will allow me to modify the printersettings. I haven't proved it yet but will later

PrintDocument pd = new PrintDocument();
pd.DocumentName = "test.doc";

PrinterSettings printerSettings = new PrinterSettings();
printerSettings.?? <- I want to set the printer setting here e.g. DL, A4, etc
pd.PrinterSettings = printerSettings;
pd.Print();

I have generate word mail merge documents in c# (cheques, letters, documents) but all of these require different printer settings (cheque = custom setting, letters = DL Env,documents= A4)

I have these settings saved and can access them when loading the printer preferences dialog but I would like to be able to build it into code instead of manually changing the printer settings. I've looked around and it seems printer settings class should be it but I can't seem to get it to work.

example psuedo code of what I am trying to do

//create the mail merge
IList<Letter> letters = MailMerge.Create(enum.letters)
Printer.Print(letters) //<-- in here I am trying set the printing preferences to DL Env


//create the mail merge
IList<Document> docs = MailMerge.Create(enum.documents)
Printer.Print(docs) //<-- in here I am trying set the printing preferences to A4

any help appreciated.

thanks

Answer

Yahoo Serious picture Yahoo Serious · Jan 4, 2011

You could probably use WMI. My only WMI-experience is some C#-code for WMI to retrieve some printer-properties, I haven't tried to set any printer-properties, but I think that it should be possible. Maybe these MSDN-links and code can help you get started.

WMI Tasks: Printers and Printing shows the commands in VB-script. How To: Retrieve Collections of Managed Objects shows how to use a SelectQuery and enumeration. How To: Execute a Method shows how to execute a method :-).

EDIT: I just noticed this StackOverflow article: How do I programatically change printer settings ..., that seems to use WMI to change some printer-settings.

My retrieve-code looks like this:

    //using System.Management;

    private void GetPrinterProperties(object sender, EventArgs e)
    {
        // SelectQuery from:
        //    http://msdn.microsoft.com/en-us/library/ms257359.aspx
        // Build a query for enumeration of instances
        var query = new SelectQuery("Win32_Printer");
        // instantiate an object searcher
        var searcher = new ManagementObjectSearcher(query); 
        // retrieve the collection of objects and loop through it
        foreach (ManagementObject lPrinterObject in searcher.Get())
        {
            string lProps = GetWmiPrinterProperties(lPrinterObject);
            // some logging, tracing or breakpoint here...
        }
    }

    // log PrinterProperties for test-purposes
    private string GetWmiPrinterProperties(ManagementObject printerObject)
    {
        // Win32_Printer properties from:
        //    http://msdn.microsoft.com/en-us/library/aa394363%28v=VS.85%29.aspx
        return String.Join(",", new string[] {
                GetWmiPropertyString(printerObject, "Caption"),
                GetWmiPropertyString(printerObject, "Name"),
                GetWmiPropertyString(printerObject, "DeviceID"),
                GetWmiPropertyString(printerObject, "PNPDeviceID"),
                GetWmiPropertyString(printerObject, "DriverName"),
                GetWmiPropertyString(printerObject, "Portname"),
                GetWmiPropertyString(printerObject, "CurrentPaperType"),
                GetWmiPropertyString(printerObject, "PrinterState"),
                GetWmiPropertyString(printerObject, "PrinterStatus"),
                GetWmiPropertyString(printerObject, "Location"),
                GetWmiPropertyString(printerObject, "Description"),
                GetWmiPropertyString(printerObject, "Comment"),
            });
    }

    private string GetWmiPropertyString(ManagementObject mgmtObject, string propertyName)
    {
        if (mgmtObject[propertyName] == null)
        {
            return "<no "+ propertyName + ">";
        }
        else
        {
            return mgmtObject[propertyName].ToString();
        }
    }
}