![](/img/trans.png)
[英]Programmatically changing the destination printer for a WinForms WebBrowser control
[英]How do I programmatically change printer settings with the WebBrowser control?
我最終想出了如何在不提示用戶或不顯示IE窗口的情況下打印轉換后的XML的方法 ,但是現在我需要指定許多副本以及可能的其他打印機設置。
有沒有辦法以編程方式更改WebBrowser控件上的打印機設置?
有問題的代碼:
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();
}
我成功的唯一方法是即時修改注冊表(並將注冊表改回不影響其他任何內容)。
您可以在CurrentUser下的“ Software \\ Microsoft \\ Internet Explorer \\ PageSetup”中找到所需的設置。
要更換打印機,可以使用以下方法:
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;
}
至於份數,您總是可以將WebBrowser.Print放入while循環中。
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);
}
您需要通過代碼更改注冊表設置,以更改Internet Explorer或Web瀏覽器控件的設置。 請查看下面的鏈接,它描述了如何執行此操作,以及是否還需要使用注冊表來更改其他選項,然后使用regedit.exe查找Internet Explorer的其他項。
http://support.microsoft.com/kb/236777
ps:您應該注意,通過代碼對Internet Explorer的注冊表設置所做的任何更改都將保留在您的系統/用戶帳戶中。
這對我來說效果很好,但是我在.NET 3.5上
this.webBrowser1.ShowPrintDialog();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.