- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个打开自定义打印预览表单的 c# .Net 应用程序(包含显示 HTML 文件的 WebBrowser 表单控件和打印 Web 浏览器控件内容的按钮:
webBrowser.Print();
但是,我现在想在不打开此表单的情况下打印 HTML 文件。
我试图将 html 加载到网络浏览器表单中而不显示表单并调用了 webBrowser.Print() 但什么也不会打印。看来我只有在显示表单并将 HTML 加载到控件中时才能打印。
最佳答案
我有一个示例控制台应用程序,它使用 WinForms WebBrowser
打印一组 HTML 文件。您可以借用它的 DoWorkAsync
部分用于 WinForms 应用程序中的打印任务,几乎无需任何更改:
// by Noseratio - http://stackoverflow.com/users/1768303/noseratio
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleApplicationWebBrowser
{
class Program
{
// Entry Point of the console app
static void Main(string[] args)
{
try
{
// download each page and dump the content
var task = MessageLoopWorker.Run(DoWorkAsync,
"http://www.example.com", "http://www.example.net", "http://www.example.org");
task.Wait();
Console.WriteLine("DoWorkAsync completed.");
}
catch (Exception ex)
{
Console.WriteLine("DoWorkAsync failed: " + ex.Message);
}
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
}
// navigate WebBrowser to the list of urls in a loop
static async Task<object> DoWorkAsync(object[] args)
{
Console.WriteLine("Start working.");
var wb = new WebBrowser();
wb.ScriptErrorsSuppressed = true;
if (wb.Document == null && wb.ActiveXInstance == null)
throw new ApplicationException("Unable to initialize the underlying WebBrowserActiveX");
// get the underlying WebBrowser ActiveX object;
// this code depends on SHDocVw.dll COM interop assembly,
// generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
// and add as a reference to the project
var wbax = (SHDocVw.WebBrowser)wb.ActiveXInstance;
TaskCompletionSource<bool> loadedTcs = null;
WebBrowserDocumentCompletedEventHandler documentCompletedHandler = (s, e) =>
loadedTcs.TrySetResult(true); // turn event into awaitable task
TaskCompletionSource<bool> printedTcs = null;
SHDocVw.DWebBrowserEvents2_PrintTemplateTeardownEventHandler printTemplateTeardownHandler = (p) =>
printedTcs.TrySetResult(true); // turn event into awaitable task
// navigate to each URL in the list
foreach (var url in args)
{
loadedTcs = new TaskCompletionSource<bool>();
wb.DocumentCompleted += documentCompletedHandler;
try
{
wb.Navigate(url.ToString());
// await for DocumentCompleted
await loadedTcs.Task;
}
finally
{
wb.DocumentCompleted -= documentCompletedHandler;
}
// the DOM is ready,
Console.WriteLine(url.ToString());
Console.WriteLine(wb.Document.Body.OuterHtml);
// print the document
printedTcs = new TaskCompletionSource<bool>();
wbax.PrintTemplateTeardown += printTemplateTeardownHandler;
try
{
wb.Print();
// await for PrintTemplateTeardown - the end of printing
await printedTcs.Task;
}
finally
{
wbax.PrintTemplateTeardown -= printTemplateTeardownHandler;
}
Console.WriteLine(url.ToString() + " printed.");
}
wb.Dispose();
Console.WriteLine("End working.");
return null;
}
}
// a helper class to start the message loop and execute an asynchronous task
public static class MessageLoopWorker
{
public static async Task<object> Run(Func<object[], Task<object>> worker, params object[] args)
{
var tcs = new TaskCompletionSource<object>();
var thread = new Thread(() =>
{
EventHandler idleHandler = null;
idleHandler = async (s, e) =>
{
// handle Application.Idle just once
Application.Idle -= idleHandler;
// return to the message loop
await Task.Yield();
// and continue asynchronously
// propogate the result or exception
try
{
var result = await worker(args);
tcs.SetResult(result);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
// signal to exit the message loop
// Application.Run will exit at this point
Application.ExitThread();
};
// handle Application.Idle just once
// to make sure we're inside the message loop
// and SynchronizationContext has been correctly installed
Application.Idle += idleHandler;
Application.Run();
});
// set STA model for the new thread
thread.SetApartmentState(ApartmentState.STA);
// start the thread and await for the task
thread.Start();
try
{
return await tcs.Task;
}
finally
{
thread.Join();
}
}
}
}
关于c# - 在不预览的情况下打印 WebBrowser,即单击打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19734151/
运行这一行: webbrowser.open(page.path, new=0, autoraise=True) 我总是在新标签页中打开页面。 我怎样才能将它放在与它应该使用 new=0 相同的选项卡
基于 MSDN 上的这篇文章:How To Determine When a Page Is Done Loading in WebBrowser Control ,并且从过去关于 StackOver
我不明白为什么这行简单的代码行不通: // Bulding tree var declaration = new XDeclaration("1.0", "UTF-8", "yes"); var ro
这些是等价的吗? WebBrowser.Navigate('http://google.com'); 和 WebBrowser.Stop(); WebBrowser.Navigate('http://
我需要获取有关 Webbrowser 的滚动条(位置、大小、可见性)的信息控制外部应用程序,我尝试使用 GetScrollBarInfo来 self 之前的功能 question ,但该函数总是返回
目前在带有 WebBrowser 的 vb6 项目中,我遇到了一个问题。我想在我的应用程序中显示 adsense 广告,所以我创建了一个 html 页面并将文件放入服务器(adsense 批准),我尝
Windows Phone 8 应用程序,WebBrowser 控件。我通过 NavigateToString 加载了一大块 HTML (设置后 IsScriptEnabled=true )。一段时间
我有一个嵌入 WebBrowser 的 native Windows 应用程序, IE。 CLSID_WebBrowser 8856F961-340A-11D0-A96B-00C04FD705A2 S
提前致谢。 在我的应用程序中,我将 WebBrowser 控件替换为 Microsoft 的 WebView2 . 我想知道 WebView2 中 WebBrowser 控件的以下成员的等价物。 We
我怎样才能让 Web 浏览器调用自定义方案来启动 Activity,然后我想按下后退按钮但不返回 Web 浏览器。 我只想在调用网络浏览器然后调用我的方案启动另一个 Activity 时实现转发。当我
我们的目标是:嵌入在 .net winform 中的支持 Watin 的浏览器测试。 目前,我们正在使用 .net WebBrowser 控件将浏览器行为嵌入到 winform 中。我们使用如下代码将
我正在使用将 WebBrowser 控件嵌入到 Windows 窗体中的应用程序,我注意到一个严重的内存泄漏问题。在阅读了互联网上的其他类似案例后,问题似乎出在 IE 上。 我问过其他人是如何解决这个
我使用了“LoadCompleted”事件,基本上一旦收到该事件,我就需要截取屏幕截图(我已经构建)。问题是一旦这个事件结束,屏幕截图就只是空白(页面还没有加载),只有当我延迟 5-10 秒时,屏幕截
我试图自动打开多个用户配置文件,给定几个不同网站上的名称列表,但我找不到在新窗口中打开链接的方法,这意味着我无法对我打开的不同网站进行排序自己的橱窗收藏。 这是我的代码: import webbrow
我使用 vb.net + wpf+ System.Windows.Forms.WebBrowser 在 wpf 表单上显示网页内容。 此网站“http://manuim.com/laisha/”在控件
有没有办法检查用户是否安装了 PDF 阅读器? 也许使用 ActiveX 组件? 请你帮助我好吗? 最佳答案 不,我不这么认为 - 但默认情况下你总是可以通过谷歌的 PDF 阅读器直接链接你的链接 -
尝试在 WPF(C#/XAML、.NET 4.0)WebBrowser 应用程序中测试基本浏览器概念。到目前为止,唯一的问题是以编程方式缩放。有没有人有这方面的经验? MSDN 没有列出任何内容:ht
首先是一些定义,以保持清楚。 User: A live person, using the software Client: A company that is paying for a custom
尝试使用 webBrowser.Navigate("https://home.nest.com/") 加载页面时,我不断收到脚本错误.它会从我的普通互联网浏览器中正常显示,但不会在我的程序中显示。 谁
显然,WPF WebBrowser control 存在一些严重的键盘和焦点问题。 .我已经组装了一个简单的 WPF 应用程序,只有一个 WebBrowser 和两个按钮。该应用程序加载一个非常基本的
我是一名优秀的程序员,十分优秀!