Link to home
Start Free TrialLog in
Avatar of NetSoftDS
NetSoftDSFlag for Australia

asked on

Programmatically Print TCppWebBrowser in Landscape

Can anyone provide a C/C++ code example of how to print a HTML page displayed in a TCppWebBrowser component in Landscape orientation?

This is for systems with IE6 or greater installed.
Avatar of DanRollins
DanRollins
Flag of United States of America image

I suggest that you read:
   Beyond Print Preview: Print Customization for Internet Explorer
   http://msdn2.microsoft.com/en-us/library/bb250434.aspx
and the continuation:
   http://msdn2.microsoft.com/en-us/library/ms976415.aspx

Although the article is not specific to Borland's class object, the same basic functionality exists.   You will write code like:

    VARIANT vTemplatePath;
    V_VT(&vTemplatePath) = VT_BSTR;
    V_BSTR(&vTemplatePath) = SysAllocString(L"c:/MyTemplate.htm");
    pCmdTarg->Exec(&CGID_MSHTML,
               IDM_PRINT,
               OLECMDEXECOPT_PROMPTUSER,
               &vTemplatePath,
               NULL);

where pCmdTarg is a pointer to the webbrowser control's IWebBrowser2 interface object.

Part two of the article says this about landscape printing:
   Checking page orientation

   There is no page orientation setting with the print architecture. When a
    page switches from portrait mode to landscape mode or back, the page
    width and height properties merely exchange places. The print template
    checks the page orientation and paper size by verifying that the
    pageWidth property of the TemplatePrinter behavior is 1100 (remember,
    page measurement is in hundredths of an inch) and that the pageHeight is
    850. If the property settings are otherwise, the template displays an
    appropriate alert and opens the Page Setup dialog box so the user can
    correct the settings.

As best as I can tell, the needed attribute, pageWidth, is read only, as is the
   dialogArguments->__IE_PrinterCmd_DevMode
DEVMODE structure, which is normally manipulated to force landscape printing.

Thus, it appears that the only way to force landscape printing is to change the settings for the default printer (perhaps by manipulating the system Registry before Exec'ing the IDM_PRINT verb).  I'd not recommend that, but it should be possible.  An alternative might be to pop up the Print dialog, and then send some keystrokes to it to set for landscape printing.

A simpler way may exist, but it would not surprise me if it were in fact, quite difficult.  In general, webbrowsing is designed for on-screen viewing.  Sites that expect to output to a printer often provide special "printer-friendly" versions of the page.  Also there are security issues:  It would be disastrous if a site could remotely mess around with your printer settings and/or spew pages from your printer without user intervention.  A locally-hosted browser control would tend to live within the constraints of that same sandbox.

-- Dan
You will also find some good info in these webreference.com articles:
   Print Templates, Part I
   http://www.webreference.com/js/column89/

   Print Templates, Part II: TemplatePrinter
   http://www.webreference.com/js/column91/
ASKER CERTIFIED SOLUTION
Avatar of NetSoftDS
NetSoftDS
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial