我想只打印iframe的内容而不是整个页面

时间:2013-09-05 17:53:33

标签: c# javascript

我在Iframe中显示PDF,我有一个动态按钮,上面写着Print。单击该按钮,我只想打印iframe中显示的PDF而不是整个页面。我正在使用以下代码进行打印,但它正在打印整个页面。

try {
iframe = document.getElementById('id of iframe');
iframe.contentWindow.document.execCommand('print', false, null);
}

catch (e) {
window.print();
}

在这种情况下,如何仅打印iframe的内容?

1 个答案:

答案 0 :(得分:0)

如果您的按钮位于iframe之外,则点击操作会将焦点设置为父窗口,而不是具有要打印内容的iframe。

如果是这种情况,请参阅此帖: Setting focus to iframe contents

相关问题