如何打印从Web服务的HTML文档? [英] How do I print an HTML document from a web service?

查看:127
本文介绍了如何打印从Web服务的HTML文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C#Web服务打印HTML。 Web浏览器控件是矫枉过正,并在服务环境不运作良好,也没有非常严密的安全约束系统上运作良好。是否有任何形式的免费 .NET 库将支持基本HTML页的打印?下面是代码我到目前为止,它不能正常运行。

I want to print HTML from a C# web service. The Web Browser control is overkill, and does not function well in a service environment, nor does it function well on a system with very tight security constraints. Is there any sort of free .NET library that will support the printing of a basic HTML page? Here is the code I have so far, which does not run properly.

public void PrintThing(string document)
{
    if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
    {
        Thread thread =
            new Thread((ThreadStart) delegate { PrintDocument(document); });
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
    }
    else
    {
        PrintDocument(document);
    }
}

protected void PrintDocument(string document)
{
    WebBrowser browser = new WebBrowser();
    browser.DocumentText = document;
    while (browser.ReadyState != WebBrowserReadyState.Complete)
    {
        Application.DoEvents();
    }
    browser.Print();
}



从UI类型的线程调用时能正常工作,但没有任何反应时调用从服务型螺纹。更改打印() ShowPrintPreviewDialog()产生以下的IE脚本错误:

This works fine when called from UI-type threads, but nothing happens when called from a service-type thread. Changing Print() to ShowPrintPreviewDialog() yields the following IE script error:

错误:dialogArguments .___ IE_PrintType'为空或不是对象

网址:RES://ieframe.dll/preview.dlg

Error: 'dialogArguments.___IE_PrintType' is null or not an object
URL: res://ieframe.dll/preview.dlg

和出现一个小空打印预览对话框。

And a small empty print preview dialog appears.

推荐答案

您可以在命令行中使用打印以下内容:

You can print from the command line using the following:

RUNDLL32.EXE%WINDIR%\System32\mshtml.dll,PrintHTML%1

rundll32.exe %WINDIR%\System32\mshtml.dll,PrintHTML "%1"

%1中要打印的HTML文件的文件路径。

Where %1 is the file path of the html file to be printed.

如果您不需要从内存中打印(或不能提供的写入磁盘的临时文件),你可以使用:使用

If you don't need to print from memory (or can afford to write to the disk in a temp file) you can use:

using (Process printProcess = new Process())
{
string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
printProcess.StartInfo.FileName = systemPath + @"\rundll32.exe";
printProcess.StartInfo.Arguments = systemPath + @"\mshtml.dll,PrintHTML """ + fileToPrint + @"""";
printProcess.Start();
}

NB这仅适用于Windows 2000及以上,我认为。

N.B. This only works on Windows 2000 and above I think.

这篇关于如何打印从Web服务的HTML文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