Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无需预览即可打印WebBrowser,即单击打印_C#_.net_Printing_Webbrowser Control - Fatal编程技术网

C# 无需预览即可打印WebBrowser,即单击打印

C# 无需预览即可打印WebBrowser,即单击打印,c#,.net,printing,webbrowser-control,C#,.net,Printing,Webbrowser Control,我有一个c#.Net应用程序,它可以打开一个自定义打印预览表单(与显示HTML文件的WebBrowser表单控件和一个打印web浏览器控件内容的按钮不同): webBrowser.Print(); 但是,我现在想打印HTML文件而不打开此表单 我尝试将html加载到webbrowser表单中,但不显示表单,并调用了webbrowser.Print()但是没有任何东西可以打印。似乎只有当我显示表单并将HTML加载到控件中时,我才能打印。我知道的唯一方法是将网站呈现为图像,然后以静默方式打印。也许

我有一个c#.Net应用程序,它可以打开一个自定义打印预览表单(与显示HTML文件的WebBrowser表单控件和一个打印web浏览器控件内容的按钮不同):

webBrowser.Print();
但是,我现在想打印HTML文件而不打开此表单


我尝试将html加载到webbrowser表单中,但不显示表单,并调用了webbrowser.Print()但是没有任何东西可以打印。似乎只有当我显示表单并将HTML加载到控件中时,我才能打印。

我知道的唯一方法是将网站呈现为图像,然后以静默方式打印。也许project会帮到你。

你正在尝试的是调用

无声印刷


也许你应该根据你的技术来搜索替代方案。表单和WPF的实现方式各不相同。

我有一个示例控制台应用程序,它使用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();
            }
        }
    }
}
//按比例-http://stackoverflow.com/users/1768303/noseratio
使用制度;
使用系统线程;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间控制台应用程序WebBrowser
{
班级计划
{
//控制台应用程序的入口点
静态void Main(字符串[]参数)
{
尝试
{
//下载每个页面并转储内容
var task=MessageLoopWorker.Run(DoWorkAsync,
"http://www.example.com", "http://www.example.net", "http://www.example.org");
task.Wait();
WriteLine(“DoWorkAsync已完成”);
}
捕获(例外情况除外)
{
Console.WriteLine(“DoWorkAsync失败:+ex.Message”);
}
控制台。WriteLine(“按Enter键退出”);
Console.ReadLine();
}
//将WebBrowser导航到循环中的URL列表
静态异步任务DoWorkAsync(对象[]args)
{
Console.WriteLine(“开始工作”);
var wb=新的WebBrowser();
wb.ScriptErrorsSuppressed=true;
if(wb.Document==null&&wb.ActiveXInstance==null)
抛出新的ApplicationException(“无法初始化底层WebBrowserActiveX”);
//获取底层WebBrowser ActiveX对象;
//此代码依赖于SHDocVw.dll COM互操作程序集,
//生成SHDocVw.dll:“tlbimp.exe ieframe.dll”,
//并添加为项目的参考
var wbax=(SHDocVw.WebBrowser)wb.ActiveXInstance;
TaskCompletionSource loadedTcs=null;
WebBrowserDocumentCompletedEventHandler documentCompletedHandler=(s,e)=>
loadedTcs.TrySetResult(true);//将事件转换为可等待的任务
TaskCompletionSource printedTcs=null;
SHDocVw.DWebBrowserEvents2_PrintTemplateTeardownEventHandler printTemplateTeardownHandler=(p)=>
printedTcs.TrySetResult(true);//将事件转换为可等待的任务
//导航到列表中的每个URL
foreach(参数中的变量url)
{
loadedTcs=new TaskCompletionSource();
wb.DocumentCompleted+=documentCompletedHandler;
尝试
{
导航(url.ToString());
//等待文档完成
等待加载的任务;
}
最后
{
wb.DocumentCompleted-=documentCompletedHandler;
}
//DOM已经准备好了,
Console.WriteLine(url.ToString());
Console.WriteLine(wb.Document.Body.OuterHtml);
//打印文档
printedTcs=新任务完成源();
wbax.PrintTemplateTeardown+=printTemplateTeardownHandler;
尝试
{
wb.Print();
//等待打印模板拆卸-打印结束
等待打印的任务;
}
最后
{
wbax.PrintTemplateTeardown-=printTemplateTeardownHandler;
}
Console.WriteLine(url.ToString()+“printed.”);
}
wb.Dispose();
控制台写入线(“结束工作”);
返回null;
}
}
//用于启动消息循环并执行异步任务的帮助器类
公共静态类MessageLoopWorker
{
公共静态异步任务运行(Func worker,params object[]args)
{
var tcs=new TaskCompletionSource();
变量线程=新线程(()=>
{
EventHandler idleHandler=null;
idleHandler=async(s,e)=>
{
//处理应用程序。仅空闲一次
Application.Idle-=idleHandler;
//返回到消息循环
等待任务;
//并异步继续
//提出结果或例外
尝试
{
var结果=等待工作人员(args);
tcs.SetResult(结果);
}
捕获(例外情况除外)
{
tcs.SetException(ex);
}
//退出消息循环的信号
//此时将退出Application.Run
Application.ExitThread();
};
//处理应用程序。仅空闲一次
//确保我们在消息循环中