打印网页浏览器内容为景观

本文关键字:景观 浏览器 网页 网页浏览 打印 | 更新日期: 2023-09-27 18:11:35

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

I've try:

  1. 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>
    

    我试着用它:

    mshtml.IHTMLDocument2 doc = wbReport.Document.DomDocument as mshtml.IHTMLDocument2;
    doc.execCommand("print", false, "template.tpl");
    
  2. 在HKCU'Software'Microsoft'Internet Explorer'PageSetup中创建值为"2"的注册键"orientation"

  3. 添加样式:

    <style type="text/css" media="print">
    @page
    {
     size: landscape;
     margin: 2cm;
    }
    </style>
    

但我仍然打印我的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。

注意,你可能想要停止计时器一旦设置横向视图。