html 点击打印预览,不预览即打印WebBrowser,即单击打印

小编典典

我有一个示例控制台应用程序,该应用程序使用WinForms打印一组HTML文件WebBrowser。您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 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 loadedTcs = null;

WebBrowserDocumentCompletedEventHandler documentCompletedHandler = (s, e) =>

loadedTcs.TrySetResult(true); // turn event into awaitable task

TaskCompletionSource 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();

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();

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 Run(Func> worker, params object[] args)

{

var tcs = new TaskCompletionSource();

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();

}

}

}

}

2020-05-19

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值