How to Create Presentations in C++

How to Create Presentations in C++

Aspose.Slides FOSS for C++ lets you create PowerPoint presentations entirely in C++ with no dependency on Microsoft Office. This guide shows how to create a new presentation, add slides and shapes, format text, and save the result.

Step-by-Step Guide

Step 1: Build and Link the Library

Clone and build with CMake. C++17 or later is required.

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 .

In your project’s CMakeLists.txt:

find_package(AsposeSlideFoss REQUIRED)
target_link_libraries(my_app PRIVATE aspose-slides-foss)

No other system packages are required.


Step 2: Include the Required Headers

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

All types live in the Aspose::Slides::Foss namespace. For brevity, the examples below use a namespace alias:

namespace asf = Aspose::Slides::Foss;

Step 3: Create a Presentation

Construct a Presentation on the stack. A new presentation starts with one blank slide.

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

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

    asf::Presentation prs;
    std::cout << "Slides in new presentation: " << prs.slides().size() << "\n";
    prs.save("output.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Important: Use stack allocation or std::unique_ptr so the destructor releases internal resources automatically.


Step 4: Access a Slide

The first slide is at index 0. A blank presentation has exactly one slide.

namespace asf = Aspose::Slides::Foss;

asf::Presentation prs;
auto& slide = prs.slides()[0]; // zero-based index
prs.save("output.pptx", asf::SaveFormat::PPTX);

Step 5: Add a Shape

Use slide.shapes().add_auto_shape() to add an AutoShape. The parameters are (shape_type, x, y, width, height) all in points (1 point = 1/72 inch; standard slide is 720 x 540 pt).

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

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

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

    // Rectangle at (50, 50) with 400 wide and 120 tall
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 400, 120);

    // Attach a text frame
    shape.add_text_frame("Hello from Aspose.Slides FOSS!");

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

Step 6: Save the Presentation

Call prs.save(path, SaveFormat::PPTX) before the Presentation goes out of scope. PPTX is the only supported output format.

prs.save("result.pptx", asf::SaveFormat::PPTX);

The file is written atomically; if an error occurs before this call, no output file is created.


Complete Working Example

The following program creates a two-slide presentation with a title shape on the first slide and a table on the second.

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

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

    asf::Presentation prs;

    // --- Slide 1: title shape ---
    auto& slide1 = prs.slides()[0];
    auto& title = slide1.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 40, 40, 640, 80);
    auto& tf = title.add_text_frame("Q1 Results: Executive Summary");
    auto& fmt = tf.paragraphs()[0].portions()[0].portion_format();
    fmt.set_font_height(32);
    fmt.set_font_bold(asf::NullableBool::TRUE);
    fmt.fill_format().set_fill_type(asf::FillType::SOLID);
    fmt.fill_format().solid_fill_color().set_color(
        asf::Color::from_argb(255, 0, 70, 127));

    // --- Slide 2: table ---
    prs.slides().add_empty_slide(prs.layout_slides()[0]);
    auto& slide2 = prs.slides()[1];
    std::vector<double> col_w = {200.0, 120.0, 120.0};
    std::vector<double> row_h = {40.0, 40.0, 40.0};
    auto& table = slide2.shapes().add_table(40, 40, col_w, row_h);

    std::vector<std::string> headers = {"Region", "Revenue", "Growth"};
    std::vector<std::vector<std::string>> data = {
        {"North", "$1.2M", "+8%"},
        {"South", "$0.9M", "+4%"},
    };
    for (size_t col = 0; col < headers.size(); ++col) {
        table.rows()[0][col].text_frame().set_text(headers[col]);
    }
    for (size_t r = 0; r < data.size(); ++r) {
        for (size_t c = 0; c < data[r].size(); ++c) {
            table.rows()[r + 1][c].text_frame().set_text(data[r][c]);
        }
    }

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

Common Issues and Fixes

Segfault or double-free on Presentation destruction

You are storing a reference or pointer to a slide or shape after the Presentation has been destroyed. All references returned by slides(), shapes(), etc. are invalidated when the Presentation destructor runs. Keep the Presentation alive for the entire duration you use its child objects.

SaveFormat::PPTX is not a function compiler error

SaveFormat::PPTX is an enum value, not a function. Use it as prs.save("file.pptx", asf::SaveFormat::PPTX).

Linker error: undefined reference to Aspose::Slides::Foss::Presentation

Ensure your CMakeLists.txt links against the library with target_link_libraries(my_app PRIVATE aspose-slides-foss).


Frequently Asked Questions

What is the default slide size?

A new Presentation() creates slides at the standard 10 x 7.5 inch (720 x 540 point) size. Changing the slide size is not yet supported in this edition.

Can I add more than one slide?

Yes. Call prs.slides().add_empty_slide(prs.layout_slides()[0]) to append a blank slide and access it by index:

prs.slides().add_empty_slide(prs.layout_slides()[0]);
auto& slide2 = prs.slides()[1];

Can I open an existing file and add slides?

Yes:

asf::Presentation prs("existing.pptx");
prs.slides().add_empty_slide(prs.layout_slides()[0]);
prs.save("existing.pptx", asf::SaveFormat::PPTX);

What formats can I save to?

Only SaveFormat::PPTX is supported. Export to PDF, HTML, SVG, or images is not available in this edition.


See Also