Change the default paper size for the printer

I have several custom paper sizes defined on the printer (the printer is installed by default). I need to choose one of these formats as standard.

A software (C #) solution would be ideal, but on the command line would also be good.

Now I can get a list of paper sizes (name / sizes) defined on the printer, and I can find out which one is the default.

To choose a different default format, the only solution I still have is to change the dmPaperSize field in the devMode structure; BUT I cannot find out the correct value corresponding to the desired paper size. Therefore, I set dmPaperSize to 0 and increase it until the correct format appears on the printer. This takes a lot of time on some printers.

Is there any other way to select (by name) the default folder format on the default printer?

+6
source share
2 answers

You are in the right direction when changing the default printer settings .. NET does not provide direct support for changing the default printer settings.

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

Available paper sizes from the printer can be obtained using PrintDocument.PrinterSettings . See the sample code below for available documents from the printer and using PaperSize.RawKind to change the paper size 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); } } } 
+8
source

The following code will set the default paper size:

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

About how to print using PrintDocument, you can link to this link .

Hope this helps.

+5
source

All Articles