Mega Code Archive

 
Categories / Delphi / Examples
 

Print a html web page from my application

Question: I need to print a web page or a local HTML file from my application, but the TWebBrowser class has no print method. How can I print my document then? Answer: Looking into MSHTML.pas, you'll notice that IHTMLWindow3 has a print method. The question is how to a IHTMLWindow3 interface von TWebBrowser. The solution is to use a regular TWebBrowser component, use Navigate() to load the desired document and put the code for printing in the OnNavigateComplete event. procedure TForm1.WebBrowser_V1NavigateComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant); var HTMLDoc: IHTMLDocument2; HTMLWnd: IHTMLWindow2; HTMLWindow3: IHTMLWindow3; begin HTMLDoc := (Sender as TWebBrowser).Document as IHTMLDocument2; if HTMLDoc = nil then raise Exception.Create('Could not convert the WebBrowser to an IHTMLDocument2'); HTMLWnd := HTMLDoc.parentWindow; HTMLWindow3 := HTMLWnd as IHTMLWindow3; // finally, we get to the print method HTMLWindow3.print; end; // .. begin WebBrowser1.Navigate('http://www.yahoo.com/'); end;