หัวข้อนี้มุ่งเน้นไปที่วิธีการ แทรกรูปภาพลงในตาราง PowerPoint โดยใช้ C# รวมรายละเอียดทั้งหมดในการสร้างสภาพแวดล้อม ขั้นตอนทีละขั้นตอนในการสร้างและเติมเซลล์ตารางด้วยรูปภาพ และตัวอย่างการทำงานที่แสดงวิธี เพิ่มรูปภาพใน PPTX ตารางใน C# . แอปพลิเคชันที่พัฒนาขึ้นสามารถใช้ในสภาพแวดล้อมที่กำหนดค่า .NET เช่น Windows, macOS หรือ Linux
ขั้นตอนในการแทรกรูปภาพในตาราง PowerPoint โดยใช้ C#
- ตั้งค่าสภาพแวดล้อมเพื่อเพิ่ม Aspose.Slides for .NET เพื่อแทรกรูปภาพตาราง
- สร้างอินสแตนซ์วัตถุคลาส Presentation เพื่อเพิ่มงานนำเสนอใหม่และเข้าถึงสไลด์แรกจากคอลเลกชันสไลด์
- แทรกตารางในสไลด์ที่เลือกโดยมีความสูงที่แน่นอนสำหรับแถวและคอลัมน์โดยใช้เมธอด AddTable()
- แทรกรูปภาพที่ต้องการในคอลเลกชันรูปภาพงานนำเสนอ
- เข้าถึงเซลล์ที่อยู่ในแถวและคอลัมน์แรกจากตารางและตั้งค่ารูปภาพที่เพิ่มเข้าไปภายในนั้น
- บันทึกงานนำเสนอด้วยภาพตารางในรูปแบบ PPTX
ในขั้นตอนข้างต้น เราได้อธิบายวิธีการ แสดงรูปภาพในตาราง PPTX ใน C# กระบวนการจะเริ่มต้นด้วยการสร้างงานนำเสนอเริ่มต้นโดยใช้อินสแตนซ์ของคลาสการนำเสนอและการเข้าถึงสไลด์แรก ในขั้นตอนต่อๆ ไป เราจะเพิ่มตารางใหม่โดยใช้เมธอด AddTable() โดยระบุจำนวนแถวและคอลัมน์สำหรับตาราง จากนั้นตามด้วยการโหลดและเพิ่มรูปภาพต้นฉบับภายในคอลเลกชันรูปภาพการนำเสนอ สุดท้าย เซลล์ที่ต้องการจากตารางจะถูกเลือกและอิมเมจที่โหลดจะถูกตั้งค่าสำหรับเซลล์นั้นๆ ก่อนบันทึกงานนำเสนอเอาต์พุตบนดิสก์
รหัสเพื่อแทรกรูปภาพในตาราง PowerPoint โดยใช้ C
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 โปรดดูบทความ วิธีสร้างตารางใน PowerPoint โดยใช้ C#