2010-04-30 137 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