Example usage for org.apache.poi.hssf.usermodel HSSFClientAnchor setAnchorType

List of usage examples for org.apache.poi.hssf.usermodel HSSFClientAnchor setAnchorType

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFClientAnchor setAnchorType.

Prototype

@Override
public void setAnchorType(AnchorType anchorType) 

Source Link

Document

Sets the anchor type

Usage

From source file:com.automaster.autoview.server.servlet.TesteImagemPOI.java

public static void main(String[] args) throws FileNotFoundException, IOException {

    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet("new sheet");
    byte data[] = new byte[8000];
    try {/*from   ww w.  j a  v a 2 s  . com*/
        new DataInputStream(new FileInputStream(
                "D:\\Users\\Adriano\\Documents\\NetBeansProjects\\JRGWT\\web\\imagens\\logo.jpg")).read(data);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    int index = wb.addPicture(data, HSSFWorkbook.PICTURE_TYPE_JPEG);

    HSSFClientAnchor ancora = new HSSFClientAnchor(0, 0, 0, 0, (short) 0, 5, (short) 10, 10);
    ancora.setAnchorType(2);
    sheet.createDrawingPatriarch().createPicture(ancora, index);

    //Write the Excel file
    FileOutputStream fileOut = null;
    fileOut = new FileOutputStream("C://myFile.xls");
    wb.write(fileOut);
    fileOut.close();

}

From source file:com.krawler.esp.servlets.exportExcel.java

License:Open Source License

public void addimage(String imagepath, int pictype, HSSFWorkbook wb, HSSFSheet sheet, int dx1, int dy1, int dx2,
        int dy2, int col1, int row1, int col2, int row2) throws IOException {
    FileInputStream fimage = null;
    ByteArrayOutputStream bos = null;
    try {//from  w  w w .  ja  va2 s . c  o  m
        fimage = new FileInputStream(imagepath);
        bos = new ByteArrayOutputStream();
        int c;
        while ((c = fimage.read()) != -1)
            bos.write(c);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println(e);
    } finally {
        fimage.close();
    }

    int imgindex = wb.addPicture(bos.toByteArray(), pictype);
    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
    HSSFClientAnchor anchor;
    anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, (short) col1, row1, (short) col2, row2);
    anchor.setAnchorType(2);
    patriarch.createPicture(anchor, imgindex);
}

From source file:com.siteview.ecc.report.xls.JRXlsExporter.java

License:Open Source License

protected void exportImage(JRPrintImage element, JRExporterGridCell gridCell, int colIndex, int rowIndex,
        int emptyCols) throws JRException {
    try {//from w w w .ja v  a 2  s .c  om
        int topPadding = Math.max(element.getLineBox().getTopPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getTopPen()));
        int leftPadding = Math.max(element.getLineBox().getLeftPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getLeftPen()));
        int bottomPadding = Math.max(element.getLineBox().getBottomPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getBottomPen()));
        int rightPadding = Math.max(element.getLineBox().getRightPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getRightPen()));

        //pngEncoder.setImage( null );

        int availableImageWidth = element.getWidth() - leftPadding - rightPadding;
        availableImageWidth = availableImageWidth < 0 ? 0 : availableImageWidth;

        int availableImageHeight = element.getHeight() - topPadding - bottomPadding;
        availableImageHeight = availableImageHeight < 0 ? 0 : availableImageHeight;

        JRRenderable renderer = element.getRenderer();

        if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
            if (renderer.getType() == JRRenderable.TYPE_IMAGE) {
                // Image renderers are all asked for their image data and dimension at some point.
                // Better to test and replace the renderer now, in case of lazy load error.
                renderer = JRImageRenderer.getOnErrorRendererForImageData(renderer, element.getOnErrorType());
                if (renderer != null) {
                    renderer = JRImageRenderer.getOnErrorRendererForDimension(renderer,
                            element.getOnErrorType());
                }
            }
        } else {
            renderer = null;
        }

        if (renderer != null) {
            int normalWidth = availableImageWidth;
            int normalHeight = availableImageHeight;

            Dimension2D dimension = renderer.getDimension();
            if (dimension != null) {
                normalWidth = (int) dimension.getWidth();
                normalHeight = (int) dimension.getHeight();
            }

            float xalignFactor = 0f;
            switch (element.getHorizontalAlignment()) {
            case JRAlignment.HORIZONTAL_ALIGN_RIGHT: {
                xalignFactor = 1f;
                break;
            }
            case JRAlignment.HORIZONTAL_ALIGN_CENTER: {
                xalignFactor = 0.5f;
                break;
            }
            case JRAlignment.HORIZONTAL_ALIGN_LEFT:
            default: {
                xalignFactor = 0f;
                break;
            }
            }

            float yalignFactor = 0f;
            switch (element.getVerticalAlignment()) {
            case JRAlignment.VERTICAL_ALIGN_BOTTOM: {
                yalignFactor = 1f;
                break;
            }
            case JRAlignment.VERTICAL_ALIGN_MIDDLE: {
                yalignFactor = 0.5f;
                break;
            }
            case JRAlignment.VERTICAL_ALIGN_TOP:
            default: {
                yalignFactor = 0f;
                break;
            }
            }

            BufferedImage bi = new BufferedImage(element.getWidth(), element.getHeight(),
                    BufferedImage.TYPE_INT_ARGB);
            Graphics2D grx = bi.createGraphics();

            switch (element.getScaleImage()) {
            case JRImage.SCALE_IMAGE_CLIP: {
                int xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
                int yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));

                Shape oldClipShape = grx.getClip();

                grx.clip(new Rectangle(leftPadding, topPadding, availableImageWidth, availableImageHeight));

                try {
                    renderer.render(grx, new Rectangle(xoffset + leftPadding, yoffset + topPadding, normalWidth,
                            normalHeight));
                } finally {
                    grx.setClip(oldClipShape);
                }

                break;
            }
            case JRImage.SCALE_IMAGE_FILL_FRAME: {
                renderer.render(grx,
                        new Rectangle(leftPadding, topPadding, availableImageWidth, availableImageHeight));

                break;
            }
            case JRImage.SCALE_IMAGE_RETAIN_SHAPE:
            default: {
                if (element.getHeight() > 0) {
                    double ratio = (double) normalWidth / (double) normalHeight;

                    if (ratio > (double) availableImageWidth / (double) availableImageHeight) {
                        normalWidth = availableImageWidth;
                        normalHeight = (int) (availableImageWidth / ratio);
                    } else {
                        normalWidth = (int) (availableImageHeight * ratio);
                        normalHeight = availableImageHeight;
                    }

                    int xoffset = leftPadding + (int) (xalignFactor * (availableImageWidth - normalWidth));
                    int yoffset = topPadding + (int) (yalignFactor * (availableImageHeight - normalHeight));

                    renderer.render(grx, new Rectangle(xoffset, yoffset, normalWidth, normalHeight));
                }

                break;
            }
            }

            short mode = backgroundMode;
            short backcolor = whiteIndex;
            if (!isIgnoreCellBackground && gridCell.getCellBackcolor() != null) {
                mode = HSSFCellStyle.SOLID_FOREGROUND;
                backcolor = getNearestColor(gridCell.getCellBackcolor()).getIndex();
            }

            short forecolor = getNearestColor(element.getLineBox().getPen().getLineColor()).getIndex();

            if (element.getMode() == JRElement.MODE_OPAQUE) {
                backcolor = getNearestColor(element.getBackcolor()).getIndex();
            }

            HSSFCellStyle cellStyle = getLoadedCellStyle(mode, backcolor, HSSFCellStyle.ALIGN_LEFT,
                    HSSFCellStyle.VERTICAL_TOP, (short) 0,
                    getLoadedFont(getDefaultFont(), forecolor, null, getLocale()), gridCell);

            createMergeRegion(gridCell, colIndex, rowIndex, cellStyle);

            cell = row.createCell(colIndex);
            //            cell.setCellStyle(cellStyle);

            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) colIndex, rowIndex,
                    (short) (colIndex + gridCell.getColSpan()),
                    rowIndex + (isCollapseRowSpan ? 1 : gridCell.getRowSpan()));
            anchor.setAnchorType(2);
            //pngEncoder.setImage(bi);
            //int imgIndex = workbook.addPicture(pngEncoder.pngEncode(), HSSFWorkbook.PICTURE_TYPE_PNG);
            int imgIndex = workbook.addPicture(
                    JRImageLoader.loadImageDataFromAWTImage(bi, JRRenderable.IMAGE_TYPE_PNG),
                    HSSFWorkbook.PICTURE_TYPE_PNG);
            patriarch.createPicture(anchor, imgIndex);
        }
    } catch (Exception ex) {
        throw new JRException("The cell cannot be added", ex);
    } catch (Error err) {
        throw new JRException("The cell cannot be added", err);
    }
}

