我正在尝试构建一个系统,该系统将从web服务器下载格式化文本,打印格式化文本,确认打印作业成功完成,然后响应web服务器,让它知道文本已经打印。都没有用户的输入。
我已经成功地使用Web浏览器控件下载HTML,然后在不需要用户输入的情况下打印它。然而,这在确认打印的能力方面是不够的。
在System.Printing中,您可以访问PrintServer和PrintQueue,并使用它们启动打印作业并查找打印作业的状态。
我还没能确认打印工作,但我已经能够启动简单的打印。但是,这不会携带来自web服务器的任何HTML格式。我没有绑定到HTML,但它必须是可以由web服务器生成的某种格式,这样就可以在不需要更新客户机应用程序的情况下修改它。
如何打印来自web服务器的输出,格式化正确,并知道打印作业是成功还是失败?
发布于 2012-03-27 13:41:29
我假设您愿意使用WebBrowser控件。以下是确认打印的解决方案。基本上,您需要处理PrintTemplateTeardown事件以等待打印作业的完成。
下面是从在没有打印对话框的情况下从Windows打印html文档中的答案中提取的示例代码:
using System.Reflection;
using System.Threading;
using SHDocVw;
namespace HTMLPrinting
{
public class HTMLPrinter
{
private bool documentLoaded;
private bool documentPrinted;
private void ie_DocumentComplete(object pDisp, ref object URL)
{
documentLoaded = true;
}
private void ie_PrintTemplateTeardown(object pDisp)
{
documentPrinted = true;
}
public void Print(string htmlFilename)
{
documentLoaded = false;
documentPrinted = false;
InternetExplorer ie = new InternetExplorerClass();
ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
ie.PrintTemplateTeardown += new DWebBrowserEvents2_PrintTemplateTeardownEventHandler(ie_PrintTemplateTeardown);
object missing = Missing.Value;
ie.Navigate(htmlFilename, ref missing, ref missing, ref missing, ref missing);
while (!documentLoaded && ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT) != OLECMDF.OLECMDF_ENABLED)
Thread.Sleep(100);
ie.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref missing, ref missing);
while (!documentPrinted)
Thread.Sleep(100);
ie.DocumentComplete -= ie_DocumentComplete;
ie.PrintTemplateTeardown -= ie_PrintTemplateTeardown;
ie.Quit();
}
}
}希望能帮上忙!
https://stackoverflow.com/questions/9848921
复制相似问题