如何使用 Java 将图像插入到 PowerPoint 表格中

这篇简单的文章重点介绍如何使用 Java 将图像插入到 PowerPoint 表格中。它公开了设置环境所需的所有信息和分步过程以及示例代码,以创建幻灯片表格单元格并用图像填充幻灯片表格单元格,该图像显示如何PPTX 表格中添加图像爪哇。此应用程序可用于任何 Java 配置环境,如 macOS、Windows 或 Linux。

使用 Java 在 PowerPoint 表格中插入图像的步骤

  1. 设置环境添加Aspose.Slides for Java插入表格图片
  2. 初始化 Presentation 类实例以添加新演示文稿并访问幻灯片集合中的第一张幻灯片
  3. 使用 addTable() 方法在选定的幻灯片中创建一个表格,其中的行和列具有确定的高度
  4. 将加载的图像添加到演示图像集合中
  5. 加载属于表格内第一行和第一列的单元格,并在其中设置图像
  6. 以 PPTX 格式保存具有表格图像的演示文稿

我们已经解释了如何使用上述步骤在 Java 的 PPTX 表中显示图像。首先,将使用 Presentation 类对象创建演示文稿,并访问幻灯片集合中的第一张幻灯片。我们将通过使用 addTable() 方法提供行数和列数来插入一个新表,然后从磁盘访问图像并将其添加到演示图像集合中。最后,在将演示文稿以 PPTX 格式保存到磁盘之前,我们将在选定的表格单元格内设置图像。

使用 Java 在 PowerPoint 表格中插入图像的代码

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);
}
}

在上面的示例中,我们演示了如何使用 Java* 通过几个 API 调用*在幻灯片中插入表格图像。我们已经创建了一个示例演示文稿,并使用 ITable 类对象添加了一个包含行和列集合的表。为表内任何特定单元格公开的 CellFormat 实例用于使用 FillType.Picture 枚举器将单元格的填充格式设置为图像。最后,将图像添加到演示图像集合中,并用于显示细胞图像。

在此示例中,我们了解了如何使用 Java 在演示文稿中插入表格图像。如果您有兴趣了解有关在 PowerPoint 中管理表格的更多信息,请参阅主题 如何使用 Java 在幻灯片中插入表格

 简体中文