From source file:gov.nih.nci.cananolab.util.ExportUtils.java

License:BSD License

/**
 * Output Datums in Characterization Results for work sheet.
 *
 * @param rowIndex//from w w  w  .j av a  2s  . c  o m
 * @param filePath
 * @param wb
 * @param sheet
 */
public static int createImage(int rowIndex, short colIndex, String filePath, HSSFWorkbook wb, HSSFSheet sheet,
        HSSFPatriarch patriarch) throws IOException {
    short topLeftCell = colIndex;
    short bottomRightCell = (short) (colIndex + 7);
    int topLeftRow = rowIndex + 1;
    int bottomRightRow = rowIndex + 22;
    HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 255, topLeftCell, topLeftRow, bottomRightCell,
            bottomRightRow);
    anchor.setAnchorType(2); // 2 = Move but don't size with cells
    patriarch.createPicture(anchor, loadPicture(filePath, wb));
    rowIndex = bottomRightRow + 3;

    return rowIndex;
}

From source file:net.sf.jasperreports.engine.export.JRXlsExporter.java

License:Open Source License

public void exportImage(JRPrintImage element, JRExporterGridCell gridCell, int colIndex, int rowIndex,
        int emptyCols, int yCutsRow, JRGridLayout layout) throws JRException {
    try {/*from   w  ww .j  a  v  a2 s .c o  m*/
        int topPadding = Math.max(element.getLineBox().getTopPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getTopPen()));
        int leftPadding = Math.max(element.getLineBox().getLeftPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getLeftPen()));
        int bottomPadding = Math.max(element.getLineBox().getBottomPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getBottomPen()));
        int rightPadding = Math.max(element.getLineBox().getRightPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getRightPen()));

        //pngEncoder.setImage( null );

        int availableImageWidth = element.getWidth() - leftPadding - rightPadding;
        availableImageWidth = availableImageWidth < 0 ? 0 : availableImageWidth;

        int availableImageHeight = element.getHeight() - topPadding - bottomPadding;
        availableImageHeight = availableImageHeight < 0 ? 0 : availableImageHeight;

        Renderable renderer = element.getRenderable();

        if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
            if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE) {
                // Image renderers are all asked for their image data and dimension at some point.
                // Better to test and replace the renderer now, in case of lazy load error.
                renderer = RenderableUtil.getInstance(jasperReportsContext)
                        .getOnErrorRendererForImageData(renderer, element.getOnErrorTypeValue());
                if (renderer != null) {
                    renderer = RenderableUtil.getInstance(jasperReportsContext)
                            .getOnErrorRendererForDimension(renderer, element.getOnErrorTypeValue());
                }
            } else {
                renderer = new JRWrappingSvgRenderer(renderer,
                        new Dimension(element.getWidth(), element.getHeight()),
                        ModeEnum.OPAQUE == element.getModeValue() ? element.getBackcolor() : null);
            }
        } else {
            renderer = null;
        }

        if (renderer != null) {
            int normalWidth = availableImageWidth;
            int normalHeight = availableImageHeight;

            Dimension2D dimension = renderer.getDimension(jasperReportsContext);
            if (dimension != null) {
                normalWidth = (int) dimension.getWidth();
                normalHeight = (int) dimension.getHeight();
            }

            float xalignFactor = 0f;
            switch (element.getHorizontalAlignmentValue()) {
            case RIGHT: {
                xalignFactor = 1f;
                break;
            }
            case CENTER: {
                xalignFactor = 0.5f;
                break;
            }
            case LEFT:
            default: {
                xalignFactor = 0f;
                break;
            }
            }

            float yalignFactor = 0f;
            switch (element.getVerticalAlignmentValue()) {
            case BOTTOM: {
                yalignFactor = 1f;
                break;
            }
            case MIDDLE: {
                yalignFactor = 0.5f;
                break;
            }
            case TOP:
            default: {
                yalignFactor = 0f;
                break;
            }
            }

            byte[] imageData = null;
            int topOffset = 0;
            int leftOffset = 0;
            int bottomOffset = 0;
            int rightOffset = 0;

            switch (element.getScaleImageValue()) {
            case CLIP: {
                int dpi = getPropertiesUtil().getIntegerProperty(Renderable.PROPERTY_IMAGE_DPI, 72);
                double scale = dpi / 72d;

                BufferedImage bi = new BufferedImage((int) (scale * availableImageWidth),
                        (int) (scale * availableImageHeight), BufferedImage.TYPE_INT_ARGB);

                Graphics2D grx = bi.createGraphics();
                grx.scale(scale, scale);
                grx.clip(new Rectangle(0, 0, availableImageWidth, availableImageHeight));

                renderer.render(jasperReportsContext, grx,
                        new Rectangle((int) (xalignFactor * (availableImageWidth - normalWidth)),
                                (int) (yalignFactor * (availableImageHeight - normalHeight)), normalWidth,
                                normalHeight));

                topOffset = topPadding;
                leftOffset = leftPadding;
                bottomOffset = bottomPadding;
                rightOffset = rightPadding;

                imageData = JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(bi,
                        ImageTypeEnum.PNG);

                break;
            }
            case FILL_FRAME: {
                topOffset = topPadding;
                leftOffset = leftPadding;
                bottomOffset = bottomPadding;
                rightOffset = rightPadding;

                imageData = renderer.getImageData(jasperReportsContext);

                break;
            }
            case RETAIN_SHAPE:
            default: {
                if (element.getHeight() > 0) {
                    double ratio = (double) normalWidth / (double) normalHeight;

                    if (ratio > (double) availableImageWidth / (double) availableImageHeight) {
                        normalWidth = availableImageWidth;
                        normalHeight = (int) (availableImageWidth / ratio);
                    } else {
                        normalWidth = (int) (availableImageHeight * ratio);
                        normalHeight = availableImageHeight;
                    }

                    topOffset = topPadding + (int) (yalignFactor * (availableImageHeight - normalHeight));
                    leftOffset = leftPadding + (int) (xalignFactor * (availableImageWidth - normalWidth));
                    bottomOffset = bottomPadding
                            + (int) ((1f - yalignFactor) * (availableImageHeight - normalHeight));
                    rightOffset = rightPadding
                            + (int) ((1f - xalignFactor) * (availableImageWidth - normalWidth));

                    imageData = renderer.getImageData(jasperReportsContext);
                }

                break;
            }
            }

            XlsReportConfiguration configuration = getCurrentItemConfiguration();

            short mode = backgroundMode;
            short backcolor = whiteIndex;
            if (!configuration.isIgnoreCellBackground() && gridCell.getCellBackcolor() != null) {
                mode = HSSFCellStyle.SOLID_FOREGROUND;
                backcolor = getWorkbookColor(gridCell.getCellBackcolor()).getIndex();
            }

            short forecolor = getWorkbookColor(element.getLineBox().getPen().getLineColor()).getIndex();

            if (element.getModeValue() == ModeEnum.OPAQUE) {
                backcolor = getWorkbookColor(element.getBackcolor()).getIndex();
            }

            HSSFCellStyle cellStyle = getLoadedCellStyle(mode, backcolor, HSSFCellStyle.ALIGN_LEFT,
                    HSSFCellStyle.VERTICAL_TOP, (short) 0,
                    getLoadedFont(getDefaultFont(), forecolor, null, getLocale()), gridCell,
                    isCellLocked(element), isCellHidden(element));

            createMergeRegion(gridCell, colIndex, rowIndex, cellStyle);

            cell = row.createCell(colIndex);
            cell.setCellStyle(cellStyle);

            double topPos = getRowRelativePosition(layout, yCutsRow, topOffset);
            double leftPos = getColumnRelativePosition(layout, colIndex, leftOffset);
            double bottomPos = getRowRelativePosition(layout, yCutsRow, element.getHeight() - bottomOffset);
            double rightPos = getColumnRelativePosition(layout, colIndex, element.getWidth() - rightOffset);
            HSSFClientAnchor anchor = new HSSFClientAnchor((int) ((leftPos - (int) leftPos) * 1023), //numbers taken from POI source code
                    (int) ((topPos - (int) topPos) * 255), (int) ((rightPos - (int) rightPos) * 1023),
                    (int) ((bottomPos - (int) bottomPos) * 255), (short) (colIndex + (int) leftPos),
                    (short) (rowIndex + (int) topPos),
                    //(short) (colIndex + gridCell.getColSpan()), 
                    (short) (colIndex + (int) rightPos),
                    //rowIndex + (isCollapseRowSpan ? 1 : gridCell.getRowSpan())
                    (short) (rowIndex + (int) bottomPos));

            ImageAnchorTypeEnum imageAnchorType = ImageAnchorTypeEnum.getByName(JRPropertiesUtil
                    .getOwnProperty(element, XlsReportConfiguration.PROPERTY_IMAGE_ANCHOR_TYPE));
            if (imageAnchorType == null) {
                imageAnchorType = configuration.getImageAnchorType();
                if (imageAnchorType == null) {
                    imageAnchorType = ImageAnchorTypeEnum.MOVE_NO_SIZE;
                }
            }
            anchor.setAnchorType(imageAnchorType.getValue());
            //pngEncoder.setImage(bi);
            //int imgIndex = workbook.addPicture(pngEncoder.pngEncode(), HSSFWorkbook.PICTURE_TYPE_PNG);
            int imgIndex = workbook.addPicture(imageData, HSSFWorkbook.PICTURE_TYPE_PNG);
            patriarch.createPicture(anchor, imgIndex);

            //            setHyperlinkCell(element);
        }
    } catch (Exception ex) {
        throw new JRException("The cell cannot be added", ex);
    } catch (Error err) {
        throw new JRException("The cell cannot be added", err);
    }
}

