Thursday, June 16, 2011

Javascript to print WebPart in Sharepoint


 <script type="text/javascript" language="JavaScript">
//Controls which Web Part or zone to print 'TD ID'
var WebPartElementID = "MainTable";

//Function to print Web Part
function PrintArea()
var bolWebPartFound = false;
if (document.getElementById != null)
//Create html to print in new window
var PrintingHTML = '<HTML>\n<HEAD>\n';

//Take data from Head Tag
PrintingHTML += '\n</HEAD>\n<BODY class="printSQS">\n';
var WebPartData = document.getElementById(WebPartElementID);
if (WebPartData != null)
PrintingHTML += WebPartData.innerHTML;
bolWebPartFound = true;
bolWebPartFound = false;
alert ('Cannot Find Web Part');
PrintingHTML += '\n</BODY>\n</HTML>';

//Open new window to print
if (bolWebPartFound)
var PrintingWindow ="","PrintWebPart",
// Open Print Window


HTML Source Code:

<div id="MainTable">
place your html that you want to print

<input type="button" name="btPrint" value="Print-Friendly" onclick="javascript:void(PrintArea());return false;__doPostBack('btPrint','')" id="btPrint" />

No comments:

Post a Comment