この簡単な記事では、Java を使用して PowerPoint テーブルに画像を挿入する方法に焦点を当てています。環境をセットアップするために必要なすべての情報と、スライド テーブルのセルを作成して画像で埋めるためのサンプル コードとともに、PPTX に画像を追加する方法を示すサンプル コードが公開されています。ジャバ**。このアプリケーションは、macOS、Windows、Linux などの Java 構成環境で使用できます。
Java を使用して PowerPoint テーブルに画像を挿入する手順
- Aspose.Slides for Java を追加してテーブル イメージを挿入する環境を設定します
- Presentation クラス インスタンスを初期化して、新しいプレゼンテーションを追加し、スライド コレクションの最初のスライドにアクセスします
- addTable() メソッドを使用して、選択したスライド内に行と列の明確な高さを持つテーブルを作成します
- 読み込んだ画像をプレゼンテーション画像コレクション内に追加します
- テーブル内の最初の行と列に属するセルをロードし、その中に画像を設定します
- テーブル イメージを含むプレゼンテーションを 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); | |
} | |
} |
上記の例では、いくつかの API 呼び出しを使用して、Java を使用してスライドにテーブル イメージを挿入する方法を示しました。サンプル プレゼンテーションを作成し、ITable クラス オブジェクトを使用して行と列のコレクションを持つテーブルを追加しました。テーブル内の特定のセルに対して公開されている CellFormat インスタンスは、FillType.Picture 列挙子を使用してセルの塗りつぶし形式を画像に設定するために使用されます。最後に、画像はプレゼンテーション画像コレクション内に追加され、表示用のセル画像に使用されます。
この例では、Java を使用してプレゼンテーションにテーブル イメージを挿入する方法 について学習しました。 PowerPoint 内の表の管理について詳しく知りたい場合は、トピック Java を使用してスライドに表を挿入する方法 を参照してください。