From source file:net.sf.jasperreports.engine.export.JRXlsMetadataExporter.java

License:Open Source License

protected void writeCurrentRow(Map<String, Object> currentRow, Map<String, Object> repeatedValues)
        throws JRException {
    row = sheet.createRow(sheet.getPhysicalNumberOfRows());
    setRowHeight(row);//  w ww.  j ava 2  s.c om

    for (int i = 0; i < columnNames.size(); i++) {
        String columnName = columnNames.get(i);
        CellSettings cellSettings = (CellSettings) currentRow.get(columnName) == null
                ? (repeatedValues.get(columnName) != null ? (CellSettings) repeatedValues.get(columnName)
                        : null)
                : (CellSettings) currentRow.get(columnName);
        cell = row.createCell(i);
        if (cellSettings != null) {
            int type = cellSettings.getCellType();
            cell.setCellType(type);
            Object cellValue = cellSettings.getCellValue();
            if (cellValue != null) {
                if (cellValue instanceof RichTextString) {
                    cell.setCellValue((RichTextString) cellSettings.getCellValue());
                } else if (cellValue instanceof Number) {
                    cell.setCellValue(((Number) cellSettings.getCellValue()).doubleValue());
                } else if (cellValue instanceof Date) {
                    cell.setCellValue((Date) cellSettings.getCellValue());
                } else if (cellValue instanceof Boolean) {
                    cell.setCellValue((Boolean) cellSettings.getCellValue());
                } else if (cellValue instanceof ImageSettings) {
                    ImageSettings imageSettings = (ImageSettings) cellValue;
                    try {
                        HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) (i), rowIndex,
                                (short) (i + 1), rowIndex + 1);
                        anchor.setAnchorType(imageSettings.getAnchorType());
                        patriarch.createPicture(anchor, imageSettings.getIndex());
                    } catch (Exception ex) {
                        throw new JRException("The cell cannot be added", ex);
                    } catch (Error err) {
                        throw new JRException("The cell cannot be added", err);
                    }
                }
            }

            if (cellSettings.getCellStyle() != null) {
                cell.setCellStyle(cellSettings.getCellStyle());
            }
            if (cellSettings.getFormula() != null) {
                cell.setCellFormula(cellSettings.getFormula());
            }
            if (cellSettings.getLink() != null) {
                cell.setHyperlink(cellSettings.getLink());
            }
        }
    }
    ++rowIndex;
}

