DelphiFAQ Home Search:
General :: Programming :: Delphi :: Network
Network (local network and Internet) programming under Windows with Delphi.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

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;
You don't like the formatting? Check out SourceCoder then!
Generated 0:01:34 on Nov 21, 2017