以编程方式更改 WinForms WebBrowser 控件的目标打印机

发布于 2024-09-18 13:06:21 字数 361 浏览 1 评论 0原文

我正在尝试使用不可见的 WebBrowser 控件来打印一个非常简单的 HTML 文档。我们的应用程序要求我们能够以这种方式打印多个文档,并且它们都可以发送到不同的打印机。不幸的是,我并没有很成功地将输出发送到正确的打印机。

现在的工作方式是,在打印文档之前,应用程序确定哪台打印机将接收该文档,并相应地设置默认打印机。为此,它使用从 WinSpool.drv 导入的 SetDefaultPrinter()。如果我在调试模式下单步执行代码,我可以清楚地看到默认打印机发生了变化(并且此变化反映在控制面板 UI 中),但 Web 浏览器仍然坚持使用原来的默认打印机。

据我所知,MSDN 文档并没有真正为这种情况提供解决方案。我非常感谢有关如何以编程方式完成此任务的一些意见。

I'm trying to use an invisible WebBrowser control to print a very simple HTML document. Our application requires that we be able to print several documents this way, and that they all can be sent to different printers. Unfortunately, I haven't been very successful in making the output go to the right printer.

The way it works right now is that before printing a document, the application determines which printer is to receive it, and sets the default printer accordingly. To do this it uses SetDefaultPrinter() imported from WinSpool.drv. If I step the code in debug mode I can clearly see that the default printer changes (and this change is reflected in the control panel UI), but the WebBrowser still insists on using the original default printer.

The MSDN documentation, from what I've seen, doesn't really provide a solution for this scenario. I would greatly appreciate some input on how I can accomplish this programmatically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唱一曲作罢 2024-09-25 13:06:22

鉴于您所说的,也许在更改默认打印机后重新启动包含 Web 浏览器控件的进程(或 Web 浏览器控件的进程)?这就是我看到的那种东西例如,发生在这里


我想可以分出一个执行实际打印的后台进程,但我真的希望有一个更简单的解决方案。

分叉我对可能最简单的解决方案的第一个想法。

其他一些替代方案如下。

1)。 Web 浏览器控件所包装的 IE 通过 ActiveX 公开 API。它/那些 API 之一可能允许您指定目标打印机。

2)。一些可执行文件(我不知道 IE)在注册表中有 printto 条目。例如,Acrobat Reader 有一个条目,其值如下:

""C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe""  /t "%1" "%2" "%3" "%4"

用于指定可用于打印和指定(非默认)打印机的命令行语法。您还可以通过 Google 搜索 printto,例如 ShellExecute 中的 PrintTo 命令

3)。我已经实现了我自己的用于.NET的HTML控件,它不依赖于IE。你说你的 HTML(和我认为的 CSS)很简单,所以也许 我可以渲染它,要么开箱即用,要么只需要一点额外的开发工作。我不支持打印,但是用户控件实现打印非常容易。让我为您实现这一点可能会花费您数百美元,但谁知道呢,也许这对您来说是值得的。这将是一个相当轻量级的解决方案,并且可能得到很好的支持。如果您想进一步讨论,可以给我发电子邮件。

4).您可能还会发现其他控件,与我的类似,或多或少有名/昂贵;或其他应用程序,例如OpenOffice 等。5

)。您可以考虑将 HTML(以某种方式)转换为另一种格式(例如 PDF),您有一个应用程序可以为您提供更好的打印支持。

Given what you've said, perhaps if you restart the process which contains the web browser control (or the process which is the web browser control), after you change the default printer? That's the kind of thing I see happening here, for example.


I suppose it would be possible to fork off a background process that does the actual printing, but I'm really hoping for a simpler solution.

Forking was my first thought towards a probably-simplest solution.

Some other alternatives are as follows.

1). IE, which the webbrowser control is wrapping, exposes APIs via ActiveX. One of its/those APIs might let you specify the destination printer.

2). Some executables (I don't know about IE) have printto entries in the registry. For example, Acrobat Reader has an entry whose value is as follows:

""C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe""  /t "%1" "%2" "%3" "%4"

That's used for specifying the syntax of the command-line which you can use to print and specify a (non-default) printer. You can also Google for printto, see e.g. PrintTo command in the ShellExecute.

3). I have implemented an HTML control for .NET of my own, which doesn't depend on IE. You say that your HTML (and CSS I presume) are simple, so perhaps I can render it, either out of the box or with only a little extra development effort. I don't support printing, but printing is quite easy for a user control to implement. Getting me to implement that for you would cost you several hundred but, who knows, maybe it's worth it to you. It would be quite a light-weight solution, and perhaps well supported. You could email me if you want to discuss that further.

4). You might also find other controls, similar to mine, more or less famous/expensive; or other applications, e.g. OpenOffice etc etc.

5). You could consider converting the HTML (somehow) to another format (e.g. PDF) for which you have an application which gives you better support for printing.

寒江雪… 2024-09-25 13:06:22

我遇到了完全相同的问题,并合并了 此控件 而不是标准的 .NET WebBrowser解决它。

I've had the exact same problem, and incorporated this control instead of the standard .NET WebBrowser to work around it.

魂归处 2024-09-25 13:06:22

这适用于 .NET 3.5(如果之前没有)

this.webBrowser1.ShowPrintDialog();

This works on .NET 3.5, if not before

this.webBrowser1.ShowPrintDialog();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文