from a CLI application (or library) in C#, i can send something to the printer, select it etc (using PrintDocument
and PrinterSettings
.
How can I send to a printer a rendered HTML page? Like instantiating in memory IE and use it to render / print t开发者_运维百科he page? This without opening an actual browser window (ex, do it all from the command line).
This is where I've got so far:
using mshtml;
string sWebPage = System.IO.File.ReadAllText(@"C:\Users\me\Desktop\h.html");
object[] oPageText = { sWebPage };
HTMLDocumentClass myDoc = new HTMLDocumentClass();
IHTMLDocument2 oMyDoc = (IHTMLDocument2)myDoc;
oMyDoc.write(oPageText);
//oMyDoc.execCommand("print", false, 0); <- does not wok
Thanks
There's a WebBrowser control that looks like it lets you load and render some HTML, and does have a Print method, but it's a form control so it may be tough to use in a non-Forms project.
You could use Internet Explorer via COM and set the visibility of the browser to false.
Let me know if you need an actual code example.
using SHDocVw;
InternetExplorer ie = new InternetExplorer();
ie.Visible = false;
ie.Navigate("http://xxx");
Thread.Sleep(1000);
while (ie.Busy)
{
Thread.Sleep(1000);
}
,,, ...
Using Javascript
, window.print()
will send the HTML page to the printer. Check out W3Schools reference
精彩评论