วิธีเพิ่มรูปร่างใน PowerPoint ด้วย .NET

วิธีเพิ่มรูปร่างใน PowerPoint ด้วย .NET

Aspose.Slides FOSS for .NET รองรับการเพิ่ม AutoShapes, Tables, Connectors และ PictureFrames ไปยังสไลด์การนำเสนอ. รูปทรงทั้งหมดจะถูกเพิ่มผ่านคอลเลกชัน slide.Shapes.

คู่มือแบบขั้นตอนต่อขั้นตอน

ขั้นตอนที่ 1: ติดตั้งแพคเกจ

dotnet add package Aspose.Slides.Foss

ตรวจสอบการติดตั้ง:

using Aspose.Slides.Foss;
Console.WriteLine("Ready");

ขั้นตอนที่ 2: สร้างงานนำเสนอ

ใช้ Presentation เสมอพร้อมกับคำสั่ง using.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];
// ... add shapes ...
prs.Save("output.pptx", SaveFormat.Pptx);

ขั้นตอนที่ 3: เพิ่ม AutoShape

slide.Shapes.AddAutoShape(shapeType, x, y, width, height) วางรูปทรงที่ตำแหน่งและขนาดที่กำหนด (ทั้งหมดเป็นหน่วยจุด). ใช้ค่าคงที่ ShapeType เพื่อเลือกรูปทรง.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];

// Rectangle
var rect = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 300, 100);
rect.AddTextFrame("Rectangle shape");

// Ellipse
var ellipse = slide.Shapes.AddAutoShape(ShapeType.Ellipse, 400, 50, 200, 100);
ellipse.AddTextFrame("Ellipse shape");

prs.Save("autoshapes.pptx", SaveFormat.Pptx);

ขั้นตอนที่ 4: เพิ่มตาราง

slide.Shapes.AddTable(x, y, columnWidths, rowHeights) สร้างตารางที่ตำแหน่งที่ระบุ ความกว้างของคอลัมน์และความสูงของแถวเป็นอาเรย์ของค่าจุด.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];

var colWidths = new double[] { 150.0, 150.0, 150.0 };
var rowHeights = new double[] { 40.0, 40.0, 40.0 };
var table = slide.Shapes.AddTable(50, 200, colWidths, rowHeights);

// Set header row text
string[] headers = { "Product", "Units", "Revenue" };
for (int col = 0; col < headers.Length; col++)
    table.Rows[0][col].TextFrame.Text = headers[col];

// Set data rows
string[][] rows = {
    new[] { "Widget A", "120", "$2,400" },
    new[] { "Widget B", "85", "$1,700" },
};
for (int rowIdx = 0; rowIdx < rows.Length; rowIdx++)
    for (int col = 0; col < rows[rowIdx].Length; col++)
        table.Rows[rowIdx + 1][col].TextFrame.Text = rows[rowIdx][col];

prs.Save("table.pptx", SaveFormat.Pptx);

ขั้นตอนที่ 5: เพิ่มคอนเน็กเตอร์

คอนเนคเตอร์เชื่อมต่อสองรูปทรงให้เห็นได้ชัดเจน สร้างรูปทรงก่อน แล้วจึงเพิ่มคอนเนคเตอร์และตั้งจุดเชื่อมต่อเริ่มต้นและสิ้นสุดของมัน.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];

var box1 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 100, 150, 60);
box1.AddTextFrame("Start");

var box2 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 350, 100, 150, 60);
box2.AddTextFrame("End");

var conn = slide.Shapes.AddConnector(ShapeType.BentConnector3, 0, 0, 10, 10);
conn.StartShapeConnectedTo = box1;
conn.StartShapeConnectionSiteIndex = 3;  // right side of box1
conn.EndShapeConnectedTo = box2;
conn.EndShapeConnectionSiteIndex = 1;    // left side of box2

prs.Save("connector.pptx", SaveFormat.Pptx);

ดัชนีตำแหน่งการเชื่อมต่อจะถูกกำหนดเป็นเลข 0‑3 สำหรับสี่เหลี่ยมผืนผ้า: ด้านบน=0, ด้านซ้าย=1, ด้านล่าง=2, ด้านขวา=3.


ขั้นตอนที่ 6: เพิ่มกรอบรูปภาพ

ฝังรูปภาพและเพิ่มลงในสไลด์เป็น PictureFrame. อ่านไบต์ของรูปภาพก่อน, เพิ่มลงในคอลเลกชันรูปภาพของงานนำเสนอ, แล้วสร้างเฟรม.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
byte[] imageData = File.ReadAllBytes("logo.png");
var image = prs.Images.AddImage(imageData);

var slide = prs.Slides[0];
slide.Shapes.AddPictureFrame(
    ShapeType.Rectangle,  // bounding shape type
    50, 50,               // x, y in points
    200, 150,             // width, height in points
    image
);

prs.Save("with-image.pptx", SaveFormat.Pptx);

ปัญหาทั่วไปและการแก้ไข

รูปร่างปรากฏอยู่นอกพื้นที่สไลด์ที่มองเห็นได้

สไลด์มีขนาด 720 x 540 จุดโดยค่าเริ่มต้น. ค่าของ x หรือ y ที่เกินขอบเขตเหล่านั้นจะทำให้รูปร่างอยู่นอกสไลด์. รักษา x < 720 และ y < 540, และตรวจสอบให้แน่ใจว่า x + width <= 720 และ y + height <= 540.

NullReferenceException เมื่อเข้าถึง TextFrame

AddAutoShape() คืนค่าอ็อบเจ็กต์รูปทรงโดยตรง หากคุณเห็น null ให้ตรวจสอบว่าคุณไม่ได้ละทิ้งค่าที่คืนกลับ.

ข้อความในเซลล์ตารางเป็นค่าว่างหลังจากการกำหนดค่า

คุณสมบัติที่ถูกต้องคือ .TextFrame.Text (ไม่ใช่ .Text โดยตรงบนเซลล์). เข้าถึงเซลล์โดยใช้ table.Rows[rowIndex][colIndex].TextFrame.Text = "value".


คำถามที่พบบ่อย

ฉันสามารถเพิ่มรูปทรงได้กี่รูปในสไลด์?

ไม่มีขีดจำกัดที่กำหนดโดยไลบรารี. ขีดจำกัดเชิงปฏิบัติกำหนดโดยขนาดไฟล์และความสามารถในการเรนเดอร์ของโปรแกรมดู PPTX เป้าหมายของคุณ.

ฉันสามารถเปลี่ยนตำแหน่งของรูปร่างหลังจากเพิ่มได้หรือไม่?

ใช่ วัตถุ shape ที่ส่งกลับโดย AddAutoShape() มีคุณสมบัติ X, Y, Width และ Height ที่คุณสามารถตั้งค่าได้:

shape.X = 100;
shape.Y = 200;
shape.Width = 400;
shape.Height = 80;

ฉันสามารถตั้งค่าสีเส้นขอบของรูปร่างได้หรือไม่?

ใช่, ผ่าน shape.LineFormat:

using Aspose.Slides.Foss.Drawing;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 200, 0, 0);

รองรับแผนภูมิหรือไม่?

ไม่. แผนภูมิ, SmartArt, และวัตถุ OLE ไม่ได้ถูกนำมาใช้ในฉบับนี้.


ดูเพิ่มเติม

 ภาษาไทย