Aspose.Cells allows developers to add pictures to spreadsheets at runtime. Moreover, the positioning of these pictures can be controlled at runtime, which is discussed in more detail in the coming sections.
This article explains how to add pictures, and how to insert an image that shows the content of certain cells.
Adding pictures to a spreadsheet is very easy. It only takes a few lines of code: Simply call the Add method of the Pictures collection (encapsulated in the Worksheet object). The Add method takes the following parameters:
- Upper left row index, the index of the upper left row.
- Upper left column index, the index of the upper left column.
- Image file name, the name of the image file, complete with path.
There are two possible ways to control the positioning of pictures using Aspose.Cells:
- Proportional positioning: define a position proportional to the row height and width.
- Absolute positioning: define the exact position on the page where the image will be inserted, for example, 40 pixels to the left and 20pixels below the edge of the cell.
Developers can position the pictures proportional to row height and column width using the UpperDeltaX and UpperDeltaY properties of the Aspose.Cells.Drawing.Picture object. A Picture object can be obtained from the Pictures collection by passing its picture index. This example places an image in the F6 cell.
Developers can also position the pictures absolutely by using the Left and Top properties of the Picture object. This example places an image in cell F6, 60 pixels from the left and 10 pixels from the top of the cell.
Inserting a Picture Based on Cell Reference
Aspose.Cells lets you display the contents of a worksheet cell in an image shape. You can link the picture to the cell that contains the data that you want to display. Since the cell, or cell range, is linked to the graphic object, changes that you make to the data in that cell or cell range automatically appear in the graphic object.
Add a picture to the worksheet by calling the AddPicture method of the ShapeCollection collection (encapsulated in the Worksheet object). Specify the cell range by using the Formula attribute of the Picture object.