How to print contents of Div only

Generally we do not want to create complex report and our clients are not interested in purchasing expensive reporting tools then we can generate simple HTML based reports for them. You just have to place your report inside a< div>... < / div> and you can easily print that. Here is a sample through which you can easily print contents of specified div only:-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <script language="javascript" type="text/javascript">
        function printDiv(divID) {
            //Get the HTML of div
            var divElements = document.getElementById(divID).innerHTML; 
            //Get the HTML of whole page
            var oldPage = document.body.innerHTML;
            
            //Reset the page's HTML with div's HTML only
            document.body.innerHTML = "<html><head><title></title></head><body>" + divElements + "</body>";
            
            //Print Page
            window.print();
            
            //Restore orignal HTML
            document.body.innerHTML = oldPage;
            
            //disable postback on print button
            return false;
        }
    </script>
    <title>Div Printing Test Application by Zeeshan Umar</title>
</head>
<body>
    <form runat="server">
    <asp:Button ID="btnPrint" runat="server" Text="Print" OnClientClick="return printDiv('div_print');" />
    <div id="garbage1">I am not going to be print</div>
    <div id="div_print"><h1 style="color: Red">Only Zeeshan Umar loves Asp.Net will be printed :D</h1></div>
    <div id="garbage2">I am not going to be print</div>
    </form>
</body>


Here is the output of the page when I run the code:-



And when I pressed print button, it hide all the remaining area and only my div's contents are visible which will be printed:-


Feel free to comment if you require any clarification. Happy coding !!!

Comments (15)

Loading... Logging you in...
  • Logged in as
Hi... ive been this coding for printing div. it work correctly in firefox and chrome but not in ie. does this coding support ie?
1 reply · active 712 weeks ago
As you can see in snapshot it is Internet Explorer, can you share what is your browser's version?

Also there is a chance that in your page you might have some special HTML tag which is causing my script to stop work.
As you can see in snapshot it is Internet Explorer, can you share what is your browser's version?

Also there is a chance that in your page you might have some special HTML tag which is causing my script to stop work.
impressive way to print reports
1 reply · active 700 weeks ago
it works correctly with all browsers but jquery and javascript function are disabled
any way to enable them
1 reply · active 675 weeks ago
Unfortunately it is not possible. It is user's choice that he wants to run java script of not. However you can display no script message if java script is disabled. For details see:-
http://www.w3schools.com/tags/tag_noscript.asp
Hi

this is very useful
1 reply · active 658 weeks ago
is this able for image also
bcoz im trying 4 hours but when i add image control in div , the whole page is printed

please help me asap
Zeeshan,

This is an interesting approach that I had not thought of before. I can see it being useful in a few scenarios. It is fast and easy to implement for a single report page.

However, the "standard" method of hiding unwanted elements for printing is to use CSS media types. This can be done either by adding a @media block to a stylesheet, or including a special stylesheet to handle printing.

Here is a brief tutorial on the subject: http://www.javascriptkit.com/dhtmltutors/cssmedia...

Once you have a stylesheet for printing, you can hide page elements from the printer like so:

<style type="text/css" media="print">
.no-print, #header, #footer, #navbar { display: none; visibility: hidden; }
</style>

With this approach, all of the elements will appear on-screen, but the selected elements will be hidden when printing. This does not break any JavaScript or embedded objects, so you could even print the contents of JavaScript generated charts and graphs.

BTW, another use of this CSS method is to prevent users from printing completely. By hiding the body contents, you could prevent users from printing subscription-based content.

<style type="text/css" media="print">
#body { display: none; visibility: hidden; }
</style>

Regards,
Kevin
1 reply · active 555 weeks ago
Interesting one, thanks for sharing
Dear Zeeshan,

Is there anyway to call the java script function from the code behind .cs file?

Regards
Hussain
1 reply · active 555 weeks ago
Hussain,

you can use scriptmanager.registerstartupscript function to call javascript from c# code.

Have a look at this:-
http://www.codeproject.com/Questions/443131/Call-...
It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.

Post a new comment

Comments by