このトピックでは、C# を使用して PowerPoint テーブルに画像を挿入する方法に焦点を当てています。環境を確立するためのすべての詳細、表のセルを作成して画像で埋めるための段階的な手順、C# で PPTX テーブルに画像を追加する方法を示す実際の例が含まれています .開発されたアプリケーションは、Windows、macOS、Linux などの .NET 構成環境で使用できます。
C# を使用して PowerPoint テーブルに画像を挿入する手順
- Aspose.Slides for .NET を追加してテーブル イメージを挿入する環境を設定します
- Presentation クラス オブジェクトをインスタンス化して新しいプレゼンテーションを追加し、スライド コレクションの最初のスライドにアクセスします
- AddTable() メソッドを使用して、選択したスライドに行と列の高さが決まっている表を挿入します
- プレゼンテーション画像コレクション内に目的の画像を挿入します
- テーブルから最初の行と列に属するセルにアクセスし、その中に追加された画像を設定します
- テーブル イメージを含むプレゼンテーションを PPTX 形式で保存します。
上記の手順では、C# で PPTX テーブルに画像を表示する方法を説明しました。このプロセスは、Presentation クラスのインスタンスを使用してデフォルトのプレゼンテーションを作成し、最初のスライドにアクセスすることから始まります。以降の手順では、AddTable() メソッドを使用して、テーブルの行数と列数を指定して新しいテーブルを追加します。その後、プレゼンテーション画像コレクション内のソース画像を読み込んで追加します。最後に、テーブルから目的のセルが選択され、ロードされた画像がその特定のセルに設定されてから、出力プレゼンテーションがディスクに保存されます。
C# を使用して PowerPoint テーブルに画像を挿入するコード
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); | |
} | |
} | |
} |
このトピックでは、C# を使用してプレゼンテーションにテーブル イメージを挿入する方法 に焦点を当ててきました。 PowerPoint 内の表の管理についてさらに学習を深めたい場合は、C# を使用して PowerPoint で表を作成する方法 の記事を参照してください。