Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Fitting a picture exactly to a single cell is a common requirement when designing spreadsheets that act as visual reports, product catalogs, employee directories, dashboards, or inventory lists. Rather than stretching an image across many cells or placing it loosely on a worksheet, you may want a clean, cell-bound image that stays aligned with the cell that owns it. Aspose.Cells supports this scenario in two complementary ways:
Picture to the worksheet, set its setPlacement to MOVE_AND_SIZE, and adjust its anchor cells (setUpperLeftRow, setUpperLeftColumn, setLowerRightRow, setLowerRightColumn) so the picture covers exactly one cell.setEmbeddedImage property. The image automatically scales to fit the cell’s display area and travels with the cell.
The rest of this article walks through both approaches, explains the relevant APIs, and shows how to use them in code.A floating picture is a Picture object that lives on the worksheet drawing layer. Although it is not part of any single cell, it is anchored to a cell range. The picture’s anchor cells — its upper-left and lower-right corners — determine its visual extent on the worksheet. By default, a freshly added picture spans several cells.
To make a floating picture cover exactly one cell, you need to:
Worksheet.getPictures().add(int row, int column, InputStream stream), which anchors the new picture to the given cell.Picture.setPlacement to PlacementType.MOVE_AND_SIZE so the picture moves and resizes with the underlying cell when the user changes the column width or row height.The picture’s anchor is defined by four zero-based index properties:
setUpperLeftRow — the row index of the picture’s top edge.setUpperLeftColumn — the column index of the picture’s left edge.setLowerRightRow — the row index of the picture’s bottom edge. To make the picture’s bottom edge sit at the bottom of row r, set this to r + 1.setLowerRightColumn — the column index of the picture’s right edge. To make the picture’s right edge sit at the right of column c, set this to c + 1.Row and column indices in Aspose.Cells are zero-based. Cell C6 has row index 5 and column index 2. Off-by-one errors on the lower-right anchor are the most common source of pictures that appear to overlap into an adjacent cell.
getPlacement is an enum of type PlacementType that controls how the picture behaves when the user resizes the row or column beneath it. The recommended value for a single-cell picture is PlacementType.MOVE_AND_SIZE, which causes the picture to move and resize together with its underlying cell, preserving the exact fit.
Workbook (or open an existing one).Worksheet from workbook.getWorksheets().get(0).InputStream (typically a FileInputStream) so the stream is closed properly.worksheet.getPictures().add(5, 2, stream) to add a picture anchored to cell C6. Capture the returned Picture reference.setUpperLeftRow(5), setUpperLeftColumn(2), setLowerRightRow(6), setLowerRightColumn(3).picture.setPlacement(PlacementType.MOVE_AND_SIZE) to keep the picture aligned with C6 when the column or row is resized..xlsx file.
The following code demonstrates the complete approach.import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook
from asposecells.api import Workbook, SaveFormat, PlacementType
workbook = Workbook()
worksheet = workbook.getWorksheets().get(0)
FileInputStream = jpype.JClass("java.io.FileInputStream")
fs = FileInputStream("logo.png")
try:
picIndex = worksheet.getPictures().add(5, 2, fs)
picture = worksheet.getPictures().get(picIndex)
picture.setUpperLeftRow(5)
picture.setUpperLeftColumn(2)
picture.setLowerRightRow(6)
picture.setLowerRightColumn(3)
picture.setPlacement(PlacementType.MoveAndSize)
finally:
fs.close()
workbook.save("output.xlsx", SaveFormat.Xlsx)
jpype.shutdownJVM()
Aspose.Cells also exposes a simpler mechanism for cell-bound images: the Cell.setEmbeddedImage property. Assigning image bytes to this property attaches the image to the cell itself, as if it were inline content.
Cell.setEmbeddedImage the most concise option when your goal is simply “an image that lives inside this cell.”Workbook (or open an existing one).Worksheet from workbook.getWorksheets().get(0).byte[] array (for example, by using a Files.readAllBytes call from java.nio.file.Files).worksheet.getCells().get("C6") or worksheet.getCells().get(5, 2).setEmbeddedImage property..xlsx file.
The following code demonstrates the complete approach.import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, SaveFormat
workbook = Workbook()
worksheet = workbook.getWorksheets().get(0)
# Get the target cell C6
cell = worksheet.getCells().get("C6")
# Read the image file into a byte array
imageData = open("logo.png", "rb").read()
# Embed the image directly into the cell
cell.setEmbeddedImage(imageData)
# Optionally adjust row height and column width so the embedded image is more visible
worksheet.getCells().setColumnWidth(2, 30) # Column C (index 2)
worksheet.getCells().setRowHeight(5, 100) # Row 6 (index 5)
# Save the resulting workbook as an .xlsx file
workbook.save("output.xlsx", SaveFormat.Xlsx)
jpype.shutdownJVM()
Both approaches produce a picture that fits inside a single cell, but they differ in how the picture is stored and how it behaves:
PictureCollection.Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.