2

嗨伙计们!我设计了一个碧玉报告导出为包含存储在我本地机器中的图像的 pdf。现在根据我的需要,我需要从项目类路径中动态添加图像。下面我发布我的代码。请帮助我如何动态添加图像...

 File tempFile = File.createTempFile(getClass().getName(), ".pdf");
    try {
        FileOutputStream fos = new FileOutputStream(tempFile);
        try {
            ServletOutputStream servletOutputStream = response.getOutputStream();
            InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("jasperpdf.jasper");
            try {
                String datum1 = request.getParameter("fromdate");
                String datum2 = request.getParameter("todate");
                SimpleDateFormat sdfSource = new SimpleDateFormat("dd-MM-yyyy");
                Date date = sdfSource.parse(datum1);
                Date date2 = sdfSource.parse(datum2);
                SimpleDateFormat sdfDestination = new SimpleDateFormat("yyyy-MM-dd");
                datum1 = sdfDestination.format(date);
                System.out.println(datum1);
                datum2 = sdfDestination.format(date2);
                System.out.println(datum2);

                String strQuery = "";
                ResultSet rs = null;

                conexion conexiondb = new conexion();
                conexiondb.Conectar();

                strQuery = "Select calldate,src,dst,duration,disposition,cdrcost from cdrcost where date(calldate) between '" + datum1 + "' and '" + datum2 + "'";

                rs = conexiondb.Consulta(strQuery);
                JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);

                JasperRunManager.runReportToPdfStream(reportStream, fos, new HashMap(), resultSetDataSource);
                rs.close();
4

2 回答 2

0

我是 jasper 报告的新手,可能这个代码对你有用

private static JRDesignImage getImage(int x_postion, int y_position, int width, int height,ScaleImageEnum scale_type, HorizontalAlignEnum align_type,
        JRDesignExpression expression) {
        JRDesignImage image = new JRDesignImage(null); 
        image.setX(0); 
        image.setY(8); 
        image.setWidth(97); 
        image.setHeight(50); 
        image.setScaleImage(ScaleImageEnum.RETAIN_SHAPE); 
        image.setHorizontalAlignment(HorizontalAlignEnum.LEFT);

        image.setExpression(expression); 
    // TODO Auto-generated method stub
    return image;
}

然后加

band = new JRDesignBand();
    band.setHeight(73);

    expression = new JRDesignExpression(); 
    expression.setValueClass(java.lang.String.class); 
    expression.setText("$P{imagePath}");
   // jasperDesign.addField();

    band.addElement(getImage(0,8,97,50,ScaleImageEnum.RETAIN_SHAPE,HorizontalAlignEnum.LEFT,expression)); 
于 2013-07-11T10:08:33.480 回答
0

当您提供图像的相对路径时它是否有效?即images/image.jpg您的项目中应该有一个名为 images 的文件夹,其中应该有文件 image.jpg ..

于 2013-06-11T16:39:36.843 回答