Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aspose-com-kb/dd68b49257bbe95849c406d47d15da33 to your computer and use it in GitHub Desktop.
Save aspose-com-kb/dd68b49257bbe95849c406d47d15da33 to your computer and use it in GitHub Desktop.
How to Insert Image into PowerPoint Table using Java. For more details: https://kb.aspose.com/slides/java/how-to-insert-image-into-powerpoint-table-using-java/
import com.aspose.slides.FillType;
import com.aspose.slides.ICell;
import com.aspose.slides.IPPImage;
import com.aspose.slides.ISlide;
import com.aspose.slides.ITable;
import com.aspose.slides.License;
import com.aspose.slides.PictureFillMode;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Paths;
public class InsertTableImage {
public static void main(String[] args) throws Exception{
String filesPath = "/Documents/KnowledgeBase/TestData/";
License svgImportlicense = new License();
svgImportlicense.setLicense(filesPath + "Conholdate.Total.Product.Family.lic");
//Generate a default presentation to insert a PNG image
Presentation tblImagePresentation = new Presentation();
//Load the first default slide inside the presentation slides collection
ISlide slide = tblImagePresentation.getSlides().get_Item(0);
// Load and add the image inside the presentation image collection
IPPImage ppTblImage = tblImagePresentation.getImages().
addImage(Files.readAllBytes(Paths.get(filesPath + "Source.png")));
// Define the arrays containing the row heights and column widths
double[] dColumnsWidths = { 55, 55, 55 };
double[] dRowsHeights = { 54, 26, 46, 45 };
// Insert a new table with set rows and columns
ITable tblWithImage = slide.getShapes().addTable(60, 60, dColumnsWidths, dRowsHeights);
// Load the first cells inside the first row of the table
ICell tblCell = tblWithImage.get_Item(0, 0);
// Set the cell cell fill format to add a picture
tblCell.getCellFormat().getFillFormat().setFillType(FillType.Picture);
// Now set the picture fill mode to stretch
tblCell.getCellFormat().getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.Stretch);
// Set the image for the table cell inside
tblCell.getCellFormat().getFillFormat().getPictureFillFormat().getPicture().setImage(ppTblImage);
//Save the presentation with a table image on the disk
tblImagePresentation.save(filesPath + "PresentationWithTableImage.pptx", SaveFormat.Pptx);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment