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

Free Support Forum - aspose.com

Adding pictures to cells- resizing cells to contain picture doesn't work

With the sample code below which adds a 100x100 picture to each of several cells with a 2px border, I expect to get a column of cells sized to fit the image. Instead, each image is a different size, and the column width is extremely large.


If I comment out the call to picture.setPlacement, the pictures are at least all the same size now, but the column width is still extreme and the border is more than 2px. Further, autosizing the columns and rows in excel doesn’t adjust the width or height of the image columns to fit the image exactly.

So:

1) Why does setPlacement() cause each image to be a different size?

2) Why are the cells not sized to 104px x 104px in accordance with my parameters?

3) If I move the setPlacement call to a loop just before the save, and call setPlacement(MOVE_AND_SIZE) on each Picture in the PictureCollection at that point, the images are all the same size initially. However, they do not resize when the cell is resized in excel. Why not?

In short, how can I get this simple example to do what’s expected…put same-sized pictures in cells just the right size to show them, have the pictures resize properly (to still fill the cell) when cells are resized in excel, and have auto-resizing cells in excel do something sane?

I need this to work with aspose-cells 7.2.2, but it doesn’t work with either that or the latest 8.5.2 version.

We have a small business license, so I’d like priority support on this, but don’t know how to arrange that.
public class AsposeTest {

	public static void main(String[] args) throws Exception {
		Workbook workbook = new Workbook();
		WorksheetCollection worksheets = workbook.getWorksheets();
		Worksheet sheet = worksheets.get(0);

		int column=1;
		for (int row=5; row<10; row++ ) {
			int pictureIndex = sheet.getPictures().add(row,column,"/Users/christop/Desktop/test.png");

			Picture picture = sheet.getPictures().get(pictureIndex);
			picture.setWidth(100);
			picture.setHeight(100);
			picture.setUpperLeftRow(row);
			picture.setTop(2);
			picture.setUpperLeftColumn(column);
			picture.setLeft(2);                
			picture.setLockAspectRatio(true);
			picture.setPlacement(PlacementType.MOVE_AND_SIZE);
			sheet.getCells().setRowHeight(row, 104);
		}
		sheet.getCells().setColumnWidth(column, 104);
		sheet.autoFitColumns(column, 104);

//		for (int i=0;i<sheet.getPictures().getCount(); ++i) {
//			sheet.getPictures().get(i).setPlacement(PlacementType.MOVE_AND_SIZE);
//		}

		
		workbook.save("/Users/christop/Desktop/test.xls");
		System.out.println("Saved file!");
	}
}

Hi Jon,


Thank you for contacting Aspose support.

Please check the piece of code provided at the bottom of this thread. Also attached is it’s resultant spreadsheet for your reference. Please see if this is what you are looking for. Regarding your concerns, please find below the answers in the same sequence.

  1. We have tested the scenario against the latest version of Aspose.Cells for Java 8.5.2.4, and we are not able to observe the problem of pictures insertion with different sizes. The problem could be due to some issue with your current version of the API therefore please test the scenario against the latest release.
  2. Please use the Cells.setRowHeightPixel & Cells.setColumnWidthPixel methods that accept the height & width values in Pixels.
  3. Could you please share the desired results that you may create manually in Excel and could give us a better understanding of your requirements?
  4. Regarding Priority Support, you have to first subscribe to the Priority Support and then will be able to post the concerns in Priority Support Forum. If you already have an active Priority Support subscription, you can directly post in the above linked support forum.

Java

Workbook workbook = new Workbook();
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);

int column=1;
int pictureWidth = 0;
for (int row=5; row<10; row++ ) {
int pictureIndex = sheet.getPictures().add(row,column,“D:/test.png”);
Picture picture = sheet.getPictures().get(pictureIndex);
picture.setBorderLineColor(Color.getRed());
picture.setBorderWeight(.1d);
sheet.getCells().setRowHeightPixel(row, picture.getHeight());
picture.setPlacement(PlacementType.MOVE_AND_SIZE);
pictureWidth = picture.getHeight();
}
sheet.getCells().setColumnWidthPixel(column, pictureWidth);
workbook.save(“D:/test.xls”);