C# 如何使用 WebBrowser 控件以编程方式更改打印机设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/714507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:13:03  来源:igfitidea点击:

How do I programmatically change printer settings with the WebBrowser control?

c#winformsprintingwebbrowser-control

提问by Chris Doggett

I finally figured out how to print transformed XMLwithout prompting the user or showing an IE window, but now I need to specify a number of copies and possibly other printer settings.

我终于想出了如何在不提示用户或不显示 IE 窗口的情况下打印转换后的 XML,但现在我需要指定份数和可能的其他打印机设置。

Is there a way to programmatically change printer settings on a WebBrowser control?

有没有办法以编程方式更改 WebBrowser 控件上的打印机设置?

The code in question:

有问题的代码:

private static void PrintReport(string reportFilename)
{
    WebBrowser browser = new WebBrowser();

    browser.DocumentCompleted += browser_DocumentCompleted;

    browser.Navigate(reportFilename);
}

private static void browser_DocumentCompleted
    (object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = sender as WebBrowser;

    if (null == browser)
    {
        return;
    }

    browser.Print();

    browser.Dispose();
}

采纳答案by Austin Salonen

The only method I've had success with is modifying the registry on the fly (and changing them back to not affect anything else).

我成功的唯一方法是动态修改注册表(并将它们改回不影响其他任何东西)。

You can find the settings you need at "Software\Microsoft\Internet Explorer\PageSetup" under CurrentUser.

您可以在 CurrentUser 下的“Software\Microsoft\Internet Explorer\PageSetup”中找到您需要的设置。

To change the printer, you can use this:

要更改打印机,您可以使用:

using System.Management

public static bool SetDefaultPrinter(string defaultPrinter)
{
    using (ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer"))
    {
        using (ManagementObjectCollection objectCollection = objectSearcher.Get())
        {
            foreach (ManagementObject mo in objectCollection)
            {
                if (string.Compare(mo["Name"].ToString(), defaultPrinter, true) == 0)
                {
                    mo.InvokeMethod("SetDefaultPrinter", null, null);
                    return true;
                }
            }
        }
    }
    return false;
}



As for the number of copies, you can always put the WebBrowser.Print in a while loop.

至于份数,您可以随时将 WebBrowser.Print 放入 while 循环中。

回答by lilmoe

you need to change registry settings via code to change settings for internet explorer or the web browser control. check out the link below, it describes how to do so, also if there's more options you need to alter using the registry, then use regedit.exe to find what other keys internet explorer has.

您需要通过代码更改注册表设置以更改 Internet Explorer 或 Web 浏览器控件的设置。查看下面的链接,它描述了如何执行此操作,此外,如果您需要使用注册表更改更多选项,请使用 regedit.exe 查找 Internet Explorer 具有的其他键。

http://support.microsoft.com/kb/236777

http://support.microsoft.com/kb/236777

ps: you should note that any changes you make via your code to internet explorer's registry settings will persist on your system/user account.

ps:您应该注意,您通过代码对 Internet Explorer 注册表设置所做的任何更改都将保留在您的系统/用户帐户上。

回答by cessationoftime

This worked well for me, however I am on .NET 3.5

这对我来说效果很好,但是我使用的是 .NET 3.5

this.webBrowser1.ShowPrintDialog();

回答by Nino Mirza Mu?i?

            string strKey = "Software\Microsoft\Internet Explorer\PageSetup";
        bool bolWritable = true;

        RegistryKey oKey = Registry.CurrentUser.OpenSubKey(strKey, bolWritable);
        Console.Write(strKey);

        if (stringToPrint.Contains("Nalog%20za%20sluzbeno%20putovanje_files"))
        {
            oKey.SetValue("margin_bottom", 15);
            oKey.SetValue("margin_top", 0.19);
        }
        else
        {
            //Return onld walue
            oKey.SetValue("margin_bottom", 0.75);
            oKey.SetValue("margin_top", 0.75);
        }