Javascript 如何在打印机中打印图像文件

Javascript 如何在打印机中打印图像文件,javascript,image,printing,Javascript,Image,Printing,我写了一个简单的程序来打印JSF中的图像 <h:commandButton value="Print" onclick="printImage();"/><br> <rich:panel id="imageViewerPanel"> <h:graphicImage id="imageViewer" url="sampleImage.png"

我写了一个简单的程序来打印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>
我有一个图像(sampleImage.png)。。我已经将我的电脑连接到打印机

  <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>
手动打开图像并选择“打印”选项,然后从打印机获取图像

  <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>
现在我想要使用javascript打印图像

  <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>
文件名:imagePrint.jsp

  <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>

  <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>

打印机
函数printImage()
{                                       
//这里我想写一个打印图像的脚本
}

  <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>

  <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>
帮我解决这个问题

  <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>
以下用于将文本区域打印到打印机中的脚本。。。。。 所以我需要打印图像

            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();
            }             
  <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>

  <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>
函数printText()
{
警报(“文本区打印到打印机启动…);
var textElem=document.getElementById(“FileViewPerform:textAreaGrid1”).innerHTML;
警报(“文本区域内容:“+textElem”);

如果(textElem.toLowerCase().indexOf(“您将无法使用javascript打印,因为您无法从浏览器管理硬件设备并在那里执行它。

您可以将其发送到浏览器

   window.print();
  <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>
由浏览器决定做什么

  <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>
打印页面的特定部分时,考虑打印样式表。使用<代码>媒体< /代码>属性允许您创建特定文件的仅打印样式。

<link rel="stylesheet" href="/assets/css/print.css" media="print" />
  <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>

您将使用这两种方法接收图像id h:form和h:graphicImage标记id

  <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>
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();           
}
  <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>
函数printImage() { var iamgeId=document.getElementById('FileViewPerform:imageViewer'); var imagObject=新图像(); imagObject=iamgeId; var originalImage=''; popup=window.open(“”,'popup','toolbar=no,menubar=no,width=200,height=150'); popup.document.open(); popup.document.write(“”); 弹出。文档。写入(原始图像); popup.document.write(“”); 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>

  <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>

  <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>

  <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

  <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>
通过,

NEC Eswara Moorthy。

感谢您的回复…现在您可能会再次看到我的帖子…我已经为打印文本区域内容编写了脚本…您好,此方法用于打印整个窗口…但我只需要打印特定图像…例如,在我的页面中有徽标…所以我只需要使用徽标,而不是页面的全部内容。。。我是新来的。我不明白这个…我在css文件中写了什么内容。。。
  <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>