Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change printer default paper size

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 ?

like image 254
Andy Avatar asked Feb 17 '14 10:02

Andy


People also ask

What is default printing paper size?

Default print jobs are formatted to fit an 8.5-inch by 11-inch piece of paper. Depending on what type of document you're printing and what program you're using to print it, it may be labeled “Letter.” A4 size paper, on the other hand, is closer to 8.25-inches by 11.75-inches.

How do I change the default settings on my printer?

Go to Devices > Printers & scanners > select a printer > Manage. Then select Set as default. If you don't see the Set as default option, the Let Windows manage my default printer option may be selected. You'll need to clear that selection before you can choose a default printer on your own.

How do I change the custom paper size on my HP printer?

Open the item you want to print, click File, and then click Print. In the Print window, click Preferences, Properties, or Printer Properties. Click the Paper/Quality tab, and then click Custom. Click New, type a name for the custom paper size, select the width and height, and then click OK to save the paper.


2 Answers

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);
        }            
    }
}
like image 141
Junaith Avatar answered Oct 02 '22 02:10

Junaith


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.

like image 36
Shashank Chaturvedi Avatar answered Oct 02 '22 00:10

Shashank Chaturvedi