从Windows服务打印HTML文档格式打印对话框 [英] Print html document from Windows Service without print dialog
问题描述
我使用的是Windows服务,我想打印的.html页面时,该服务将启动。我用这code和它的打印好。但打印对话框来,我怎么打印照片,无需打印对话框?
公共无效printdoc(字符串文件)
{
流程的PrintJob =新的Process(); printjob.StartInfo.FileName =文件;
printjob.StartInfo.UseShellExecute =真;
printjob.StartInfo.Verb =打印;
printjob.StartInfo.CreateNoWindow = TRUE;
printjob.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 对PrintJob.start();
}
有没有任何其他的方式来打印此而不显示打印对话框。
更新:响应<一个href=\"http://stackoverflow.com/questions/419412/print-html-document-from-windows-service-without-print-dialog/419427#419427\">this:
不过,我已经使用这个类,但是当我打电话了
<$p$p><$c$c>axW.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT,SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER ,楼盘EM,楼盘EM);我的程序越来越块这里的时候,我从窗口服务使用,但它是从Windows应用程序正常工作。
首先,这里的code:
使用的System.Reflection;
使用的System.Threading;
使用SHDOCVW;命名空间HTMLPrinting
{
公共类HTMLPrinter
{
私人布尔documentLoaded;
私人布尔documentPrinted; 私人无效ie_DocumentComplete(对象pDisp,参考目标URL)
{
documentLoaded = TRUE;
} 私人无效ie_PrintTemplateTeardown(对象pDisp)
{
documentPrinted = TRUE;
} 公共无效打印(串htmlFilename)
{
documentLoaded = FALSE;
documentPrinted = FALSE; InternetExplorer的IE =新InternetExplorerClass();
ie.DocumentComplete + =新DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
ie.PrintTemplateTeardown + =新DWebBrowserEvents2_PrintTemplateTeardownEventHandler(ie_PrintTemplateTeardown); 反对失踪= Missing.Value; ie.Navigate(htmlFilename,参考失踪,失踪参考,参考失踪,失踪参考);
而(documentLoaded&安培;!&安培;!ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT)= OLECMDF.OLECMDF_ENABLED)
Thread.sleep代码(100); ie.ExecWB(OLECMDID.OLECMDID_PRINT,OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,裁判失踪,失踪参考);
而(!documentPrinted)
Thread.sleep代码(100); ie.DocumentComplete - = ie_DocumentComplete;
ie.PrintTemplateTeardown - = ie_PrintTemplateTeardown;
ie.Quit();
}
}
}
- 您可以通过添加到Microsoft Internet控制,添加引用对话框的COM选项卡上找到一个参考访问SHDOCVW命名空间。
- InternetExplorer对象的更多信息,请 MSDN 找到
- 的导航()方法将加载HTML文件。其他参数允许您指定可选参数,如标志和标头。
- 我们无法打印,直到文件被加载。在这里,我进入一个循环等待直到DocumentComplete事件被调用,在其上标记被设置通知我们导航已完成。注意,每当导航完成的DocumentComplete被称为 - 在成功或失败
- 一旦documentLoaded标志被设置时,印刷状态通过直到启用打印QueryStatusWB()查询。
- 开始打印与ExecWB()调用。在
OLECMDID_PRINT
命令指定,OLECMDEXECOPT_DONTPROMPTUSER
选项以无需用户交互自动打印一起。一个重要的注意的是,这将打印到默认打印机。要指定一台打印机,你将不得不设置默认打印机(在code,你可以调用<一个href=\"http://www.pinvoke.net/default.aspx/winspool/SetDefaultPrinter.html?diff=y\">SetDefaultPrinter()).这两个最后的参数使可选的输入和输出参数。 - 我们不想放弃,直到打印完成,所以再次进入一个循环。该PrintTemplateTeardown事件被触发之后,documentPrinted标志被设置。然后,对象可以被清理。
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.
Update: in response to this:
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);
My program getting block here when i am using from window service but it is working fine from windows application.
First off, here's the code:
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();
}
}
}
- 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屋!