c# 打印 html,C#:“如何使用WebBrowser并将html打印到控制台。”

我已经加了10个小时,现在试图通过WebBrowser属性获得一个洞。C#:“如何使用WebBrowser并将html打印到控制台。”

我只是试图导航到google.com并将html代码打印到控制台。这不断给我一个空引用。

有很多与此相关的问题,他们都表示需要或不正确设置DocumentCompleted Eventhandler。

所以我尽我所能,尽管仍然没有运气。然后我竟然复制了粘贴Microsoft的官方示例我仍然得到空引用!

using System;

using System.Windows.Forms;

namespace Aamanss

{

class MainClass

{

public static void PrintHelpPage()

{

// Create a WebBrowser instance.

WebBrowser webBrowserForPrinting = new WebBrowser();

// Add an event handler that prints the document after it loads.

webBrowserForPrinting.DocumentCompleted +=

new WebBrowserDocumentCompletedEventHandler(PrintDocument);

// Set the Url property to load the document.

webBrowserForPrinting.Url = new Uri("https://www.google.com");

}

public static void PrintDocument(object sender,

WebBrowserDocumentCompletedEventArgs e)

{

// Print the document now that it is fully loaded.

((WebBrowser)sender).Print();

// Dispose the WebBrowser now that the task is complete.

((WebBrowser)sender).Dispose();

}

public static void Main(string[] args)

{

PrintHelpPage();

}

}

}

正如你可以看到上述代码的大部分是从微软复制粘贴。而这个错误:

Gtk-Message: Failed to load module "atk-bridge"

libgluezilla not found. To have webbrowser support, you need libgluezilla installed

System.NullReferenceException: Object reference not set to an instance of an object

at System.Windows.Forms.WebBrowser.Navigate (System.Uri url) [0x0000e] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:304

at System.Windows.Forms.WebBrowser.set_Url (System.Uri value) [0x00007] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:231

at (wrapper remoting-invoke-with-check) System.Windows.Forms.WebBrowser:set_Url (System.Uri)

at Aamanss.MainClass.PrintHelpPage() [0x00024] in /root/Projects/Aamanss/Aamanss/Program.cs:19

at Aamanss.MainClass.Main (System.String[] args) [0x00001] in /root/Projects/Aamanss/Aamanss/Program.cs:34

[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object

at System.Windows.Forms.WebBrowser.Navigate (System.Uri url) [0x0000e] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:304

at System.Windows.Forms.WebBrowser.set_Url (System.Uri value) [0x00007] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:231

at (wrapper remoting-invoke-with-check) System.Windows.Forms.WebBrowser:set_Url (System.Uri)

at Aamanss.MainClass.PrintHelpPage() [0x00024] in /root/Projects/Aamanss/Aamanss/Program.cs:19

at Aamanss.MainClass.Main (System.String[] args) [0x00001] in /root/Projects/Aamanss/Aamanss/Program.cs:34

我真的绝望弄清楚如何让一般的web浏览器导航的工作,因此我真诚地希望你们中的一个可以说明这傻瓜。

2017-04-20

Nulle

+0

是否正在打印以打印到控制台或物理打印机。 –

+0

我正在打印到控制台。我正在运行它在monoDevelop作为控制台应用程序 –

+0

检查此答案:http://stackoverflow.com/questions/19734151/print-webbrowser-without-previewing-ie-single-click-print –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值