1

我尝试动态加载图像,一切正常。我加载并以动态方式正确显示的图像,我添加了用于打印的标签。现在,如果我要求打印动态创建的图像,我将无法打印。

       <pou:commandButton value="Print" type="button" icon="ui-icon-print">  
                    <pou:printer target="image"  />  
       </pou:commandButton>     
       <pou:graphicImage id="image" value="#{printDynamicBean.graphicIMG}" />  

我的豆子是这样的:

    public StreamedContent getGraphicIMG() {
        //Graphic   
        BufferedImage bufferedImg;
        try {
            bufferedImg = ImageIO.read(baseImage);
        } catch (IOException e) {

        }
        try {

          Graphics2D g2 = bufferedImg.createGraphics();
          g2.setColor(Color.black);
          int style = Font.BOLD | Font.ITALIC;
          Font f1 = new Font("TimesNewRoman", style , 60);
          g2.setFont(f1);
          g2.drawString("Hello Print", 80, 580);
          ByteArrayOutputStream os = new ByteArrayOutputStream();
          ImageIO.write(bufferedImg, "png", os);
          graphicIMG = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");
        } catch (IOException ex) {
          Logger.getLogger(PrintCartelliniBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return graphicIMG;


}

就好像她忘记了创造的形象。

谢谢。

4

1 回答 1

2
using CDI bean you can do this : 

@Model
public class ImageBean {

    private StreamedContent image;

    @Produces
    @Named
    public StreamedContent getImage() {
        if (FacesContext.getCurrentInstance().getRenderResponse()) {
            // rendering the view, return a stub StreamedContent to generate right URL.
            image = new DefaultStreamedContent();
        } else {
            // requesting the image
            image = your dynamic image;
        }

        return image;
    }
}
  • 在你看来:<pou:graphicImage id="image" value="#{image}" />
于 2013-04-29T19:14:36.583 回答