2011-09-13 165 views
1
  1. 如何打印部分页面?打印部分页面并在新窗口中打开部分页面Asp.Net

  2. 如何在新窗口中显示部分页面?然后用户打印。

注意:部分页面位于IFrame中。

+0

什么叫部分页面呢?如果在IFrame中加载“partial page”,则可以使用javascript:window.open('partialpage.aspx','Partial Page);以弹出新窗口 – Waqas

+0

是编号打开新窗口并且不打开新窗口,打印iframe中的内容。 – TroyS

回答

2

打印IFrame的内容时有一些限制。 JavaScript安全策略会阻止您在其他域中打印某些内容。这是我多年来用来在页面上打印特定内容的方法。

首先你需要一个简单的JavaScript打印功能。这是我使用的一个

print = function(str) { 
    var WinPrint = window.open('', '', 'left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0'); 
    WinPrint.document.open("text/html", "replace"); 
    WinPrint.document.write('<html><head><title>Printing</title></head><body>'); 
    WinPrint.document.write(str); 
    WinPrint.document.write('</body></html>'); 
    WinPrint.document.close(); 
    WinPrint.focus(); 
    WinPrint.print(); 
    WinPrint.close(); 
}; 

然后,你需要从IFRAME

getIFrameContent = function(IFrameID) { 
    return document.getElementById(IFrameID).contentWindow.document.body.innerHTML; 
}; 

然后一个简单的功能拉HTML来包装整个事情更容易

printIFrame = function(IFrameID) { 
    print(getIFrameContent(IFrameID)); 
}; 

一旦你拥有了所有可以调用printIFrame方法并简单地发送IFrame的ID的方法。方法2: 在新窗口中打开Iframe并打印该窗口。

var printIFrame2 = function (IFrameID) { 
     var URL = document.getElementById(IFrameID).getAttribute("src"); 
     var WinPrint = window.open(URL, 'WinPrint', 'left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0'); 
     WinPrint.focus(); 
     WinPrint.print(); 
     WinPrint.close(); 
    }; 
+0

太好了,我会试试看。 – TroyS

+0

我把它连接起来并在代码后面的菜单项中做了一个ScriptManager.RegisterStartupScript。它调用它,但我得到第一个代码块的错误print = function(str){.... Microsoft JScript运行时错误:对象不支持这个动作 – TroyS

+0

好吧,现在我有它的工作,它只打印文本但是,如何在IFrame,Div或任何控件中打印所有内容? – TroyS

1

我不知道其他人发布的代码,除非我问一个问题。所以对于Cris来说,这是你要将JavaScript放在主页面中的吗?

<script type="text/javascript"> 
    print = function (str) { 
     var WinPrint = Window.open('', '', 'left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0'); 
     WinPrint.document.open(""); 
     WinPrint.document.write(''); 
     WinPrint.document.write(str); 
     WinPrint.document.write('</body></html>'); 
     WinPrint.document.close(); 
     WinPrint.focus(); 
     WinPrint.print(); 
     WinPrint.close(); 
    }; 
    getIFrameContent = function (IFrameID) { 
     return document.getElementById(IFrameID).contentWindow.document.body.innerHTML; 
    }; 
    printIFrame = function (IFrameID) { 
     print(getIFrameContent(IFrameID)); 
    }; 

</script> 

然后从后面的代码调用,像这样......

ScriptManager.RegisterStartupScript(Page, Me.GetType(), "key", "printIFrame(" & MyIFrame.ID & ");", True) 
+0

如果您需要从服务器端调用打印iframe,这将起作用。另一种方法是在页面上添加一个按钮或超链接,调用脚本就像'Print IFrame' –

0

这是我测试:

页的打印部分:

<div id="printArea"> 
    <h1>I am going to print from here</h1> 
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> 
</div> 

打印按钮:

<a href="#" onClick="printDiv();return false;">Print</a> 

JavaScript代码:

function printDiv() { 
    var w = window.open('', '','width=auto,height=auto,resizeable,scrollbars'); 
    w.document.write($("#printArea").html()); 
    w.document.close(); 
    javascript:w.print(); 
    w.close(); 
    return false; 
} 
相关问题