簡體   English   中英

如何在打印機中打印圖像文件

[英]How to print a image file in printer

我寫了一個簡單的程序在JSF中打印圖像。

我有一張圖像(sampleImage.png)。我已經將我的PC連接到打印機了。

手動打開圖像並選擇打印選項,然后從打印機獲取圖像。

現在我想使用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();
            }             

您將無法使用JavaScript進行打印,因為您無法通過瀏覽器管理硬件設備,並且無法在其中執行。

您將同時使用h:form和h:graphicImage標記ID來接收圖像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>

它適用於FireFox 3.0.18。

通過,
NEC的Eswara Moorthy。

您可以將其發送到瀏覽器

   window.print();

由瀏覽器決定要做什么。

要打印頁面的特定部分,請考慮使用打印樣式表。 使用media屬性可以使某些文件僅打印樣式。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM