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

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

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

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

ขั้นตอนที่ 1: สร้างและเชื่อมโยงไลบรารี

git clone https://github.com/aspose-slides-foss/Aspose.Slides-FOSS-for-Cpp.git
cd Aspose.Slides-FOSS-for-Cpp && mkdir build && cd build
cmake .. && cmake --build .

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

ใช้การจัดสรรบนสแตกสำหรับการทำความสะอาดแบบ RAII.

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    // ... add shapes ...
    prs.save("output.pptx", asf::SaveFormat::PPTX);
    return 0;
}

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

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

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    // Rectangle
    auto& rect = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 300, 100);
    rect.text_frame()->set_text("Rectangle shape");

    // Ellipse
    auto& ellipse = slide.shapes().add_auto_shape(
        asf::ShapeType::ELLIPSE, 400, 50, 200, 100);
    ellipse.text_frame()->set_text("Ellipse shape");

    prs.save("autoshapes.pptx", asf::SaveFormat::PPTX);
    return 0;
}

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

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

#include <Aspose/Slides/Foss/presentation.h>
#include <vector>
#include <string>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    std::vector<double> col_widths = {150.0, 150.0, 150.0};
    std::vector<double> row_heights = {40.0, 40.0, 40.0};
    auto& table = slide.shapes().add_table(50, 200, col_widths, row_heights);

    // Set header row text
    std::vector<std::string> headers = {"Product", "Units", "Revenue"};
    for (size_t col = 0; col < headers.size(); ++col) {
        table.rows()[0][col].text_frame()->set_text(headers[col]);
    }

    // Set data rows
    std::vector<std::vector<std::string>> rows = {
        {"Widget A", "120", "$2,400"},
        {"Widget B", "85",  "$1,700"},
    };
    for (size_t r = 0; r < rows.size(); ++r) {
        for (size_t c = 0; c < rows[r].size(); ++c) {
            table.rows()[r + 1][c].text_frame()->set_text(rows[r][c]);
        }
    }

    prs.save("table.pptx", asf::SaveFormat::PPTX);
    return 0;
}

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

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

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    auto& box1 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 100, 150, 60);
    box1.text_frame()->set_text("Start");

    auto& box2 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 350, 100, 150, 60);
    box2.text_frame()->set_text("End");

    auto& conn = slide.shapes().add_connector(
        asf::ShapeType::BENT_CONNECTOR3, 0, 0, 10, 10);
    conn.set_start_shape_connected_to(&box1);
    conn.set_start_shape_connection_site_index(3); // right side of box1
    conn.set_end_shape_connected_to(&box2);
    conn.set_end_shape_connection_site_index(1);   // left side of box2

    prs.save("connector.pptx", asf::SaveFormat::PPTX);
    return 0;
}

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


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

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

#include <Aspose/Slides/Foss/presentation.h>
#include <fstream>
#include <vector>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;

    std::ifstream file("logo.png", std::ios::binary);
    std::vector<uint8_t> data((std::istreambuf_iterator<char>(file)),
                               std::istreambuf_iterator<char>());

    auto& image = prs.images().add_image(data);

    auto& slide = prs.slides()[0];
    slide.shapes().add_picture_frame(
        asf::ShapeType::RECTANGLE, // bounding shape type
        50, 50,                    // x, y in points
        200, 150,                  // width, height in points
        image);

    prs.save("with-image.pptx", asf::SaveFormat::PPTX);
    return 0;
}

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

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

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

add_auto_shape() ส่งคืนการอ้างอิงถึงอ็อบเจกต์ที่ถูกทำลาย

อย่าเก็บอ้างอิงไว้เกินช่วงอายุของ Presentation. วัตถุลูกทั้งหมดเป็นของ Presentation และจะถูกทำให้เป็นโมฆะเมื่อทำลาย.

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

วิธีที่ถูกต้องคือ .text_frame()->set_text(). เข้าถึงเซลล์เป็น table.rows()[row_index][col_index].text_frame()->set_text("value").


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

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

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

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

ใช่. วัตถุ shape ที่ส่งกลับโดย add_auto_shape() มีเมธอด set_x(), set_y(), set_width() และ set_height():

shape.set_x(100);
shape.set_y(200);
shape.set_width(400);
shape.set_height(80);

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

ใช่, ผ่าน shape.line_format():

shape.line_format().fill_format().solid_fill_color().set_color(
    asf::Color::from_argb(255, 200, 0, 0));

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

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


ดูเพิ่มเติม

 ภาษาไทย