Java Code Examples for org.apache.poi.ss.usermodel.ClientAnchor#setCol2()

The following examples show how to use org.apache.poi.ss.usermodel.ClientAnchor#setCol2() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractExcelWriteExecutor.java    From easyexcel with Apache License 2.0 7 votes vote down vote up
private void setImageValue(CellData cellData, Cell cell) {
    Sheet sheet = cell.getSheet();
    int index = sheet.getWorkbook().addPicture(cellData.getImageValue(), HSSFWorkbook.PICTURE_TYPE_PNG);
    Drawing drawing = sheet.getDrawingPatriarch();
    if (drawing == null) {
        drawing = sheet.createDrawingPatriarch();
    }
    CreationHelper helper = sheet.getWorkbook().getCreationHelper();
    ClientAnchor anchor = helper.createClientAnchor();
    anchor.setDx1(0);
    anchor.setDx2(0);
    anchor.setDy1(0);
    anchor.setDy2(0);
    anchor.setCol1(cell.getColumnIndex());
    anchor.setCol2(cell.getColumnIndex() + 1);
    anchor.setRow1(cell.getRowIndex());
    anchor.setRow2(cell.getRowIndex() + 1);
    anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
    drawing.createPicture(anchor, index);
}
 
Example 2
Source File: AbstractInliner.java    From yarg with Apache License 2.0 5 votes vote down vote up
@Override
public void inlineToXls(HSSFPatriarch patriarch, HSSFCell resultCell, Object paramValue, Matcher paramsMatcher) {
    try {
        Image image = new Image(paramValue, paramsMatcher);
        if (image.isValid()) {
            HSSFSheet sheet = resultCell.getSheet();
            HSSFWorkbook workbook = sheet.getWorkbook();

            int pictureIdx = workbook.addPicture(image.imageContent, Workbook.PICTURE_TYPE_JPEG);

            CreationHelper helper = workbook.getCreationHelper();
            ClientAnchor anchor = helper.createClientAnchor();
            anchor.setCol1(resultCell.getColumnIndex());
            anchor.setRow1(resultCell.getRowIndex());
            anchor.setCol2(resultCell.getColumnIndex());
            anchor.setRow2(resultCell.getRowIndex());
            if (patriarch == null) {
                throw new IllegalArgumentException(String.format("No HSSFPatriarch object provided. Charts on this sheet could cause this effect. Please check sheet %s", resultCell.getSheet().getSheetName()));
            }
            HSSFPicture picture = patriarch.createPicture(anchor, pictureIdx);
            Dimension size = ImageUtils.getDimensionFromAnchor(picture);
            double actualHeight = size.getHeight() / EMU_PER_PIXEL;
            double actualWidth = size.getWidth() / EMU_PER_PIXEL;
            picture.resize((double) image.width / actualWidth, (double) image.height / actualHeight);
        }
    } catch (IllegalArgumentException e) {
        throw new ReportFormattingException("An error occurred while inserting bitmap to xls file", e);
    }
}
 
Example 3
Source File: ExcelImageHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected ClientAnchor computeExcel97ClientAnchor( final SlimSheetLayout currentLayout,
    final TableRectangle rectangle, final StrictBounds cb ) {
  final int cell1x = rectangle.getX1();
  final int cell1y = rectangle.getY1();
  final int cell2x = Math.max( cell1x, rectangle.getX2() - 1 );
  final int cell2y = Math.max( cell1y, rectangle.getY2() - 1 );

  final long cell1width = currentLayout.getCellWidth( cell1x );
  final long cell1height = currentLayout.getRowHeight( cell1y );
  final long cell2width = currentLayout.getCellWidth( cell2x );
  final long cell2height = currentLayout.getRowHeight( cell2y );

  final long cell1xPos = currentLayout.getXPosition( cell1x );
  final long cell1yPos = currentLayout.getYPosition( cell1y );
  final long cell2xPos = currentLayout.getXPosition( cell2x );
  final long cell2yPos = currentLayout.getYPosition( cell2y );

  final int dx1 = (int) ( 1023 * ( ( cb.getX() - cell1xPos ) / (double) cell1width ) );
  final int dy1 = (int) ( 255 * ( ( cb.getY() - cell1yPos ) / (double) cell1height ) );
  final int dx2 = (int) ( 1023 * ( ( cb.getX() + cb.getWidth() - cell2xPos ) / (double) cell2width ) );
  final int dy2 = (int) ( 255 * ( ( cb.getY() + cb.getHeight() - cell2yPos ) / (double) cell2height ) );

  final ClientAnchor anchor = printerBase.getWorkbook().getCreationHelper().createClientAnchor();
  anchor.setDx1( dx1 );
  anchor.setDy1( dy1 );
  anchor.setDx2( dx2 );
  anchor.setDy2( dy2 );
  anchor.setCol1( cell1x );
  anchor.setRow1( cell1y );
  anchor.setCol2( cell2x );
  anchor.setRow2( cell2y );
  anchor.setAnchorType( ClientAnchor.AnchorType.MOVE_DONT_RESIZE );
  return anchor;
}
 
