如何在 C++ 中向 PowerPoint 添加形状

如何在 C++ 中向 PowerPoint 添加形状

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 点。xy 超出该范围的值会使形状位于幻灯片之外。保留 x < 720y < 540,并确保 x + width <= 720y + height <= 540

add_auto_shape() 返回已销毁对象的引用

不要在 Presentation 的生命周期之外存储引用。所有子对象均由 Presentation 拥有,并在销毁时失效。

表格单元格文本在赋值后为空

正确的方法是 .text_frame()->set_text()。访问单元格使用 table.rows()[row_index][col_index].text_frame()->set_text("value")


常见问题

我可以在幻灯片上添加多少个形状?

没有库强加的限制。实际限制取决于文件大小以及目标 PPTX 查看器的渲染能力。

添加后我可以更改形状的位置吗?

是的。add_auto_shape() 返回的 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 对象在此版本中未实现。


另请参阅

 中文