Link to home
Start Free TrialLog in
Avatar of Gewgala
GewgalaFlag for United States of America

asked on

Invoking Print Preview of iFrame contents, not the whole frameset

Ok, in the following code it invokes print preview.  The .aspx page I'm working with is a page that contains 4 different iframes.  I would like to do print preview of just the contents in iFrame #4, but when this code is executed it's giving a print preview of the main page that contains the 4 iframes.  That's the problem.  

I've tried lots of stuff, but I can't figure it out.

Here is the code of the page in iframe #4 that calls the javascript method to open print preview on page load:

    <script language="javascript" type="text/javascript">
        function printpr()
        {
            var OLECMDID = 7;
            /* OLECMDID values:
            * 6 - print
            * 7 - print preview
            * 1 - open window
            * 4 - Save As
            */
            var PROMPT = 2; // 2 DONTPROMPTUSER
            var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0  CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
            top.frames.printFrame.document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
            top.frames.printFrame.WebBrowser1.ExecWB(OLECMDID, PROMPT);
            top.frames.printFrame.WebBrowser1.outerHTML = "";
        }    
    </script>

the code after the WebBrowser declaration says "top.frames.printFrame".  I added that there to try and specify that I only wanted to embed the browser object in the iframe named "printFrame" (iframe #4), but it doesn't do anything differently.  It could be the same thing without that, obviously.

Help is much appreciated.
Avatar of RedKelvin
RedKelvin

Hi,
You have probably seen this, but I don't think it is possible

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2409699&SiteID=1

RedK
Avatar of Gewgala

ASKER

lol, in that link you provided, that's a link to a post that I made in the microsoft forums.  :)  I thought that looked familiar!  I hadn't read the reply though.  I'll check out the link that was provided.

By the way, I was able to get it to work where you only print the preview of the selected iframe.  Here was my workaround:

a print.css file
and javascript

In my print.css file, I pretty much made everything invisible to the printer/print preview by giving display:none properties to everything basically, except one literal control.  With javascript, I used innerHTML to take the contents of the iframe and then put them in the literal control that was the only thing visible to the printer, thanks to the print.css file that I referenced.  This gave me exactly what I wanted.  So in short, it wasn't previewing the contents of the iframe necessarily, although it was indirectly, rather it was copying the contents of the iframe to a literal control on the main page and making everything but the literal control invisible with my custom print.css file.

Does that make sense?
<$.02>
As with anything to do with programming, sense arises from necessity ;-)
</$.02>
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America 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