Matt
Matt

Reputation: 5595

How can you print an html string from C# (with user permision)?

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?

Upvotes: 0

Views: 2158

Answers (2)

Noon Silk
Noon Silk

Reputation: 55082

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 :)

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415735

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>

Upvotes: 3

Related Questions