Change printer default paper size

前端 未结 2 765
一个人的身影
一个人的身影 2020-12-29 16:14

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 program

相关标签:
2条回答
  • 2020-12-29 17:12

    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);
            }            
        }
    }
    
    0 讨论(0)
  • 2020-12-29 17:17

    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.

    0 讨论(0)
提交回复
热议问题