views:

179

answers:

2

I have an html string and I want to print it out. I'm okay with it opening the confirm or print preview dialog box I just want a function like this

HtmlPrinter.PrintHtml("<html><body>Page I'm printing</body></html>");

anyone know anything that will work like this?

+3  A: 

Web browsers give you the ability to print the current page. You can trigger the print dialog box via javascript and you can use CSS to style that page such that the printed output bears little resemblance to what's shown on the screen, but that's about it as far as printing goes.

Going beyond plain html, you might be able to use something like flash, silverlight, or another browser plugin. But really the closest you can get is something like this:

<html>
    <head>
        <style>
            @media print
            {
               .HidePrint { display:none; visible:none;}
            }
            @media screen
            {
                .HideScreen {display:none; visible:none;}
            }
        </style>
    </head>
    <body onload="window.print();">
        <div class="HideScreen">
            Page I'm printing
        </div>
        <div class="HidePrint">
            Other content...
        </div>
    </body>
</html>
Joel Coehoorn
Awesome, thanks! Didn't know you could use @media to specify different style sheets for printing.
Matt
A: 

Joels answer is correct, obviously.

To help you understand why; C# runs on the server, JavaScript runs on the client. If you did try and print from C#, you'd be printing from your server machine :)

Noon Silk
Makes sense, thanks for the explanation. Probably should have posted this as a comment though, I'm guessing that's why your answer got down voted.
Matt
*shrug*, didn't make sense as a comment to his post or yours, IMHO, but I'm new here. I don't mind being downvoted because my information is still correct :)
Noon Silk