[英]Print webbrowser content as landscape
我需要從WebBrowser控件以橫向打印文檔(不顯示PrintPreview並更改默認打印機頁面方向)。
我試過了:
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");
在HKCU \\ Software \\ Microsoft \\ Internet Explorer \\ PageSetup中創建值為“ 2”的注冊表項“ orientation”
添加樣式:
<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。
請注意,一旦設置了風景視圖,您可能想停止計時器。
有人遇到同樣的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.