2011-12-09 54 views
2

我正在尝试使用示例here来打印ASP.NET Image如何使用javascript打印图像

当我使用上面的例子为一个股利,它的工作原理,但我无法适应它打印其他任何东西。我怎样才能打印出Image?我是否将Image封装在div中,并将脚本中的“.text()”部分更改为其他内容?

回答

7

因为您将错误的参数传递给打印功能。用JavaScript打印东西就像调用window.print();方法一样简单。为了测试它,只需使用开发工具的浏览器和写入其控制台:

window.print(); 

现在,当你要打印一些特定的,有两种方式:

  1. 创建一个特殊的用于在同一页面中打印的样式表,隐藏其他元素并仅显示指定的区域。
  2. 或者打开一个新窗口,在那里复制你想要的东西,然后打印出来。

现在,你可以做的是写自己的函数:

function printImage(image) 
{ 
     var printWindow = window.open('', 'Print Window','height=400,width=600'); 
     printWindow.document.write('<html><head><title>Print Window</title>'); 
     printWindow.document.write('</head><body ><img src=\''); 
     printWindow.document.write(image.src); 
     printWindow.document.write('\' /></body></html>'); 
     printWindow.document.close(); 
     printWindow.print(); 
} 

var image = document.getElementById('image'); 
printImage(image); 

,你也可以看到在行动here此功能。

只要让浏览器打开弹出窗口,并且还请注意,我只会将图像元素的src值传递到新窗口。

+0

您还可以添加'printWindow.close()'作为最后一行自动打印后关闭窗口。 – Nate

0

是的,而不是使用.text(),您使用.html()您将<img />(包括当然的来源)传递给假文档准备打印。

+0

尝试过。只是在新窗口中显示文字“null”,而不是图像。尝试使用原始,以防万一asp.net图像无法正常渲染 – user982119

+0

您可以发布您的代码吗? –

0

为什么不把它变得这么简单?

<input type="button" value="Print Div" onclick="PrintElem('<img src=myimage.jpg>')" /> 

,然后调用弹出这样的: 弹出(ELEM);

(你,如果你传递要打印的图像这样并不真的需要这个弹出()函数)

2

是。

以下java脚本将帮助您在图像控件中打印图像。

var printContent = document.getElementById('divImage'); 
var img = document.getElementById('imgMain'); 

var windowUrl = 'MyPage.aspx'; 
var uniqueName = new Date(); 
var windowName = "MyPage" + uniqueName.getTime(); 

printWindow = window.open(windowUrl, windowName, 'location=1,status=1,scrollbars=1,width=800,height=600'); 

printWindow.document.write("<div style='width:100%;'>"); 
printWindow.document.write("<img id='img' src='" + img.src + "'/>"); 
printWindow.document.write("</div>"); 

printWindow.document.close(); 
printWindow.focus(); 
printWindow.print(); 
printWindow.close(); 
return false; 
0

这是更好的解决办法是,在谷歌浏览器也工作得很好:

var printWindow = window.open('', 'Print Window', 'height=533,width=800'); 
     printWindow.document.write('<html><head><title>Print Window</title>'); 

     printWindow.document.write("<script src='script/jquery-1.11.3.min.js' type='text/javascript'><\/script>"); 


     printWindow.document.write("<script type='text/javascript'>"); 

     printWindow.document.write("var DoPrint = false;"); 
     printWindow.document.write("$(document).ready(function() {"); 

     printWindow.document.write("DoPrint = true;"); 

     printWindow.document.write("});"); 

     printWindow.document.write("<\/script>"); 


     printWindow.document.write('</head><body ><img src=\''); 
     printWindow.document.write(document.getElementById('image').src); 
     printWindow.document.write('\' /></body></html>'); 
     printWindow.document.close(); 


     function Print() { 

      if (typeof printWindow.DoPrint != "undefined" && printWindow.DoPrint == true) { 

       clearInterval(PrintintervalNumber); 

       printWindow.print(); 
       printWindow.close(); 

      } 

     } 


     PrintintervalNumber = setInterval(Print, 1000);