2010-04-30 93 views
0

编写一个简单的程序在JSF打印图像....我如何打印图像文件在打印机

我有一个形象(sampleImage.png)..我已经连我的电脑打印机.. ..

手动我打开图像,然后选择打印选项,然后我得到的图像从打印机....

现在我使用JavaScript要打印图像....

文件名:imagePrint。 jsp

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Printer</title>   
     <script type="text/javascript"> 
      function printImage() 
      {          
       // Here i want write a script for print image        
      } 
     </script> 

<body> 
     <h:form id="fileViewerForm"> 
      <rich:panel> 
       <f:facet name="header"> 
        <h:outputText value="Printer"/> 
       </f:facet> 

       <h:commandButton value="PrintImage" onclick="printImage();"/> 

        <rich:panel id="imageViewerPanel">        
         <h:graphicImage id="imageViewer" value="sampleImage.png" url="sampleImage.png"/>    
        </rich:panel>          
      </rich:panel> 
     </h:form> 
    </body> 
</html> 

帮助我这个..

打印下面的脚本textarea的入打印机..... 所以我需要打印的图像

  function printText() 
      { 
       alert("Text Area print to printer start...."); 
       var textElem = document.getElementById("fileViewerForm:textAreaGrid1").innerHTML; 
       alert("Text Area Content : " + textElem); 

       if(textElem.toLowerCase().indexOf("<textarea", 0) != -1) 
       { 
        textElem = document.getElementById("fileViewerForm:fileContent1").value; 
        var regExp = /\n/gi; 
        textElem = textElem.replace(regExp,'<br>'); 
       } 
       popup = window.open('','popup','toolbar=no,menubar=no,width=200,height=150'); 
       popup.document.open(); 
       popup.document.write("<html><head></head><body onload='print()'>"); 
       popup.document.write(textElem); 
       popup.document.write("</body></html>"); 
       popup.document.close(); 
      }    

回答

1

您将同时使用 ^ h接收图像ID:形式和h:graphicImage的标签ID的

Java的脚本是:

function printImage()   
{    
    var iamgeId = document.getElementById('fileViewerForm:imageViewer'); 

    var imagObject = new Image(); 
    imagObject = iamgeId; 
    var originalImage = '<img id="imageViewer" src="'+imagObject.src+'" 
         height="'+imagObject.height+'" 
         width="'+imagObject.width+'" />'; 

    popup = window.open('','popup','toolbar=no,menubar=no,width=200,height=150'); 
    popup.document.open(); 
    popup.document.write("<html><head></head><body onload='print()'>"); 
    popup.document.write(originalImage); 
    popup.document.write("</body></html>"); 
    popup.document.close();   
} 

JSF代码:

<h:commandButton value="Print" onclick="printImage();"/><br> 
     <rich:panel id="imageViewerPanel">     

      <h:graphicImage id="imageViewer" url="sampleImage.png" 
          value="sampleImage.png" width="200" 
                height="200" /> 
     </rich:panel> 
    </h:panelGrid> 

它适用于火狐3.0.18。

By,
Eswara Moorthy,NEC。

1

你将不能够使用JavaScript来打印,因为你不能管理从浏览器的硬件设备,并执行那里。

+0

感谢您的回复... 现在,您可能会再次看到我的帖子... 已经编写了一个用于打印文本区域内容的脚本... – jackrobert 2010-04-30 03:52:21

0

你可以发送这个浏览器

window.print(); 

它是由浏览器来决定做什么。

要打印页面的特定部分,请考虑打印样式表。使用media属性可以使某个文件仅打印样式。

<link rel="stylesheet" href="/assets/css/print.css" media="print" /> 
+0

嗨,这种方法是用来打印整个窗口... 但我只需要打印特定图像.. 例如,在我的网页上有标志...所以我只需要采取标志,而不是全部内容页面... – jackrobert 2010-04-30 03:59:35

+1

使用打印样式表。 – alex 2010-04-30 04:05:30

+0

我是新来的。我无法理解这... 我写入CSS文件的内容... – jackrobert 2010-04-30 05:13:06