Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Printing html from WebBrowser control

SgLifeSgLife Member Posts: 3
I am creating a dialog based application with a CWebBrowser2 control. I need to print the page displayed in the browser in landscape or portrait format without displaying the PrintDialog. Landscape or Portrait is decided by some other inputs.
I have two methods in my App class for setting the orientation and for starting the printing. Now the probelm for me is how to get the content in the browser or in other words how to get the html displayed in the browser? what to do after a m_Browser.GetDocument() call ?? I dont know how I can associate the html with DOCINFO structure ( setting the file name does not work of course). Landscape/Portrait modes and a dc.TextOut() for a hardcoded string works fine. But how to print the html displayed in the browser?
Please note: setting the orientation and using the ExecWb() function does not work because I am always getting portrait mode only.

Excpecting your valuable comments. Thanks.

Comments

  • manucpmanucp Member Posts: 34
    What you get with m_Browser.GetDocument() is an IHTMLDocument2 object. You should use thw browser control to print. Otherwise the formatting work is overwhelming. Are you familiar with COM? If the answer is yes I can give you more options....

    : I am creating a dialog based application with a CWebBrowser2
    : control. I need to print the page displayed in the browser in
    : landscape or portrait format without displaying the PrintDialog.
    : Landscape or Portrait is decided by some other inputs.
    : I have two methods in my App class for setting the orientation and
    : for starting the printing. Now the probelm for me is how to get the
    : content in the browser or in other words how to get the html
    : displayed in the browser? what to do after a m_Browser.GetDocument()
    : call ?? I dont know how I can associate the html with DOCINFO
    : structure ( setting the file name does not work of course).
    : Landscape/Portrait modes and a dc.TextOut() for a hardcoded string
    : works fine. But how to print the html displayed in the browser?
    : Please note: setting the orientation and using the ExecWb() function
    : does not work because I am always getting portrait mode only.
    :
    : Excpecting your valuable comments. Thanks.
    :
  • SgLifeSgLife Member Posts: 3
    Hi Manu,

    I am familiar with COM even though I dont have much experience. Please post some sample code if you have any. Thanks.
  • manucpmanucp Member Posts: 34
    : Hi Manu,
    :
    : I am familiar with COM even though I dont have much experience.
    : Please post some sample code if you have any. Thanks.
    :

    Sorry, no sample code since some solution involves a lot of code. But here are some tips:

    A) The easiest way is to get access to the IHTMLDocument2 interface - easy since you get it directly from the browser control with GetDocument().

    Now you can use execCommand to print the document. The major drawback is that you don't have any control over pagination. To solve this if you are generating the HTML document, you can use styles to insert page breaks. If not you should examine Print Templates. I don't like them I'm afraid.

    B) If you need some control over page breaks, you can do it the hard way:
    Get the body of the document. To do that invoke GetBody and you'll get a IHTMLElement for the body. Now you need to do some recursion.
    For each node check if it has width and height and if positive, query its IHTMLElementRender interface. You can use it to render the element in your own Device Context (Printer or Screen). Some elements are not renderizable or they can have nothing to render so you must navigate their children.

    Unfortunately, some elements show their dimensions as zero if they contain elements with 'absolute' placement. That's the reason to navigate the children.

    Also you should render in a memory device context and then 'Blt' it to the destination device context. Note that you have to keep track of the place where you are rendering to know when a page is about to break. And don't forget to decide if yow want to keep or lose color information.

    I'll be pleased to answer specific questions but I've used this option for a customer project and I cannot send you the full code and it is not easy task to cut samples which have a meaning.

    Best regards
  • SgLifeSgLife Member Posts: 3
    Hi Manu,

    Thanks a lot! That was some explanation. I tried playing around with execWb() and also tried to get hold of the View and print it. But nothing worked. HTML template worked, but as you said its kinda messy. And they suggest swapping height and width to change orientation but that was not really working. So in the end not all happy with the progress and decided to go for a change in plans. Generate an html, convert it into pdf using a third party tool( USD 400) and then print the PDF :) The PL gladly accepted the idea and saved me a lot of trouble.

    Thanks again.
Sign In or Register to comment.