org.apache.poi.ss.usermodel.Picture Java Examples

The following examples show how to use org.apache.poi.ss.usermodel.Picture. 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: ImageListener.java    From hy.common.report with Apache License 2.0 6 votes vote down vote up
/**
 * 自动缩放
 * 
 * @author      ZhengWei(HY)
 * @createDate  2019-05-30
 * @version     v1.0
 *
 * @param i_Picture
 * @param i_Image
 */
protected void autoScale(Picture i_Picture ,BufferedImage i_Image)
{
    double v_Scale = 1D;
    
    if ( i_Image.getWidth() > i_Image.getHeight() )
    {
        double v_ScaleY = Help.division(i_Image.getHeight() ,i_Image.getWidth());
        i_Picture.resize(v_Scale ,v_ScaleY);
        // i_Picture.resize(v_Scale ,v_Scale);
    }
    else if ( i_Image.getWidth() < i_Image.getHeight() )
    {
        double v_ScaleX = Help.division(i_Image.getWidth() ,i_Image.getHeight());
        i_Picture.resize(v_ScaleX ,v_Scale);
        // i_Picture.resize(0.5D ,v_Scale);
    }
    else
    {
        i_Picture.resize(v_Scale ,v_Scale);
    }
}
 
Example #2
Source File: ImageListener.java    From hy.common.report with Apache License 2.0 6 votes vote down vote up
/**
 * 重置图片大小,设置顶部、左侧边距
 * 
 * @author      ZhengWei(HY)
 * @createDate  2017-10-31
 * @version     v1.0
 *
 * @param i_Picture
 */
protected void resizeMarginLeftTop(Picture i_Picture ,BufferedImage i_Image ,boolean i_IsScale ,Double i_ScaleX ,Double i_ScaleY)
{
    if ( i_ScaleX != null && i_ScaleY != null )
    {
        i_Picture.resize(i_ScaleX ,i_ScaleY);
    }
    else if ( i_ScaleX != null )
    {
        i_Picture.resize(i_ScaleX ,1);
    }
    else if ( i_ScaleY != null )
    {
        i_Picture.resize(1 ,i_ScaleY);
    }
    else if ( i_Image != null )
    {
        autoScale(i_Picture ,i_Image);
    }
    
    i_Picture.getAnchor().setDx1(i_Picture.getAnchor().getDx1() + Help.NVL(this.marginLeft ,0));
    i_Picture.getAnchor().setDx2(i_Picture.getAnchor().getDx2() + Help.NVL(this.marginLeft ,0));
    i_Picture.getAnchor().setDy1(i_Picture.getAnchor().getDy1() + Help.NVL(this.marginTop  ,0));
    i_Picture.getAnchor().setDy2(i_Picture.getAnchor().getDy2() + Help.NVL(this.marginTop  ,0));
}
 
Example #3
Source File: SpreadsheetTab.java    From taro with MIT License 6 votes vote down vote up
public void addPicture(int row, int col, byte[] bytes, int pictureType) {
    if (drawing == null) {
        drawing = sheet.createDrawingPatriarch();
    }

    int pictureIndex = workbook.getPoiWorkbook().addPicture(bytes, pictureType);
    //add a picture shape
    ClientAnchor anchor = workbook.getPoiWorkbook().getCreationHelper().createClientAnchor();
    //set top-left corner of the picture,
    //subsequent call of Picture#resize() will operate relative to it
    anchor.setCol1(col);
    anchor.setRow1(row);

    Picture pict = drawing.createPicture(anchor, pictureIndex);
    //auto-size picture relative to its top-left corner
    pict.resize();
}
 
Example #4
Source File: WatermarkExcelTests.java    From kbase-doc with Apache License 2.0 5 votes vote down vote up
@Test
public void test2() throws IOException {
	 //create a new workbook
	XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
    String imgPath = "D:\\Xiaoi\\logo\\logo.png";
    //add picture data to this workbook.
    InputStream is = new FileInputStream(imgPath);
    byte[] bytes = IOUtils.toByteArray(is);
    int pictureIdx = wb.addPicture(bytes, XSSFWorkbook.PICTURE_TYPE_PNG);
    is.close();

    CreationHelper helper = wb.getCreationHelper();

    //create sheet
    Sheet sheet = wb.createSheet();

    // Create the drawing patriarch.  This is the top level container for all shapes. 
    Drawing drawing = sheet.createDrawingPatriarch();

    //add a picture shape
    ClientAnchor anchor = helper.createClientAnchor();
    //set top-left corner of the picture,
    //subsequent call of Picture#resize() will operate relative to it
    anchor.setCol1(3);
    anchor.setRow1(2);
    anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
    Picture pict = drawing.createPicture(anchor, pictureIdx);

    //auto-size picture relative to its top-left corner
    pict.resize();

    //save workbook
    String file = "E:\\ConvertTester\\excel\\picture.xls";
    if(wb instanceof XSSFWorkbook) file += "x";
    try (OutputStream fileOut = new FileOutputStream(file)) {
        wb.write(fileOut);
    }
}
 
