开发者

C# How to send a rendered HTML page to the printer

开发者 https://www.devze.com 2023-01-05 14:55 出处:网络
from a CLI application (or library) in C#, i can send something to the printer, select it etc (usingPrintDocument and PrinterSettings.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消