Document Solutions for Excel, Java Edition | Document Solutions
Features / Shapes And Pictures / Size and Position of Image
In This Topic
    Size and Position of Image
    In This Topic

    Sometimes, it is required to render an image in a worksheet at a specific position. In such cases, it becomes very difficult to determine the position or size of the image by traversing through the cells of the worksheet.

    DsExcel allows you to know the size and absolute position of an image by using GetRangeBoundary method of type Rectangle in the CellInfo class. The method returns the location and size of the image (in pixels).

    Using Code

    Refer to the following example code to get the location and size of an image by adding it at a specified range in a worksheet.

    Java
    Copy Code
    IWorkbook workbook = new Workbook();
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    IRange range = worksheet.getRange("A1:D4");
    
    // Get the absolute location and size of the Range["D4:H8"] in the worksheet.
    Rectangle rect = CellInfo.GetRangeBoundary(range);
                
    // Add the image to the Range["D4:H8"].
     worksheet.getShapes().addPictureInPixel("image.png", rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
     
    workbook.save("GetRangePosition.xlsx");