We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Setting pictures position

Hello,

I try to align pictures in the current Asopse Cells 7.0.3.1. I’ve no issue with the horizontal allignement and via Picture.setLeft but setTop does not seem to work as expected:

int pic = sheet.getPictures().add(5,5, “bridge.jpg”);
Picture picture = sheet.getPictures().get(pic);

picture.setLeft(20);

picture.setTop(20);

The setLeft moves the picture 20 pixles to the right as expected, setTop gives thew following error:

java.lang.IllegalArgumentException: The top value must be less than the upper left row height.
at com.aspose.cells.Shape.setTop(Unknown Source)


Any hint is welcome,

Kind Regards,

Andreas.






Hi,


Your error suggest that it is an error with your code, the error refers to that the top row height (i.e. fifth row height) is lesser than 20 pixels and so you cannot give greater value than your upper row’s height. For your information, if you are inserting a picture in a blank/default worksheet with default row heights of cells (in a worksheet) in the new workbook, now even you check each row height is 17 pixels, so you cannot give value more than that.

Here is the sample code that works absolutely fine as I tested:

//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Obtaining the reference of the newly added worksheet.
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
//Adding a picture at the location of a cell whose row and column indices
//are 5 in the worksheet. It is “F6” cell
int pictureIndex = worksheet.getPictures().add(5,5,“e:\test\school.jpg”);
Picture picture = worksheet.getPictures().get(pictureIndex);
System.out.println(worksheet.getCells().getRowHeightPixel(4)); //I got 17

//Positioning the picture proportional to row height and colum width
picture.setLeft(20);
picture.setTop(16); //It works now
//Saving the Excel file
workbook.save(“outtest_pictures.xls”);

Hi,

thanks for you feedback.

Is it possible to place the picture relative to a cell - beyond the size of the cell? The documentation is not very clear in this area.


Kind Regards,

Andreas.

Hi,


Well, there is no such method or API to directly do it. I think you need to calculate he upper / top row height(s) and then check if you need to move the picture a bit below and to the below row accordingly. See the following code for your reference.

Sample code:

//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Obtaining the reference of the newly added worksheet.
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
//Adding a picture at the location of a cell whose row and column indices
//are 5 in the worksheet. It is “F6” cell
int pictureIndex = worksheet.getPictures().add(5,5,“e:\test\school.jpg”);
Picture picture = worksheet.getPictures().get(pictureIndex);
//Get pixels of the top left row.
int trow = worksheet.getCells().getRowHeightPixel(4);

System.out.println(worksheet.getCells().getRowHeightPixel(4));

//Your desired pixels, you may code accordingly
int top = 20;

if(top>trow)
{


picture.moveToRange(6, 5, picture.getLowerRightRow()+1 ,picture.getLowerRightColumn());
picture.setLeft(20);
picture.setTop(top-trow);


}


//… Your code goes here
//…
//Saving the Excel file
workbook.save(“outtest_pictures.xls”);