How to Load Presentations in C++
Aspose.Slides FOSS for C++ lets you open any .pptx file, inspect its content, and either save it back to PPTX or extract data from it. This guide covers opening a file, iterating slides, reading shape text, and round-tripping the save.
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 an Existing Presentation
Pass the file path to the Presentation constructor. The destructor handles cleanup.
#include <Aspose/Slides/Foss/presentation.h>
#include <iostream>
int main() {
namespace asf = Aspose::Slides::Foss;
asf::Presentation prs("input.pptx");
std::cout << "Slide count: " << prs.slides().size() << "\n";
prs.save("output.pptx", asf::SaveFormat::PPTX);
return 0;
}Unknown XML parts in the source file are preserved verbatim: the library never removes content it does not yet understand.
Step 3: Inspect Slides
Iterate over all slides and print their shape count:
#include <Aspose/Slides/Foss/presentation.h>
#include <iostream>
int main() {
namespace asf = Aspose::Slides::Foss;
asf::Presentation prs("deck.pptx");
for (size_t i = 0; i < prs.slides().size(); ++i) {
auto& slide = prs.slides()[i];
std::cout << "Slide " << i << ": "
<< slide.shapes().size() << " shapes\n";
}
return 0;
}Step 4: Read Shape Text
Iterate over shapes and read text from shapes that have a TextFrame:
#include <Aspose/Slides/Foss/presentation.h>
#include <iostream>
int main() {
namespace asf = Aspose::Slides::Foss;
asf::Presentation prs("deck.pptx");
for (size_t i = 0; i < prs.slides().size(); ++i) {
auto& slide = prs.slides()[i];
for (size_t j = 0; j < slide.shapes().size(); ++j) {
auto& shape = slide.shapes()[j];
if (shape.has_text_frame()) {
auto text = shape.text_frame().text();
if (!text.empty()) {
std::cout << " Shape text: " << text << "\n";
}
}
}
}
return 0;
}Step 5: Read Document Properties
Access core document properties from prs.document_properties():
#include <Aspose/Slides/Foss/presentation.h>
#include <iostream>
int main() {
namespace asf = Aspose::Slides::Foss;
asf::Presentation prs("deck.pptx");
auto& props = prs.document_properties();
std::cout << "Title: " << props.title() << "\n";
std::cout << "Author: " << props.author() << "\n";
std::cout << "Subject: " << props.subject() << "\n";
return 0;
}Step 6: Round-Trip Save
After inspecting or modifying the presentation, save it back to PPTX:
prs.save("output.pptx", asf::SaveFormat::PPTX);Saving to a different path creates a new file. Saving to the same path overwrites the original.
Common Issues and Fixes
File not found or cannot open
Check that the path to the .pptx file is correct relative to the working directory. Use std::filesystem::path for robust path construction:
#include <filesystem>
auto path = std::filesystem::path(__FILE__).parent_path() / "assets" / "deck.pptx";
asf::Presentation prs(path.string());Exception: File format is not supported
The library supports .pptx (Office Open XML) only. Legacy .ppt (binary PowerPoint 97-2003) files are not supported.
Shape has no text_frame
Some shapes (Connectors, PictureFrames, GroupShapes) do not have a text frame. Guard with shape.has_text_frame() before accessing text.
Frequently Asked Questions
Does loading preserve all original content?
Yes. Unknown XML parts are preserved verbatim on round-trip save. The library only serializes the parts of the document model it understands, and passes through any XML it does not recognize.
Can I load a password-protected PPTX?
Password-protected (encrypted) presentations are not supported in this edition.
Can I extract embedded images?
Access the images collection: prs.images() returns the ImageCollection. Each image has a width(), height(), and binary_data() method to read the raw image data.
Is loading from an in-memory buffer supported?
Loading from a std::vector<uint8_t> or std::istream is not exposed in the current API. Write the bytes to a temporary file first, then pass the path to the Presentation constructor.