更改打印机的默认纸张大小

更改打印机的默认纸张大小

本文介绍了更改打印机的默认纸张大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台打印机(打印机设置为默认值)上定义多个自定义纸张尺寸。我需要能够选择这些格式作为默认一一。

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.

一个编程(C#)解决方案将是理想的,但一个命令行之一将是也没关系

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.

为了选择另一种格式作为默认值,我至今唯一的办法就是通过改变在 DEVMODE在 dmPaperSize 字段结构;但我不能找出对应于所需纸张格式正确的值。因此,我将 dmPaperSize 0,并增加它,直到出现在打印机上正确的格式。这需要某些打印机上很长的时间。

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 ?

推荐答案

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

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.

我用 PrinterSettings 类从 CodeProject上的文章,以更改打印机设置。

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

从打印机可用的纸张尺寸可以使用 PrintDocument.PrinterSettings 进行检索。请参见下面的示例代码从打印机中取出可用papersizes并使用 PaperSize.RawKind 更改打印机的纸张大小。

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);
        }
    }
}

这篇关于更改打印机的默认纸张大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:16