Monday, January 31, 2011

Directly print any div of web page using JavaScript & HTML


Sample Div that is to print:
<div id="divPrint">
  <br />
<asp:Label ID="lblDetails" runat="server" Font-Bold="True" Font- Size="Medium">
</asp:Label>
  <br />                                   
      <asp:GridView ID="gvSample" runat="server">
      </asp:GridView>
</div>


JavaScript Code:
<script type="text/javascript">

        function CallPrint(strid) {
            var prtContent = document.getElementById(strid);
            var width = 600;
            var height = 500;
            var left = (screen.width - width) / 2;
            var top = (screen.height - height) / 2;
            var WinPrint = window.open('', '', 'letf=' + left + ',top=' + top + ',width=' + width + ',height=' + height + ',toolbar=0,scrollbars=0,status=0');
            WinPrint.document.write(prtContent.innerHTML);
            WinPrint.document.close();
            WinPrint.focus();
            WinPrint.print();
            WinPrint.close();
        }
</script>


Print Button:
<input type="button" value="Print" id="btnPrint" runat="Server" onclick="javascript:CallPrint('divPrint')" />

No comments:

Post a Comment