IHTMLDocument2 Printing without Dialog Box

2019-07-04 12:03发布

问题:

I've been working on a project that automatically prints HTML invoices. It's been particularly challenging as I've needed to keep the CSS formatting.

Following advice, I've found myself using IHTMLDocument2 to carry out my printing. I'm supposed to be able to:

mshtml.IHTMLDocument2 doc = new mshtml.HTMLDocument() as mshtml.IHTMLDocument2;
doc.write(htmlContent); //htmlContent is a string of HTML
doc.execCommand("PRINT", false, null);

The second argument specifies whether to produce the Printer Options box, but it doesn't help. I've read that using PRINT will always result in a dialog box - however I haven't been able to find an alternative.

Any ideas?

回答1:

According MSDN:

Print

Opens the print dialog box so the user can print the current page.

So I think there is no way to workaround this behaviour. Try to use other classes for your application or open the print dialog box during printing.
You should note that you must provide true as secord parameter (showUI [in, optional]) in execCommand method.