Example 4
Source File: ExcelImageHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected ClientAnchor computeExcel2003ClientAnchor( final SlimSheetLayout currentLayout,
    final TableRectangle rectangle, final StrictBounds cb ) {
  final int cell1x = rectangle.getX1();
  final int cell1y = rectangle.getY1();
  final int cell2x = Math.max( cell1x, rectangle.getX2() - 1 );
  final int cell2y = Math.max( cell1y, rectangle.getY2() - 1 );

  final long cell1xPos = currentLayout.getXPosition( cell1x );
  final long cell1yPos = currentLayout.getYPosition( cell1y );
  final long cell2xPos = currentLayout.getXPosition( cell2x );
  final long cell2yPos = currentLayout.getYPosition( cell2y );

  final int dx1 = (int) StrictGeomUtility.toExternalValue( ( cb.getX() - cell1xPos ) * XSSFShape.EMU_PER_POINT );
  final int dy1 = (int) StrictGeomUtility.toExternalValue( ( cb.getY() - cell1yPos ) * XSSFShape.EMU_PER_POINT );
  final int dx2 =
      (int) Math.max( 0, StrictGeomUtility.toExternalValue( ( cb.getX() + cb.getWidth() - cell2xPos )
          * XSSFShape.EMU_PER_POINT ) );
  final int dy2 =
      (int) Math.max( 0, StrictGeomUtility.toExternalValue( ( cb.getY() + cb.getHeight() - cell2yPos )
          * XSSFShape.EMU_PER_POINT ) );

  final ClientAnchor anchor = printerBase.getWorkbook().getCreationHelper().createClientAnchor();
  anchor.setDx1( dx1 );
  anchor.setDy1( dy1 );
  anchor.setDx2( dx2 );
  anchor.setDy2( dy2 );
  anchor.setCol1( cell1x );
  anchor.setRow1( cell1y );
  anchor.setCol2( cell2x );
  anchor.setRow2( cell2y );
  anchor.setAnchorType( ClientAnchor.AnchorType.MOVE_DONT_RESIZE );
  return anchor;
}
 
Example 5
Source File: PageHandler.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * <p>
 * Process a CellImage from the images list and place the image on the sheet.
 * </p><p>
 * This involves changing the row height as necesssary and determining the column spread of the image.
 * </p>
 * @param cellImage
 * The image to be placed on the sheet.
 */
private void processCellImage( HandlerState state, Drawing drawing, CellImage cellImage ) {
	Coordinate location = cellImage.location;
	
	Cell cell = state.currentSheet.getRow( location.getRow() ).getCell( location.getCol() );

	IImageContent image = cellImage.image;		
	
	StyleManagerUtils smu = state.getSmu();
	float ptHeight = cell.getRow().getHeightInPoints();
	if( image.getHeight() != null ) {
		ptHeight = smu.fontSizeInPoints( image.getHeight().toString() );
	}

	// Get image width
	int endCol = cell.getColumnIndex();
       double lastColWidth = ClientAnchorConversions.widthUnits2Millimetres( (short)state.currentSheet.getColumnWidth( endCol ) )
       		+ 2.0;
       int dx = smu.anchorDxFromMM( lastColWidth, lastColWidth );
       double mmWidth = 0.0;
       if( smu.isAbsolute(image.getWidth())) {
           mmWidth = image.getWidth().convertTo(DimensionType.UNITS_MM);
       } else if(smu.isPixels(image.getWidth())) {
           mmWidth = ClientAnchorConversions.pixels2Millimetres( image.getWidth().getMeasure() );
       }
	// Allow image to span multiple columns
	CellRangeAddress mergedRegion = getMergedRegionBegunBy( state.currentSheet, location.getRow(), location.getCol() );
	if( (cellImage.spanColumns) || ( mergedRegion != null ) ) {
        log.debug( "Image size: ", image.getWidth(), " translates as mmWidth = ", mmWidth );
        if( mmWidth > 0) {
            double mmAccumulatedWidth = 0;
            int endColLimit = cellImage.spanColumns ? 256 : mergedRegion.getLastColumn();
            for( endCol = cell.getColumnIndex(); mmAccumulatedWidth < mmWidth && endCol < endColLimit; ++ endCol ) {
                lastColWidth = ClientAnchorConversions.widthUnits2Millimetres( (short)state.currentSheet.getColumnWidth( endCol ) )
                		+ 2.0;
                mmAccumulatedWidth += lastColWidth;
                log.debug( "lastColWidth = ", lastColWidth, "; mmAccumulatedWidth = ", mmAccumulatedWidth);
            }
            if( mmAccumulatedWidth > mmWidth ) {
                mmAccumulatedWidth -= lastColWidth;
                --endCol;
                double mmShort = mmWidth - mmAccumulatedWidth;
                dx = smu.anchorDxFromMM( mmShort, lastColWidth );
            }
        }
	} else {
		float widthRatio = (float)(mmWidth / lastColWidth);
		ptHeight = ptHeight / widthRatio;
	}

	int rowsSpanned = state.findRowsSpanned( cell.getRowIndex(), cell.getColumnIndex() );
	float neededRowHeightPoints = ptHeight;
	
	for( int i = 0; i < rowsSpanned; ++i ) {
		int rowIndex = cell.getRowIndex() + 1 + i;
		neededRowHeightPoints -= state.currentSheet.getRow(rowIndex).getHeightInPoints();
	}
	
	if( neededRowHeightPoints > cell.getRow().getHeightInPoints()) {
		cell.getRow().setHeightInPoints( neededRowHeightPoints );
	}
	
	// ClientAnchor anchor = wb.getCreationHelper().createClientAnchor();
	ClientAnchor anchor = state.getWb().getCreationHelper().createClientAnchor();
       anchor.setCol1(cell.getColumnIndex());
       anchor.setRow1(cell.getRowIndex());
       anchor.setCol2(endCol);
       anchor.setRow2(cell.getRowIndex() + rowsSpanned);
       anchor.setDx2(dx);
       anchor.setDy2( smu.anchorDyFromPoints( ptHeight, cell.getRow().getHeightInPoints() ) );
       anchor.setAnchorType(ClientAnchor.MOVE_DONT_RESIZE);
    drawing.createPicture(anchor, cellImage.imageIdx);
}