带代码的CEWP打印按钮可以打印SharePoint表单吗?

时间:2013-10-16 15:42:00

标签: javascript sharepoint cewp

我目前有一个表单的预览,它看起来我们希望它如何查找正在查看的列表项但问题是当我在FOrm显示中添加我的打印按钮作为CEWP它执行与使用Control完全相同的功能P并打印整个页面。见代码。

          <div style="text-align: center"><input onclick="window.print();return false;" type="button" value=" Print this page "/>&#160;</div>"

我想添加到这个上面,让它只打印窗体,窗口中当前视图外面没有其他内容,实际填充8.5乘11。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

this InfoPath print button的启发,一个解决方案是获取CEWP的内容并创建一个仅包含这些内容的新窗口。

var patt = /**your CEWP ID here***/g;
var alldivs = document.getElementsByTagName('div');
var printpageHTML = '';
for(var i=0; i<alldivs.length; i++){
  if(patt.test(alldivs[i].id)){
    printpageHTML = '<HTML><HEAD>\n' +
      document.getElementsByTagName('HEAD')[0].innerHTML +
      '</HEAD>\n<BODY>\n' + 
      alldivs[i].innerHTML.replace('inline-block','block') + 
      '\n</BODY></HTML>';
    break;
  }
}
var printWindow = window.open('','printWindow');
printWindow.document.open();
printWindow.document.write(printpageHTML);
printWindow.document.close();
printWindow.print();
printWindow.close();

修正:删除了HTML字符的转义。

答案 1 :(得分:0)

简单的打印功能:

<input id="printpagebutton" type="button" value="Print this page" onclick="printpage()"/>

<script type="text/javascript">
function printpage() {
    //Get the print button and put it into a variable
    var printButton = document.getElementById("printpagebutton");
    //Set the print button visibility to 'hidden' 
    printButton.style.visibility = 'hidden';
    //Print the page content
    window.print()
    //Set the print button to 'visible' again 
    //[Delete this line if you want it to stay hidden after printing]
    printButton.style.visibility = 'visible';
  }
 </script>