From source file:net.sf.jasperreports.engine.export.JRXlsMetadataExporter.java

License:Open Source License

public void exportImage(JRPrintImage element) throws JRException {

    String currentColumnName = element.getPropertiesMap()
            .getProperty(JRXlsAbstractMetadataExporter.PROPERTY_COLUMN_NAME);
    if (currentColumnName != null && currentColumnName.length() > 0) {
        boolean repeatValue = getPropertiesUtil().getBooleanProperty(element,
                JRXlsAbstractMetadataExporter.PROPERTY_REPEAT_VALUE, false);

        setColumnName(currentColumnName);
        adjustColumnWidth(currentColumnName, element.getWidth(),
                ((JRXlsExporterNature) nature).getColumnAutoFit(element));
        adjustRowHeight(element.getHeight(),
                Boolean.TRUE.equals(((JRXlsExporterNature) nature).getRowAutoFit(element)));

        int topPadding = Math.max(element.getLineBox().getTopPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getTopPen()));
        int leftPadding = Math.max(element.getLineBox().getLeftPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getLeftPen()));
        int bottomPadding = Math.max(element.getLineBox().getBottomPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getBottomPen()));
        int rightPadding = Math.max(element.getLineBox().getRightPadding().intValue(),
                getImageBorderCorrection(element.getLineBox().getRightPen()));

        //pngEncoder.setImage( null );

        int availableImageWidth = element.getWidth() - leftPadding - rightPadding;
        availableImageWidth = availableImageWidth < 0 ? 0 : availableImageWidth;

        int availableImageHeight = element.getHeight() - topPadding - bottomPadding;
        availableImageHeight = availableImageHeight < 0 ? 0 : availableImageHeight;

        Renderable renderer = element.getRenderable();

        if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {

            if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE) {
                // Image renderers are all asked for their image data and dimension at some point.
                // Better to test and replace the renderer now, in case of lazy load error.
                renderer = RenderableUtil.getInstance(jasperReportsContext)
                        .getOnErrorRendererForImageData(renderer, element.getOnErrorTypeValue());
                if (renderer != null) {
                    renderer = RenderableUtil.getInstance(jasperReportsContext)
                            .getOnErrorRendererForDimension(renderer, element.getOnErrorTypeValue());
                }//  ww w.  j  av  a 2  s.c  o  m
            } else {
                renderer = new JRWrappingSvgRenderer(renderer,
                        new Dimension(element.getWidth(), element.getHeight()),
                        ModeEnum.OPAQUE == element.getModeValue() ? element.getBackcolor() : null);
            }
        } else {
            renderer = null;
        }

        if (renderer != null) {
            int normalWidth = availableImageWidth;
            int normalHeight = availableImageHeight;

            Dimension2D dimension = renderer.getDimension(jasperReportsContext);
            if (dimension != null) {
                normalWidth = (int) dimension.getWidth();
                normalHeight = (int) dimension.getHeight();
            }

            float xalignFactor = 0f;
            switch (element.getHorizontalAlignmentValue()) {
            case RIGHT: {
                xalignFactor = 1f;
                break;
            }
            case CENTER: {
                xalignFactor = 0.5f;
                break;
            }
            case LEFT:
            default: {
                xalignFactor = 0f;
                break;
            }
            }

            float yalignFactor = 0f;
            switch (element.getVerticalAlignmentValue()) {
            case BOTTOM: {
                yalignFactor = 1f;
                break;
            }
            case MIDDLE: {
                yalignFactor = 0.5f;
                break;
            }
            case TOP:
            default: {
                yalignFactor = 0f;
                break;
            }
            }

            byte[] imageData = null;
            //            int topOffset = 0;
            //            int leftOffset = 0;
            //            int bottomOffset = 0;
            //            int rightOffset = 0;
            //            
            switch (element.getScaleImageValue()) {
            case CLIP: {
                int dpi = getPropertiesUtil().getIntegerProperty(Renderable.PROPERTY_IMAGE_DPI, 72);
                double scale = dpi / 72d;

                BufferedImage bi = new BufferedImage((int) (scale * availableImageWidth),
                        (int) (scale * availableImageHeight), BufferedImage.TYPE_INT_ARGB);

                Graphics2D grx = bi.createGraphics();
                grx.scale(scale, scale);
                grx.clip(new Rectangle(0, 0, availableImageWidth, availableImageHeight));

                renderer.render(jasperReportsContext, grx,
                        new Rectangle((int) (xalignFactor * (availableImageWidth - normalWidth)),
                                (int) (yalignFactor * (availableImageHeight - normalHeight)), normalWidth,
                                normalHeight));

                //                  topOffset = topPadding;
                //                  leftOffset = leftPadding;
                //                  bottomOffset = bottomPadding;
                //                  rightOffset = rightPadding;

                imageData = JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(bi,
                        ImageTypeEnum.PNG);

                break;
            }
            case FILL_FRAME: {
                //                  topOffset = topPadding;
                //                  leftOffset = leftPadding;
                //                  bottomOffset = bottomPadding;
                //                  rightOffset = rightPadding;

                imageData = renderer.getImageData(jasperReportsContext);

                break;
            }
            case RETAIN_SHAPE:
            default: {
                if (element.getHeight() > 0) {
                    double ratio = (double) normalWidth / (double) normalHeight;

                    if (ratio > (double) availableImageWidth / (double) availableImageHeight) {
                        normalWidth = availableImageWidth;
                        normalHeight = (int) (availableImageWidth / ratio);
                    } else {
                        normalWidth = (int) (availableImageHeight * ratio);
                        normalHeight = availableImageHeight;
                    }

                    //                     topOffset = topPadding + (int) (yalignFactor * (availableImageHeight - normalHeight));
                    //                     leftOffset = leftPadding + (int) (xalignFactor * (availableImageWidth - normalWidth));
                    //                     bottomOffset = bottomPadding + (int) ((1f - yalignFactor) * (availableImageHeight - normalHeight));
                    //                     rightOffset = rightPadding + (int) ((1f - xalignFactor) * (availableImageWidth - normalWidth));

                    imageData = renderer.getImageData(jasperReportsContext);
                }
                break;
            }
            }

            XlsMetadataReportConfiguration configuration = getCurrentItemConfiguration();

            short mode = backgroundMode;
            short backcolor = whiteIndex;
            if (!configuration.isIgnoreCellBackground() && element.getBackcolor() != null) {
                mode = HSSFCellStyle.SOLID_FOREGROUND;
                backcolor = getWorkbookColor(element.getBackcolor()).getIndex();
            }

            short forecolor = getWorkbookColor(element.getLineBox().getPen().getLineColor()).getIndex();

            if (element.getModeValue() == ModeEnum.OPAQUE) {
                backcolor = getWorkbookColor(element.getBackcolor()).getIndex();
            }

            HSSFCellStyle cellStyle = getLoadedCellStyle(mode, backcolor, HSSFCellStyle.ALIGN_LEFT,
                    HSSFCellStyle.VERTICAL_TOP, (short) 0,
                    getLoadedFont(getDefaultFont(), forecolor, null, getLocale()), new BoxStyle(element),
                    isCellLocked(element), isCellHidden(element));

            addBlankElement(cellStyle, false, currentColumnName);

            int colIndex = columnNamesMap.get(currentColumnName);
            try {
                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) (colIndex), rowIndex,
                        (short) (colIndex + 1), rowIndex + 1);
                ImageAnchorTypeEnum imageAnchorType = ImageAnchorTypeEnum.getByName(JRPropertiesUtil
                        .getOwnProperty(element, XlsReportConfiguration.PROPERTY_IMAGE_ANCHOR_TYPE));
                if (imageAnchorType == null) {
                    imageAnchorType = configuration.getImageAnchorType();
                    if (imageAnchorType == null) {
                        imageAnchorType = ImageAnchorTypeEnum.MOVE_NO_SIZE;
                    }
                }
                anchor.setAnchorType(imageAnchorType.getValue());
                //pngEncoder.setImage(bi);
                //int imgIndex = workbook.addPicture(pngEncoder.pngEncode(), HSSFWorkbook.PICTURE_TYPE_PNG);
                int imgIndex = workbook.addPicture(imageData, HSSFWorkbook.PICTURE_TYPE_PNG);
                patriarch.createPicture(anchor, imgIndex);

                // set auto fill columns
                if (repeatValue) {
                    CellSettings cellSettings = new CellSettings(cellStyle);
                    cellSettings.setCellValue(new ImageSettings(imgIndex, anchor.getAnchorType()));
                    addCell(cellSettings, repeatedValues, currentColumnName);
                } else {
                    repeatedValues.remove(currentColumnName);
                }

                //               setHyperlinkCell(element);
            } catch (Exception ex) {
                throw new JRException("The cell cannot be added", ex);
            } catch (Error err) {
                throw new JRException("The cell cannot be added", err);
            }
        }
    }
}

