org.apache.poi.ss.usermodel.ClientAnchor.AnchorType Java Examples

The following examples show how to use org.apache.poi.ss.usermodel.ClientAnchor.AnchorType. 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: HSSFPicture.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Resize the image
 * <p>
 * Please note, that this method works correctly only for workbooks
 * with default font size (Arial 10pt for .xls).
 * If the default font is changed the resized image can be streched vertically or horizontally.
 * </p>
 * <p>
 * <code>resize(1.0,1.0)</code> keeps the original size,<br>
 * <code>resize(0.5,0.5)</code> resize to 50% of the original,<br>
 * <code>resize(2.0,2.0)</code> resizes to 200% of the original.<br>
 * <code>resize({@link Double#MAX_VALUE},{@link Double#MAX_VALUE})</code> resizes to the dimension of the embedded image. 
 * </p>
 *
 * @param scaleX the amount by which the image width is multiplied relative to the original width.
 * @param scaleY the amount by which the image height is multiplied relative to the original height.
 */
@Override
public void resize(double scaleX, double scaleY) {
    HSSFClientAnchor anchor = getClientAnchor();
    anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE);

    HSSFClientAnchor pref = getPreferredSize(scaleX,scaleY);

    int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());
    int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());

    anchor.setCol2((short)col2);
    // anchor.setDx1(0);
    anchor.setDx2(pref.getDx2());

    anchor.setRow2(row2);
    // anchor.setDy1(0);
    anchor.setDy2(pref.getDy2());
}
 
Example #2
Source File: XLSPrinter.java    From unitime with Apache License 2.0 6 votes vote down vote up
protected void addImageToSheet(int colNumber, int rowNumber, HSSFSheet sheet, BufferedImage image, double reqImageWidthMM, double reqImageHeightMM, int resizeBehaviour) throws IOException {
      ClientAnchorDetail colClientAnchorDetail = fitImageToColumns(sheet, colNumber, reqImageWidthMM, resizeBehaviour);
      ClientAnchorDetail rowClientAnchorDetail = fitImageToRows(sheet, rowNumber, reqImageHeightMM, resizeBehaviour);

      HSSFClientAnchor anchor = new HSSFClientAnchor(0,
                                    0,
                                    colClientAnchorDetail.getInset(),
                                    rowClientAnchorDetail.getInset(),
                                    (short)colClientAnchorDetail.getFromIndex(),
                                    rowClientAnchorDetail.getFromIndex(),
                                    (short)colClientAnchorDetail.getToIndex(),
                                    rowClientAnchorDetail.getToIndex());

      anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);
      
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", bytes);

      int index = sheet.getWorkbook().addPicture(bytes.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG);

      HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
      patriarch.createPicture(anchor, index);
  }
 
Example #3
Source File: WatermarkExcelTests.java    From kbase-doc with Apache License 2.0 5 votes vote down vote up
@Test
	public void test1() throws IOException {
		
		ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
		String imgPath = "D:\\Xiaoi\\logo\\logo.png";
		BufferedImage bufferImg = ImageIO.read(new File(imgPath));
        //这里要注意,第二个参数将会决定插入图片形式,如果是一个png的图片,背景透明,但是此处设置为jpg格式将会自动添加黑色背景
        ImageIO.write(bufferImg, "png", byteArrayOut);
        
        
        String filepath = "E:\\ConvertTester\\excel\\abcde.xlsx";
		File originFile = new File(filepath);
		InputStream in = new FileInputStream(originFile);
		XSSFWorkbook workbook = new XSSFWorkbook(in);
		XSSFSheet sheet = workbook.createSheet("testSheet");
         //画图的顶级管理器,一个sheet只能获取一个
        XSSFDrawing drawing = sheet.createDrawingPatriarch(); 
        //anchor主要用于设置图片的属性  
//        XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 1023, 255, (short) 2, 4, (short) 13, 26);
        /*
         * 参数定义:
         * 第一个参数是(x轴的开始节点);
         * 第二个参数是(是y轴的开始节点);
         * 第三个参数是(是x轴的结束节点);
         * 第四个参数是(是y轴的结束节点);
         * 第五个参数是(是从Excel的第几列开始插入图片,从0开始计数);
         * 第六个参数是(是从excel的第几行开始插入图片,从0开始计数);
         * 第七个参数是(图片宽度,共多少列);
         * 第8个参数是(图片高度,共多少行);
         */
        XSSFClientAnchor anchor = drawing.createAnchor(0, 0, Short.MAX_VALUE, Integer.MAX_VALUE, 0, 0, 10, 10);
        anchor.setAnchorType(AnchorType.DONT_MOVE_DO_RESIZE);
        //插入图片
        drawing.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));
        workbook.write(new FileOutputStream("E:\\ConvertTester\\excel\\abcd-011.xlsx"));
        workbook.close();
	}
 
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: HSSFCombobox.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected EscherContainerRecord createSpContainer() {
    EscherContainerRecord spContainer = new EscherContainerRecord();
    EscherSpRecord sp = new EscherSpRecord();
    EscherOptRecord opt = new EscherOptRecord();
    EscherClientDataRecord clientData = new EscherClientDataRecord();

    spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
    spContainer.setOptions((short) 0x000F);
    sp.setRecordId(EscherSpRecord.RECORD_ID);
    sp.setOptions((short) ((EscherAggregate.ST_HOSTCONTROL << 4) | 0x2));

    sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE);
    opt.setRecordId(EscherOptRecord.RECORD_ID);
    opt.addEscherProperty(new EscherBoolProperty(EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 17039620));
    opt.addEscherProperty(new EscherBoolProperty(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 0x00080008));
    opt.addEscherProperty(new EscherBoolProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x00080000));
    opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GROUPSHAPE__PRINT, 0x00020000));

    HSSFClientAnchor userAnchor = (HSSFClientAnchor) getAnchor();
    userAnchor.setAnchorType(AnchorType.DONT_MOVE_DO_RESIZE);
    EscherRecord anchor = userAnchor.getEscherAnchor();
    clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
    clientData.setOptions((short) 0x0000);

    spContainer.addChildRecord(sp);
    spContainer.addChildRecord(opt);
    spContainer.addChildRecord(anchor);
    spContainer.addChildRecord(clientData);

    return spContainer;
}