如何使用C#设置打印机设置?

3

编辑 我试图重建我不再拥有的代码来展示。我认为这只是打印设置类的限制,不能通过使用对话框选择功能。看起来我应该能够配置和分配一个printerSettings对象到PrintDocument,然后打印那个PrintDocument...??? 我想错了吗?

再次编辑 我认为所有的setter都在'printerSettings.DefaultPageSettings'. 这将允许我修改打印设置。我还没有证明,但以后会。

PrintDocument pd = new PrintDocument();
pd.DocumentName = "test.doc";

PrinterSettings printerSettings = new PrinterSettings();
printerSettings.?? <- I want to set the printer setting here e.g. DL, A4, etc
pd.PrinterSettings = printerSettings;
pd.Print();

我已经在C#中生成了Word邮件合并文档(支票、信函、文件),但所有这些文档都需要不同的打印机设置(支票=自定义设置,信函=DL信封,文件=A4)。
我已经保存了这些设置,并且可以在加载打印机首选项对话框时访问它们,但我希望能够将其构建到代码中,而不是手动更改打印机设置。我已经查看了一些资料,似乎打印机设置类应该是解决方案,但我无法使其正常工作。
以下是我尝试实现的伪代码示例。
//create the mail merge
IList<Letter> letters = MailMerge.Create(enum.letters)
Printer.Print(letters) //<-- in here I am trying set the printing preferences to DL Env


//create the mail merge
IList<Document> docs = MailMerge.Create(enum.documents)
Printer.Print(docs) //<-- in here I am trying set the printing preferences to A4

任何帮助都会受到欢迎。
谢谢。
2个回答

3
你可以使用WMI。 我个人只有使用C#代码检索打印机属性的WMI经验,我没有尝试设置任何打印机属性,但我认为这应该是可能的。也许这些MSDN链接和代码可以帮助你入门。 WMI Tasks: Printers and Printing 展示了VB-script中的命令。 How To: Retrieve Collections of Managed Objects 展示了如何使用SelectQuery和枚举。 How To: Execute a Method展示了如何执行方法 : -)。 编辑:我刚注意到这篇StackOverflow文章:如何通过WebBrowser控件编程更改打印机设置...,似乎使用WMI来更改某些打印机设置。
我的检索代码如下:
    //using System.Management;

    private void GetPrinterProperties(object sender, EventArgs e)
    {
        // SelectQuery from:
        //    http://msdn.microsoft.com/en-us/library/ms257359.aspx
        // Build a query for enumeration of instances
        var query = new SelectQuery("Win32_Printer");
        // instantiate an object searcher
        var searcher = new ManagementObjectSearcher(query); 
        // retrieve the collection of objects and loop through it
        foreach (ManagementObject lPrinterObject in searcher.Get())
        {
            string lProps = GetWmiPrinterProperties(lPrinterObject);
            // some logging, tracing or breakpoint here...
        }
    }

    // log PrinterProperties for test-purposes
    private string GetWmiPrinterProperties(ManagementObject printerObject)
    {
        // Win32_Printer properties from:
        //    http://msdn.microsoft.com/en-us/library/aa394363%28v=VS.85%29.aspx
        return String.Join(",", new string[] {
                GetWmiPropertyString(printerObject, "Caption"),
                GetWmiPropertyString(printerObject, "Name"),
                GetWmiPropertyString(printerObject, "DeviceID"),
                GetWmiPropertyString(printerObject, "PNPDeviceID"),
                GetWmiPropertyString(printerObject, "DriverName"),
                GetWmiPropertyString(printerObject, "Portname"),
                GetWmiPropertyString(printerObject, "CurrentPaperType"),
                GetWmiPropertyString(printerObject, "PrinterState"),
                GetWmiPropertyString(printerObject, "PrinterStatus"),
                GetWmiPropertyString(printerObject, "Location"),
                GetWmiPropertyString(printerObject, "Description"),
                GetWmiPropertyString(printerObject, "Comment"),
            });
    }

    private string GetWmiPropertyString(ManagementObject mgmtObject, string propertyName)
    {
        if (mgmtObject[propertyName] == null)
        {
            return "<no "+ propertyName + ">";
        }
        else
        {
            return mgmtObject[propertyName].ToString();
        }
    }
}

1
    private void startPrintingButton_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (DialogResult.OK == ofd.ShowDialog(this))
        {
            PrintDocument pdoc = new PrintDocument();

            pdoc.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GK420d";
            pdoc.DefaultPageSettings.Landscape = true;
            pdoc.DefaultPageSettings.PaperSize.Height = 140;
            pdoc.DefaultPageSettings.PaperSize.Width = 104;

            Print(pdoc.PrinterSettings.PrinterName, ofd.FileName);
        }
    }

    private void Print(string printerName, string fileName)
    {
        try
        {
            ProcessStartInfo gsProcessInfo;
            Process gsProcess;

            gsProcessInfo = new ProcessStartInfo();
            gsProcessInfo.Verb = "PrintTo";
            gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
            gsProcessInfo.FileName = fileName;
            gsProcessInfo.Arguments = "\"" + printerName + "\"";
            gsProcess = Process.Start(gsProcessInfo);
            if (gsProcess.HasExited == false)
            {
                gsProcess.Kill();
            }
            gsProcess.EnableRaisingEvents = true;

            gsProcess.Close();
        }
        catch (Exception)
        {
        }
    }

你提供标签打印机的代码非常有帮助(因为99%的时间我需要处理打印机代码都是因为愚蠢的标签打印机)。 - KySoto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接