How to Save Presentations in C++

How to Save Presentations in C++

Aspose.Slides FOSS for C++ saves presentations exclusively to .pptx format using prs.save(path, SaveFormat::PPTX). This guide covers the correct save pattern, saving to a different path, and common save-related errors.

Step-by-Step Guide

Step 1: Build and Link the Library

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: Open or Create a Presentation

Use stack allocation so the destructor cleans up automatically. Call save() before the object goes out of scope.

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

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

    // Create new
    {
        asf::Presentation prs;
        prs.save("new.pptx", asf::SaveFormat::PPTX);
    }

    // Open existing
    {
        asf::Presentation prs("input.pptx");
        prs.save("output.pptx", asf::SaveFormat::PPTX);
    }

    return 0;
}

Step 3: Save After All Modifications

Place the save() call after all modifications are complete, but before the Presentation goes out of scope.

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

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

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 300, 100);
    shape.add_text_frame("Hello, World!");
    prs.save("output.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Step 4: Save to a Different Path

Pass a different output path to create a new file without modifying the original:

asf::Presentation prs("template.pptx");
// modify ...
prs.save("customized.pptx", asf::SaveFormat::PPTX);

The template.pptx file is not modified; customized.pptx is created (or overwritten if it already exists).


Step 5: Verify the Output

After the save, check the file exists:

#include <filesystem>
#include <iostream>

auto output = std::filesystem::path("output.pptx");
std::cout << "Saved: " << std::filesystem::exists(output)
          << ", size: " << std::filesystem::file_size(output) << " bytes\n";

Supported Save Format

FormatEnum ValueSupported
PPTX (Office Open XML)SaveFormat::PPTXYes
PDFN/ANo
HTMLN/ANo
SVGN/ANo
PNG / JPEGN/ANo
ODP (OpenDocument)N/ANo

Only PPTX is supported. Attempting to save in any other format will throw an exception.


Common Issues and Fixes

Permission denied error on write

The output file is open in another application (e.g., PowerPoint has the file open). Close the file in other applications before saving.

File is created but appears empty or corrupted

Ensure prs.save() is called before the Presentation object is destroyed. After destruction, the internal state is released and subsequent calls will fail or produce invalid output.

Exception when saving

This occurs when attempting a save format other than PPTX, or when using an unsupported feature (such as charts or animations) during save.


Frequently Asked Questions

Can I save to the same file I opened?

Yes. Saving to the same path overwrites the original file:

asf::Presentation prs("deck.pptx");
// modify ...
prs.save("deck.pptx", asf::SaveFormat::PPTX); // overwrites original

Can I save to a memory buffer instead of a file?

Saving directly to a std::vector<uint8_t> or std::ostream is not supported in the current API. Save to a temporary file and read the bytes:

#include <filesystem>
#include <fstream>
#include <vector>

auto tmp = std::filesystem::temp_directory_path() / "temp.pptx";
prs.save(tmp.string(), asf::SaveFormat::PPTX);

std::ifstream in(tmp, std::ios::binary);
std::vector<uint8_t> bytes((std::istreambuf_iterator<char>(in)),
                            std::istreambuf_iterator<char>());
std::filesystem::remove(tmp);

Does saving preserve content I haven’t modified?

Yes. Unknown XML parts from the original file are preserved verbatim. The library only serializes the parts of the document model it understands, and passes through any XML it does not recognize.


See Also