【发布时间】:2016-01-17 15:50:07
【问题描述】:
我正在使用 WPF 应用程序中的 System.Windows.Forms.WebBrowser(在单独的 .dll 文件中使用)来在后台打印一些 HTML 内容。
我的问题是 HTML 元素不受页面中 javascript 的影响。
我知道页面中没有错误,因为如果我在 DocumentCompleted 事件之后复制其内容并将其粘贴到纯 HTML 文件中,脚本就可以工作。
我的 HTML 模板:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script>
function loaded() {
document.getElementById("content").innerHTML = "after javascript";
}
</script>
</head>
<body onload="loaded()">
<h1 id="content">before javascript</h1> <!--<= This value does not change-->
</body>
</html>
我使用这个 HTML 如下:
private void PrintTest()
{
string curDir = Directory.GetCurrentDirectory();
string fullPath = Path.Combine(curDir, "PrintDocs\\HTMLPage1.html");
string html = File.ReadAllText(fullPath);
HtmlPrint.Print(html);
}
public static class HtmlPrint
{
public static void Print(string html, string documentTitle = "")
{
var wb = new System.Windows.Forms.WebBrowser { DocumentText = html };
wb.DocumentCompleted += wb_DocumentCompleted;
}
private static void wb_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
var wb = ((System.Windows.Forms.WebBrowser)sender);
wb.Print();
wb.Dispose();
}
}
我在这里错过了什么?
【问题讨论】:
-
我的 javascript 调用存在复制\粘贴问题。修复了它,但问题仍然存在
-
@Izzy 我猜你的意思是
wb.Document.InvokeScript("loaded");,因为它神秘地起作用了。将此作为未来读者的答案。
标签: javascript c# html wpf printing