7

I have several custom paper sizes defined on a printer(the printer is set as default). I need to be able to select one of these formats as the default one.

A programmatic(C#) solution would be ideal, but a command line one would be ok too.

Right now, I am able to get the list of paper sizes(name/dimensions) defined on the printer, and I can find out which one is the default.

In order to select another format as default, the only solution I have so far is by changing the dmPaperSize field on the devMode structure; BUT I cannot find out the correct value that corresponds to the desired paper format. So I set dmPaperSize to 0, and increment it, until the correct format appears on the printer. This takes a very long time on some printers.

Is there another way to select(by name) the default papaer format on the default printer ?

Andy
  • 3,631
  • 2
  • 23
  • 32

3 Answers3

9

You are in the right direction in changing the default printer settings. .NET doesn't provide direct support to change the default settings of a printer.

I used the PrinterSettings class from this codeproject article to change the printer settings.

The available paper sizes from the printer can be retrieved using the PrintDocument.PrinterSettings. See the sample code below for retrieving the available papersizes from the printer and using the PaperSize.RawKind for changing the papersize of the printer.

public class PrinterSettingsDlg : Form
{
    PrinterSettings ps = new PrinterSettings();
    Button button1 = new Button();
    ComboBox combobox1 = new ComboBox();
    public PrinterSettingsDlg()
    {
        this.Load += new EventHandler(PrinterSettingsDlg_Load);
        this.Controls.Add(button1);
        this.Controls.Add(combobox1);
        button1.Dock = DockStyle.Bottom;
        button1.Text = "Change Printer Settings";
        button1.Click += new EventHandler(button1_Click);
        combobox1.Dock = DockStyle.Top;
    }

    void button1_Click(object sender, EventArgs e)
    {
        PrinterData pd = ps.GetPrinterSettings(PrinterName);
        pd.Size = ((PaperSize)combobox1.SelectedItem).RawKind;
        ps.ChangePrinterSetting(PrinterName, pd);
    }

    void PrinterSettingsDlg_Load(object sender, EventArgs e)
    {
        PrintDocument pd = new PrintDocument();
        pd.PrinterSettings.PrinterName = // printer name
        combobox1.DisplayMember = "PaperName";
        foreach (PaperSize item in pd.PrinterSettings.PaperSizes)
        {
            combobox1.Items.Add(item);
        }            
    }
}
Junaith
  • 3,298
  • 24
  • 34
  • PaperSize.RawKind is exactly was I was looking for, Thank you! – Andy Mar 04 '14 at 10:06
  • Cannot find PrinterData class. – digz6666 Jan 15 '15 at 06:43
  • @digz6666 - Check the codeproject link for the class. Its just a example code snippet. – Junaith Jan 15 '15 at 06:52
  • Not sure where you found `PrinterSettings.ChangePrinterSetting()` or `PrinterSettings.GetPrinterSettings()` methods, but they are not in MSDN https://msdn.microsoft.com/en-us/library/system.drawing.printing.printersettings_properties(v=vs.110).aspx Is this some sort of special object / custom class ?? – Kraang Prime Apr 05 '16 at 11:15
  • @SanuelJackson The `PrinterSettings` class is from the CodeProject article (link in answer) not from .NET framework. – Junaith Apr 06 '16 at 08:17
  • Where is `PrinterData` class defined? – Jack Jul 01 '19 at 20:28
  • @Jack, check the codeproject link for the class. Its just a example code snippet. – Junaith Jul 08 '19 at 06:07
  • @Junaith I did check the code project and that couldn't find such class there. – Jack Jul 08 '19 at 17:48
  • 2
    I agree, `PrinterData` is not defined. It is also not in the codeproject class. But go into the discussion section and see this post on how to create the missing class: https://www.codeproject.com/Articles/6899/Changing-printer-settings-using-C?msg=5142354#xx5142354xx – André Gollubits Jan 04 '21 at 10:18
6

The following code would set the default printer papersize:

PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 840, 1180);
pd.Print();

On how to print using PrintDocument you could refer this link.

Hope this helps.

Shashank Chaturvedi
  • 2,756
  • 19
  • 29
  • 1
    Although the question is about _selecting_ an existing format as default(not about printing), this code only works on some printers. BUT I am not trying to print, I want to change the _default_ _settings_ on the printer, so that when a document is sent(by any other program), the new format will be used. – Andy Mar 03 '14 at 10:24
0

For me, This line gave a casting error from Devmode to PrinterData

PrinterData pd = ps.GetPrinterSettings(PrinterName);

So this is what I did instead of using that function.

string deviceToUse = "EPSON LQ-590II"; //Printer to look for
string paperToUse = "DOT MATRIX HALF"; //Page size to look for
int paperSizeRawKind = 0; //Variable for paper size
PrintDocument printDocument = new PrintDocument();
PrinterSettings ps = new PrinterSettings();

//Iterate through all printers
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
   //Check if printer matches to what I want
   if (printer.Contains(deviceToUse))
   {
       //Iterate through all paper sizes for that printer
       for (int i = 0; i < printDocument.PrinterSettings.PaperSizes.Count; i++)
       {
          //Check if paper size matches what I want
          if (printDocument.PrinterSettings.PaperSizes[i].ToString().Contains(paperToUse))
          {
              //Set Paper Size RawKind here
              paperSizeRawKind = printDocument.PrinterSettings.PaperSizes[i].RawKind;
           }
        }

        printDocument.PrinterSettings.PrinterName = printer;
        PrinterData printData = new PrinterData();

        printData.Size = paperSizeRawKind;
        ps.ChangePrinterSetting(printer, printData);
    }
}

This code is called in a button click event.