[英]Print webbrowser content as landscape
I need to print document from WebBrowser control as landscape (without showing PrintPreview and changing default printer page orientation). 我需要从WebBrowser控件以横向打印文档(不显示PrintPreview并更改默认打印机页面方向)。
I've tried: 我试过了:
TemplatePrinter Here's my template template.html: TemplatePrinter这是我的模板template.html:
<HTML XMLNS:IE> <HEAD> <?IMPORT NAMESPACE="IE" IMPLEMENTATION="#default"> <TITLE>Landscape</TITLE> </HEAD> <BODY> <IE:TEMPLATEPRINTER id="Printer"/> <SCRIPT Language="JavaScript"> Printer.orientation="landscape"; </SCRIPT> </BODY> </HTML>
And I try to use it: 我尝试使用它:
mshtml.IHTMLDocument2 doc = wbReport.Document.DomDocument as mshtml.IHTMLDocument2; doc.execCommand("print", false, "template.tpl");
Create reg key "orientation" with value "2" in HKCU\\Software\\Microsoft\\Internet Explorer\\PageSetup 在HKCU \\ Software \\ Microsoft \\ Internet Explorer \\ PageSetup中创建值为“ 2”的注册表项“ orientation”
Add style like: 添加样式:
<style type="text/css" media="print"> @page { size: landscape; margin: 2cm; } </style>
But I still print my html as portrait. 但是我仍然将html打印为肖像。 Maybe there is better solution to print html as landscape or I have some mistakes when using TemplatePrinter?
也许有更好的解决方案将html打印为横向,或者在使用TemplatePrinter时出现一些错误?
After messing around with the registry, attempting to use print templates in IE and various other methods (none of which worked) i've produced a somewhat hacky workaround. 在弄乱注册表之后,尝试在IE中使用打印模板和其他各种方法(均无效),我产生了一个有点怪异的解决方法。
I've extended the WebBrowser control and created a new print preview method inside which I have methods to pass the ALT+L key combination to enable landscape view: 我扩展了WebBrowser控件,并创建了一个新的打印预览方法,在其中,我有一些方法可以传递ALT + L组合键以启用横向视图:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace YourProjectNamespace
{
class ExWebBrowser : WebBrowser
{
void setLandscapeTimer_Tick(object sender, EventArgs e)
{
SendKeys.SendWait("%L");
this.ProcessDialogKey(Keys.Alt & Keys.L);
SendKeys.Flush();
}
public void _ShowPrintPreviewDialog()
{
this.ShowPrintPreviewDialog();
this.Focus();
Timer setLandscapeTimer = new Timer();
setLandscapeTimer.Interval = 500;
setLandscapeTimer.Tick += new EventHandler(setLandscapeTimer_Tick);
setLandscapeTimer.Start();
}
}
}
All you need to do is simply instantiate the object, load a document as with the standard WebBrowser and call _ShowPrintPreviewDialog. 您需要做的只是简单地实例化对象,像使用标准WebBrowser一样加载文档并调用_ShowPrintPreviewDialog。
Note you may want to stop the timer once the landscape view has been set. 请注意,一旦设置了风景视图,您可能想停止计时器。
HTH somebody with the same issue. 有人遇到同样的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.