如何使用WebBrowser控件打印CSS应用的背景图像

时间:2020-03-05 18:48:56  来源:igfitidea点击:

我在winforms中使用webbrowser控件,并且发现我在CSS中应用的背景图像未包含在打印输出中。

是否有办法使Web浏览器也打印显示的文档的背景?

编辑:
由于我想以编程方式执行此操作,因此我选择了以下解决方案:

using Microsoft.Win32;

...

RegistryKey regKey = Registry.CurrentUser
                    .OpenSubKey("Software")
                    .OpenSubKey("Microsoft")
                    .OpenSubKey("Internet Explorer")
                    .OpenSubKey("Main");

//Get the current setting so that we can revert it after printjob
var defaultValue = regKey.GetValue("Print_Background");
regKey.SetValue("Print_Background", "yes");

//Do the printing

//Revert the registry key to the original value
regKey.SetValue("Print_Background", defaultValue);

处理此问题的另一种方法可能是只读取该值,并在打印之前通知用户自己调整此值。我必须同意这样调整注册表不是一个好习惯,因此我愿意接受任何建议。

感谢所有反馈

解决方案

回答

默认情况下,浏览器根本不打印背景图像。

在Firefox中

* File > Page Setup > Check Off "Print Background"
* File > Print Preview

在IE中

* Tools > Internet Options > Advanced > Printing
* Check Off "Print Background Images and Colors"

在歌剧中

* File > Print Options > Check Off "Print Page Background"
* File > Print Preview (You may have to scroll down/up to see it refresh)

回答

如果要更改重要的系统设置,请确保先阅读当前设置并在完成后将其还原。

首先,我认为这是一种非常不好的做法,但是如果我们必须这样做,那就好些。

Registry.LocalMachine

另外,如果应用程序崩溃了(并且会崩溃),请尝试以这种方式更改LocalUser而不是LocalMachine,那么我们只会使用户感到困惑,而不是每个使用计算机的人都感到困惑。

回答

此设置的相应HKCU密钥为:
HKEY_CURRENT_USER \ Software \ Microsoft \ Internet Explorer \ Main \ Print_Background

回答

另一个注册表项将是:
HKEY_CURRENT_USER \ Software \ Microsoft \ Internet Explorer \ PageSetup \ Print_Background
HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Internet Explorer \ PageSetup \ Print_Background

回答

var sh = new ActiveXObject("WScript.Shell");
key = "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Print_Background";
var defaultValue = sh.RegRead(key); 
sh.RegWrite(key,"yes","REG_SZ");
document.frames['detailFrame'].focus(); 
document.frames['detailFrame'].print();
sh.RegWrite(key,defaultValue,"REG_SZ");  
return false;