更改打印机默认纸张大小

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.

编程(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.

我使用了 thisPrinterSettings 类> codeproject 文章更改打印机设置.

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

可以使用 PrintDocument.PrinterSettings 检索打印机中可用的纸张尺寸.请参阅下面的示例代码以从打印机中检索可用的纸张尺寸并使用 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);
        }            
    }
}

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)