2011-09-13 14 views
1
  1. 部分ページを印刷するにはどうすればよいですか?新しいウィンドウで部分ページと部分ページを表示Asp.Net

  2. 新しいウィンドウに部分ページを表示するにはどうすればよいですか?ユーザーが印刷します。

注:部分ページはIFrame内にあります。

+0

あなたは、部分的なページとはどういう意味ですか? IFrame内に「部分ページ」が読み込まれている場合は、javascript:window.open( 'partialpage.aspx'、 'Partial Page);を使用できます。新しいウィンドウをポップアップする – Waqas

+0

はい新しいウィンドウを開き、新しいウィンドウを開くことなくiframeの内容を印刷したいのですが – TroyS

答えて

2

IFrameの内容を印刷する際にはいくつかの制限があります。 JavaScriptセキュリティポリシーは、別のドメインにあるものが印刷されないようにします。これは、私が何年にもわたりページ上の特定のコンテンツを印刷するために使用した方法です。

まず、JavaScriptで簡単な印刷機能が必要です。これは、私はあなたが作るために全体をラップするためにその後のiframe

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

単純な関数からHTMLをプルする必要があるその後

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(); 
}; 

を使用するものであることが容易

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を印刷する必要がある場合に機能します。もう1つの方法は、スクリプトを呼び出すページにボタンやハイパーリンクを追加することです。例えば、 '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; 
} 
関連する問題