เพิ่มรูปภาพในเซลล์ของตาราง
Contents
[
Hide
]
VSTO
ด้านล่างเป็นโค้ดสำหรับการเพิ่มรูปภาพในเซลล์ของตาราง:
//เปิดคลาส Presentation ที่มีตารางอยู่
string FileName = "Adding Image in Table Cell.pptx";
string ImageFile = "AsposeLogo.jpg";
Presentation pres = Application.Presentations.Open(FileName);
//รับสไลด์แรก
Slide sld = pres.Slides[1];
foreach (Shape shp in sld.Shapes)
{
if (shp.HasTable == Microsoft.Office.Core.MsoTriState.msoTrue)
{
Cell cell= shp.Table.Rows[1].Cells[1];
cell.Shape.Fill.UserPicture(ImageFile);
}
}
Aspose.Slides
Aspose.Slides for .NET ได้ให้ API ที่ง่ายที่สุดในการสร้างตารางอย่างง่ายดาย เพื่อเพิ่มรูปภาพในเซลล์ของตารางขณะสร้างตารางใหม่ โปรดทำตามขั้นตอนด้านล่าง:
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงของสไลด์โดยใช้ Index ของมัน
- กำหนดอาเรย์ของคอลัมน์พร้อมความกว้าง
- กำหนดอาเรย์ของแถวพร้อมความสูง
- เพิ่ม Table ไปยังสไลด์โดยใช้เมธอด AddTable ที่เปิดให้ใช้โดยอ็อบเจกต์ IShapes
- สร้างอ็อบเจกต์ Bitmap เพื่อเก็บไฟล์รูปภาพ
- เพิ่มรูปภาพ Bitmap ไปยังอ็อบเจกต์ IPPImage
- ตั้งค่า Fill Format ของเซลล์ตารางเป็น Picture
- เพิ่มรูปภาพไปยังเซลล์แรกของตาราง
- บันทึกการนำเสนอที่แก้ไขเป็นไฟล์ PPTX
string FileName = "Adding Image in Table Cell.pptx";
string ImageFile = "AsposeLogo.jpg";
Presentation MyPresentation = new Presentation(FileName);
//รับสไลด์แรก
ISlide sld = MyPresentation.Slides[0];
//สร้างอ็อบเจกต์ Bitmap Image เพื่อเก็บไฟล์รูปภาพ
using IImage image = Images.FromFile(ImageFile);
//สร้างอ็อบเจกต์ IPPImage ด้วยอ็อบเจกต์ bitmap
IPPImage imgx1 = MyPresentation.Images.AddImage(image);
foreach (IShape shp in sld.Shapes)
if (shp is ITable)
{
ITable tbl = (ITable)shp;
//เพิ่มรูปภาพไปยังเซลล์แรกของตาราง
tbl[0, 0].FillFormat.FillType = FillType.Picture;
tbl[0, 0].FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;
tbl[0, 0].FillFormat.PictureFillFormat.Picture.Image = imgx1;
}
//บันทึก PPTX ไปยังดิสก์
MyPresentation.Save(FileName, Export.SaveFormat.Pptx);