From source file:org.jxstar.report.util.ReportXlsUtil.java

/**
 * ??POI?ReportImageUtil/*from w w w  . ja  v a2  s .c  om*/
 * @param cell -- ?
 * @param bytes -- 
 */
public static void addImageToSheet(HSSFCell cell, byte[] bytes) {
    if (cell == null) {
        _log.showError("-----insertImageToSheet: cell is null!");
        return;
    }
    if (bytes == null || bytes.length == 0) {
        _log.showError("-----insertImageToSheet: bytes is null!");
        return;
    }

    //??
    HSSFSheet sheet = cell.getSheet();

    //?
    int firstRow = cell.getRowIndex();
    int lastRow = cell.getRowIndex();
    int firstCol = cell.getColumnIndex();
    int lastCol = cell.getColumnIndex();
    //??
    CellRangeAddress range = getMergedRegion(cell);
    if (range != null) {
        firstRow = range.getFirstRow();
        lastRow = range.getLastRow();
        firstCol = range.getFirstColumn();
        lastCol = range.getLastColumn();
    }
    _log.showDebug("---------image cells=[" + firstRow + "," + firstCol + "," + lastRow + "," + lastCol + "]");
    //????5??1023255
    HSSFClientAnchor anchor = new HSSFClientAnchor(10, 5, 1013, 250, (short) firstCol, firstRow,
            (short) lastCol, lastRow);
    anchor.setAnchorType(HSSFClientAnchor.MOVE_AND_RESIZE);

    //??
    HSSFPatriarch draw = sheet.getDrawingPatriarch();
    if (draw == null) {
        draw = sheet.createDrawingPatriarch();
    }

    //???
    sheet.getWorkbook().addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG);

    //???????+1??
    List<HSSFPicture> lsPicture = getAllPicture(sheet);
    int index = lsPicture.size() + 1;
    _log.showDebug("---------new image index=" + index);

    draw.createPicture(anchor, index);
}

