This topic focuses on how to insert Image into PowerPoint Table using C#. It encompasses all the details to establish the environment, a step-by-step procedure to create and fill a table cell with an image, and working example that exhibits how to add image in PPTX Table in C#. The developed application can be used in any .NET configured environment like Windows, macOS or Linux.
Steps to Insert Image in PowerPoint Table using C#
- Set up the environment to add Aspose.Slides for .NET to insert a table image
- Instantiate the Presentation class object to add a new presentation and access the first slide from the slides collection
- Insert a table in the selected slide having the definite heights for rows and columns using the AddTable() method
- Insert the desired image inside the presentation image collection
- Access the cell belonging to the first row and column from the table and set added image inside that
- Save the presentation with the table image in the PPTX format
In the above steps, we have explained how to display image in PPTX Table in C#. The process will commence by creating a default presentation using an instance of the Presentation class and getting access to its first slide. In the subsequent steps, we will add a new table using AddTable() method by providing the number of rows and columns for the table, which is then followed by loading and adding the source image inside the presentation image collection. Finally, the desired cell from the table will be selected and the loaded image will be set for that particular cell before saving the output presentation on the disk.
Code to Insert Image in PowerPoint Table using C#
using System.Drawing; | |
using Aspose.Slides; | |
namespace TestSlides | |
{ | |
public class InsertImageInTable | |
{ | |
public static void AddImageInsideTable() | |
{ | |
string filesPath = @"/Users/Documents/KnowledgeBase/TestData/"; | |
License license = new License(); | |
license.SetLicense(filesPath + "Conholdate.Total.Product.Family.lic"); | |
//Create a new presentation to insert an image inside the table | |
Presentation TablePresentation = new Presentation(); | |
//Load the first default slide of the presentation | |
ISlide targetSlide = TablePresentation.Slides[0]; | |
// Access the source image from the disk and add to presentation images | |
System.Drawing.Image tblImage = (System.Drawing.Image)new Bitmap(filesPath+ "Test.png"); | |
IPPImage ppTblImage = TablePresentation.Images.AddImage(tblImage); | |
//Now declare the rows heights and columns widths | |
double[] columnsWidths = { 45, 45, 45 ,45}; | |
double[] rowsHeights = { 45, 26, 30, 30 }; | |
// Insert a table inside the slide | |
Aspose.Slides.ITable tableWithImage = targetSlide.Shapes.AddTable(55, 55, columnsWidths, rowsHeights); | |
// Access the first cells inside the first row of the table | |
ICell tableCell = tableWithImage[0,0]; | |
// Set the cell fill format to picture | |
tableCell.CellFormat.FillFormat.FillType = FillType.Picture; | |
// Set the picture fill mode | |
tableCell.CellFormat.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch; | |
// Set the image for the selected cell inside the table | |
tableCell.CellFormat.FillFormat.PictureFillFormat.Picture.Image = ppTblImage; | |
//Save the presentation with the table image on the disk | |
TablePresentation.Save(filesPath + "PresWithTableImage.pptx", Aspose.Slides.Export.SaveFormat.Pptx); | |
} | |
} | |
} |
In this topic, we have focused on how will you insert a table image in a Presentation using C#. If want to enhance your learning further about managing the tables inside the PowerPoint, refer to the article, How to Create a Table in PowerPoint using C#.