在没有打印对话框的情况下从 Windows 服务打印 html 文档 [英] Print html document from Windows Service without print dialog
本文介绍了在没有打印对话框的情况下从 Windows 服务打印 html 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Windows 服务,我想在服务启动时打印 .html 页面.我正在使用此代码并且打印良好.但是打印对话框来了,没有打印对话框怎么打印?
I am using a windows service and i want to print a .html page when the service will start. I am using this code and it's printing well. But a print dialog box come, how do i print without the print dialog box?
public void printdoc(string document)
{
Process printjob = new Process();
printjob.StartInfo.FileName = document;
printjob.StartInfo.UseShellExecute = true;
printjob.StartInfo.Verb = "print";
printjob.StartInfo.CreateNoWindow = true;
printjob.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
printjob.Start();
}
有没有其他方法可以在不显示打印对话框的情况下打印此内容.
Have there any other way to print this without showing the print dialog box.
更新:响应这个:
但是我已经使用过这个类但是当我调用
But i have already used this class but when i am calling the
axW.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT,SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER , ref em, ref em);
当我从窗口服务使用时,我的程序在此处被阻止,但它在 Windows 应用程序中运行良好.
My program getting block here when i am using from window service but it is working fine from windows application.
推荐答案
首先,代码如下:
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();
}
}
}
- 您可以通过添加对Microsoft Internet Controls"的引用来访问 SHDocVw 命名空间,该引用位于添加引用"对话框的 COM 选项卡上.
- 有关 InternetExplorer 对象的更多信息,请访问 MSDN.
- Navigate() 方法将加载 HTML 文件.其他参数允许您指定可选参数,例如标志和标题.
- 在加载文档之前我们无法打印.在这里,我进入一个循环,等待 DocumentComplete 事件被调用,在此事件上设置一个标志,通知我们导航已完成.请注意,只要导航完成(成功或失败),就会调用 DocumentComplete.
- 设置 documentLoaded 标志后,将通过 QueryStatusWB() 查询打印状态,直到启用打印.
- 打印是通过 ExecWB() 调用开始的.指定了
OLECMDID_PRINT
命令以及选项OLECMDEXECOPT_DONTPROMPTUSER
以在无需用户交互的情况下自动打印.一个重要的注意事项是这将打印到默认打印机.要指定打印机,您必须设置默认打印机(在代码中,您可以调用 SetDefaultPrinter()).最后两个参数允许可选的输入和输出参数. - 我们不想在打印完成之前退出,因此再次进入一个循环.触发 PrintTemplateTeardown 事件后,将设置 documentPrinted 标志.然后可以清理对象.
- You can access the SHDocVw namespace by adding a reference to 'Microsoft Internet Controls', found on the COM tab of the Add Reference dialog.
- More information on the InternetExplorer object can be found on MSDN.
- The Navigate() method will load the HTML file. The other parameters allow you to specify optional parameters, such as flags and headers.
- We can't print until the document is loaded. Here, I enter a loop waiting until the DocumentComplete event is called, upon which a flag is set notifying us that navigation has completed. Note that DocumentComplete is called whenever navigation is finished - upon success or failure.
- Once the documentLoaded flag is set, the printing status is queried via QueryStatusWB() until printing is enabled.
- Printing is started with the ExecWB() call. The
OLECMDID_PRINT
command is specified, along with the optionOLECMDEXECOPT_DONTPROMPTUSER
to automatically print without user interaction. An important note is that this will print to the default printer. To specify a printer, you will have to set the default printer (in code, you could call SetDefaultPrinter()). The two final parameters allow optional input and output parameters. - We don't want to quit until printing is complete, so once again a loop is entered. After the PrintTemplateTeardown event is fired, the documentPrinted flag is set. The objects can then be cleaned up.
这篇关于在没有打印对话框的情况下从 Windows 服务打印 html 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文