How to Add Shapes to PowerPoint in C++
This guide shows how to add AutoShapes, tables, connectors, and picture frames to PowerPoint slides using Aspose.Slides FOSS for C++. All shape types are added through the slide.shapes() collection via methods such as add_auto_shape(), add_table(), add_connector(), and add_picture_frame().
Step-by-Step Guide
Step 1: Build and Link the Library
Clone the repository and build with CMake to produce the static library to link against:
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 .Step 2: Create a Presentation
Use stack allocation to construct a Presentation object; access the first slide via prs.slides()[0], then call prs.save() when done:
#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;
}Step 3: Add an AutoShape
slide.shapes().add_auto_shape(shape_type, x, y, width, height) places a shape at the given position and size (all in points). Use ShapeType constants to select the shape.
#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;
}Step 4: Add a Table
slide.shapes().add_table(x, y, col_widths, row_heights) creates a table at the specified position. Column widths and row heights are vectors of point values.
#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;
}Step 5: Add a Connector
Connectors link two shapes visually. Create the shapes first, then add a connector and set its start and end connection points.
#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;
}Connection site indices are numbered 0-3 for a rectangle: top=0, left=1, bottom=2, right=3.
Step 6: Add a Picture Frame
Embed an image and add it to the slide as a PictureFrame. Read the image bytes first, add them to the presentation’s image collection, then create the frame.
#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;
}Common Issues and Fixes
Shape appears outside the visible slide area
Slides are 720 x 540 points by default. Values of x or y beyond those bounds place the shape off-slide. Keep x < 720 and y < 540, and ensure x + width <= 720 and y + height <= 540.
add_auto_shape() returns a reference to a destroyed object
Do not store references beyond the lifetime of the Presentation. All child objects are owned by the Presentation and invalidated on destruction.
Table cell text is empty after assignment
The correct method is .text_frame()->set_text(). Access cells as table.rows()[row_index][col_index].text_frame()->set_text("value").
Frequently Asked Questions
How many shapes can I add to a slide?
There is no library-imposed limit. Practical limits depend on file size and the rendering capability of your target PPTX viewer.
Can I change a shape’s position after adding it?
Yes. The shape object returned by add_auto_shape() has set_x(), set_y(), set_width(), and set_height() methods:
shape.set_x(100);
shape.set_y(200);
shape.set_width(400);
shape.set_height(80);Can I set the shape outline (border) color?
Yes, via shape.line_format():
shape.line_format().fill_format().solid_fill_color().set_color(
asf::Drawing::Color::from_argb(255, 200, 0, 0));Are charts supported?
No. Charts, SmartArt, and OLE objects are not implemented in this edition.