更改打印机默认纸张尺寸

And*_*ndy 6 c# printing windows-xp windows-7

我在打印机上定义了几种自定义纸张尺寸(打印机默认设置).我需要能够选择其中一种格式作为默认格式.

程序化(C#)解决方案是理想的,但命令行也可以.

现在,我可以获得打印机上定义的纸张尺寸(名称/尺寸)列表,我可以找出哪个是默认值.

为了选择另一种格式作为默认值,我迄今为止唯一的解决办法是通过改变dmPaperSize场上DEVMODE结构; 但是我找不到与所需纸张格式相对应的正确值.所以我将dmPaperSize设置为0,然后递增它,直到打印机上出现正确的格式.这在一些打印机上需要很长时间.

是否有另一种方法可以在默认打印机上选择(按名称)默认的papaer格式?

Jun*_*ith 9

您在更改默认打印机设置方面的方向正确..NET不提供直接支持来更改打印机的默认设置.

我使用代码项目文章中的PrinterSettings类来更改打印机设置.

可以使用以下方法检索打印机可用的纸张尺寸PrintDocument.PrinterSettings.请参阅下面的示例代码,以便从打印机中检索可用的纸张,并使用PaperSize.RawKind更改打印机的纸张尺寸.

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);
        }            
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我同意,“PrinterData”未定义。它也不在 codeproject 类中。但是进入讨论部分并查看这篇关于如何创建缺少的类的文章:https://www.codeproject.com/Articles/6899/Changing-printer-settings-using-C?msg=5142354#xx5142354xx (2认同)

Sha*_*edi 5

以下代码将设置默认的打印机纸张大小:

PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 840, 1180);
pd.Print();
Run Code Online (Sandbox Code Playgroud)

关于如何使用PrintDocument进行打印,您可以参考此链接.

希望这可以帮助.