Change printer default paper size
Asked Answered
M

3

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 ?

Mho answered 17/2, 2014 at 10:28 Comment(1)
Did you try the solution I suggested?Selfliquidating
A
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);
        }            
    }
}
Androsterone answered 4/3, 2014 at 7:44 Comment(9)
PaperSize.RawKind is exactly was I was looking for, Thank you!Mho
Cannot find PrinterData class.Groovy
@Groovy - Check the codeproject link for the class. Its just a example code snippet.Androsterone
Not sure where you found PrinterSettings.ChangePrinterSetting() or PrinterSettings.GetPrinterSettings() methods, but they are not in MSDN msdn.microsoft.com/en-us/library/… Is this some sort of special object / custom class ??Refuse
@SanuelJackson The PrinterSettings class is from the CodeProject article (link in answer) not from .NET framework.Androsterone
Where is PrinterData class defined?Jat
@Jack, check the codeproject link for the class. Its just a example code snippet.Androsterone
@Androsterone I did check the code project and that couldn't find such class there.Jat
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: codeproject.com/Articles/6899/…Whereof
S
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.

Selfliquidating answered 28/2, 2014 at 19:36 Comment(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.Mho
B
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.

Bastinado answered 13/3, 2023 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.