From source file:org.openmicroscopy.shoola.util.file.ExcelWriter.java

License:Open Source License

/**
 * Writes the image with name imageName to the work sheet, at the 
 * specified location (rowStart, colStart)->(rowEnd, colEnd).
 * Returns the (colEnd, rowEnd).//from  w  w  w  .  j  ava  2  s  .  c o m
 * 
 * @param rowStartIndex The index of the start row.
 * @param colStartIndex The index of the start column.
 * @param width       The width of the image.
 * @param height       The height of the image.
 * @param imageName    The name of the image.
 */
public Point writeImage(int rowStartIndex, int colStartIndex, int width, int height, String imageName) {
    HSSFPatriarch patriarch = currentSheet.getDrawingPatriarch();
    HSSFClientAnchor anchor;
    double remainderWidth = (width % 68) / 68.0;
    int widthInCells = width / 68;
    double remainderHeight = (height % 18) / 18.0;
    int heightInCells = (height / 18);
    int rowEnd = rowStartIndex + heightInCells;
    int colEnd = colStartIndex + widthInCells;
    anchor = new HSSFClientAnchor(0, 0, (int) (remainderWidth * 1023), (int) (remainderHeight * 255),
            (short) colStartIndex, (short) rowStartIndex, (short) colEnd, (short) rowEnd);

    anchor.setAnchorType(3);
    int index = imageMap.get(imageName);
    patriarch.createPicture(anchor, index);
    return new Point(colEnd, rowEnd);
}

From source file:org.seasar.fisshplate.core.element.Picture.java

License:Apache License

/**
 * ???//  w ww.  j a va 2s.  c  om
 *
 * @param width
 * @param height
 * @param cellNo
 * @param rowNo
 * @param rowRangeIntVal
 * @param cellRangeIntVal
 * @return
 */
private ClientAnchor createAnchor(int width, int height, int cellNo, int rowNo, int cellRangeIntVal,
        int rowRangeIntVal) {
    HSSFClientAnchor anchor = new HSSFClientAnchor();
    // TODO ????????
    anchor.setDx1(0);
    anchor.setDx2(0);
    anchor.setDy1(0);
    anchor.setDy2(255);

    int fromCellNo = cellNo;
    int toCellNo = cellNo + cellRangeIntVal;
    int fromRowNo = rowNo;
    int toRowNo = rowNo + rowRangeIntVal;

    anchor.setCol1((short) fromCellNo);
    anchor.setCol2((short) toCellNo);
    anchor.setRow1(fromRowNo);
    anchor.setRow2(toRowNo);
    anchor.setAnchorType(2);
    return anchor;
}