Unexpected Image and Cell Width in Excel File Generated using Aspose.Cells for Java

Hello,

I’m currently using Aspose.Cells for Java (23.5) and I’ve come across an unexpected behavior with respect to the image and cell widths.

I have a method which puts images in an Excel cell, with the images being set to a height and width of 1x1 inch. I also adjust the cell to fit the images, ideally setting the cell to a width of 3 inches, with the height of cell dependent on the no. of images given , i.e, 1 inch for upto 3 images, 2 inches for 4->6 images . However, I am noticing that the third images in cell is slightly longer in width than the set width of 1 inch. Furthermore, the width of the cell itself is longer than the 3 inches I have set.

Here is the code I am using:

public static void putImagesInCell(Cell cell, List<String> imageUrls, Worksheet ws) throws Exception {
	// Get the worksheet from the cell
	Worksheet worksheet = cell.getWorksheet();

	// The size of each image
	int imageSize = 1;

	// Default cell size
	double defaultCellWidth = 3;
	double defaultCellHeight = 1;
	ws.getCells().setRowHeightInch(cell.getRow(),defaultCellHeight);
	ws.getCells().setColumnWidthInch(cell.getColumn(),defaultCellWidth);

// If there are more than 3 images, adjust the cell height
	if (imageUrls.size() > 3) {
		ws.getCells().setRowHeightInch(cell.getRow(),defaultCellHeight * Math.ceil((double) imageUrls.size() /3)); // double the cell height
	}

	// Position of the next image
	double leftOffset = 0;
	double topOffset = 0;

	// Add each image to the cell
	for (int i = 0; i < imageUrls.size(); i++) {
		// Read the image from the URL

		// Create a picture in the cell from the image
		byte[] imgData = getImageBytes(imageUrls.get(i));

		assert imgData != null;
		int index = worksheet.getPictures().add(cell.getRow(), cell.getColumn(), new ByteArrayInputStream(imgData));
		Picture pic = worksheet.getPictures().get(index);
		pic.setPlacement(PlacementType.MOVE);
		// Set the size and position of the image
		pic.setWidthInch(imageSize);
		pic.setHeightInch(imageSize);
		pic.setLeftInch(leftOffset);
		pic.setTopInch(topOffset);
		// If this is the third image, adjust the position for the next image
		if (i % 3 == 2) {
			leftOffset -= 0;
			topOffset += imageSize;
		} else {
			leftOffset += imageSize;
		}
	}
}

Here, we take the cell in which we have to insert images, the list of URLs of images to insert, and the worksheet to set column and row height and width of given cell. I have uploaded the image of the cell with the above images. If you notice here, the third images are a little longer than 1 inch which have been set for the other images. Also the cell width is around 40.83 (chars) or around 3.42 inches instead of 3 as has been set in code.

Screenshot 2023-06-13 at 11.51.00 AM.jpg (53.3 KB)

@kwiz,

Thanks for the screenshot.

We noticed the issue in your provided screenshot. But we need a standalone console (Java) application/program (source code without compilation errors) to reproduce the issue on our end. Also, share resources and sample files (input Excel file (if any), output file(s), other files, etc.). We will then evaluate your issue using provided app and samples soon.