Example #5
Source File: ImageUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Calculates the dimensions in EMUs for the anchor of the given picture
 *
 * @param picture the picture containing the anchor
 * @return the dimensions in EMUs
 */
public static Dimension getDimensionFromAnchor(Picture picture) {
    ClientAnchor anchor = picture.getClientAnchor();
    boolean isHSSF = (anchor instanceof HSSFClientAnchor);
    Sheet sheet = picture.getSheet();

    double w = 0;
    int col2 = anchor.getCol1();

    //space in the leftmost cell
    w = sheet.getColumnWidthInPixels(col2++);
    if (isHSSF) {
        w *= 1 - anchor.getDx1()/1024d;
    } else {
        w -= anchor.getDx1()/(double)EMU_PER_PIXEL;
    }
    
    while(col2 < anchor.getCol2()){
        w += sheet.getColumnWidthInPixels(col2++);
    }
    
    if (isHSSF) {
        w += sheet.getColumnWidthInPixels(col2) * anchor.getDx2()/1024d;
    } else {
        w += anchor.getDx2()/(double)EMU_PER_PIXEL;
    }

    double h = 0;
    int row2 = anchor.getRow1();
    
    h = getRowHeightInPixels(sheet,row2++);
    if (isHSSF) {
        h *= 1 - anchor.getDy1()/256d;
    } else {
        h -= anchor.getDy1()/(double)EMU_PER_PIXEL;
    }

    while(row2 < anchor.getRow2()){
        h += getRowHeightInPixels(sheet,row2++);
    }
    
    if (isHSSF) {
        h += getRowHeightInPixels(sheet,row2) * anchor.getDy2()/256;
    } else {
        h += anchor.getDy2()/(double)EMU_PER_PIXEL;
    }

    w *= EMU_PER_PIXEL;
    h *= EMU_PER_PIXEL;
    
    return new Dimension((int)Math.rint(w), (int)Math.rint(h));
}
 
Example #6
Source File: AbstractExcelFactory.java    From myexcel with Apache License 2.0 4 votes vote down vote up
private void setImage(Td td, Sheet sheet) {
    if (td.getFile() == null) {
        return;
    }
    try {
        if (createHelper == null) {
            createHelper = workbook.getCreationHelper();
        }
        byte[] bytes = Files.readAllBytes(td.getFile().toPath());
        String fileName = td.getFile().getName();
        int format;
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        switch (suffix) {
            case "jpg":
            case "jpeg":
                format = Workbook.PICTURE_TYPE_JPEG;
                break;
            case "png":
                format = Workbook.PICTURE_TYPE_PNG;
                break;
            case "dib":
                format = Workbook.PICTURE_TYPE_DIB;
                break;
            case "emf":
                format = Workbook.PICTURE_TYPE_EMF;
                break;
            case "pict":
                format = Workbook.PICTURE_TYPE_PICT;
                break;
            case "wmf":
                format = Workbook.PICTURE_TYPE_WMF;
                break;
            default:
                throw new IllegalArgumentException("Invalid image type");
        }
        int pictureIdx = workbook.addPicture(bytes, format);
        Drawing drawing = sheet.createDrawingPatriarch();
        ClientAnchor anchor = createHelper.createClientAnchor();
        anchor.setCol1(td.getCol());
        anchor.setRow1(td.getRow());
        Picture pict = drawing.createPicture(anchor, pictureIdx);
        pict.resize(1, 1);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #7
Source File: ImageListener.java    From hy.common.report with Apache License 2.0 2 votes vote down vote up
/**
 * 重置图片大小,设置顶部、左侧边距
 * 
 * @author      ZhengWei(HY)
 * @createDate  2017-10-31
 * @version     v1.0
 *
 * @param i_Picture
 */
protected void resizeMarginLeftTop(Picture i_Picture)
{
    this.resizeMarginLeftTop(i_Picture ,null ,false ,null ,null);
}