C# 以横向方式打印webbrowser内容

C# 以横向方式打印webbrowser内容,c#,winforms,internet-explorer,printing,webbrowser-control,C#,Winforms,Internet Explorer,Printing,Webbrowser Control,我需要从WebBrowser控件以横向方式打印文档(不显示打印预览和更改默认打印机页面方向) 我试过: 模板打印机 这是我的template.html: <HTML XMLNS:IE> <HEAD> <?IMPORT NAMESPACE="IE" IMPLEMENTATION="#default"> <TITLE>Landscape</TITLE> </HEAD> <BODY> <IE:TEMPLATEPR

我需要从WebBrowser控件以横向方式打印文档(不显示打印预览和更改默认打印机页面方向)

我试过:

  • 模板打印机 这是我的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>
    
  • 在HKCU\Software\Microsoft\Internet Explorer\PageSetup中创建值为“2”的注册表项“方向”

  • 添加如下样式:

    <style type="text/css" media="print">
    @page
    {
     size: landscape;
     margin: 2cm;
    }
    </style>
    
    
    @页面
    {
    规模:景观;
    边缘:2cm;
    }
    
  • 但我仍然以肖像的形式打印我的html。
    也许有更好的解决方案可以将html打印为横向打印,或者我在使用TemplatePrinter时出现了一些错误?

    在弄乱了注册表之后,尝试在IE和其他各种方法中使用打印模板(这些方法都不起作用),我产生了一个有点粗糙的解决方法

    我扩展了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();
            }
        }
    }
    
    您只需实例化对象,使用标准WebBrowser加载文档并调用_ShowPrintPreviewDialog即可

    注意:设置横向视图后,可能需要停止计时器


    有人也有同样的问题。

    这肯定不像拼写错误那么简单。但我必须指出,您的代码中有一个问题,或者需要编辑问题以反映正确的拼写。在#1中,您有“TemplatePrinter这是我的模板模板。
    html
    :”,然后当您使用它时,您有“doc.execCommand(“print”,false,“template.
    tpl
    ””);将tpl更改为html?您找到了这个问题的解决方案吗?也许可以共享模板html的外观(完整)以使其成为打印环境?
    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();
            }
        }
    }