如何在 C++ 中创建演示文稿
Aspose.Slides FOSS for C++ 让您完全使用 C++ 创建 PowerPoint 演示文稿,无需依赖 Microsoft Office。本指南展示了如何创建新演示文稿、添加幻灯片和形状、格式化文本以及保存结果。
分步指南
步骤 1:将库添加到您的 CMake 项目
使用 CMake FetchContent 将库直接集成自 GitHub。需要 C++20 或更高版本。
cmake_minimum_required(VERSION 3.20)
project(my_slides_app LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FetchContent)
FetchContent_Declare(
aspose_slides_foss
GIT_REPOSITORY https://github.com/aspose-slides-foss/Aspose.Slides-FOSS-for-Cpp.git
GIT_TAG main
)
FetchContent_MakeAvailable(aspose_slides_foss)
add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE aspose_slides_foss)不需要其他系统软件包。
步骤 2:包含所需的头文件
#include <Aspose/Slides/Foss/presentation.h>所有类型都位于 Aspose::Slides::Foss 命名空间。为简洁起见,下面的示例使用了命名空间别名:
namespace asf = Aspose::Slides::Foss;步骤 3:创建演示文稿
在堆栈上构建一个 Presentation。新演示文稿以一个空白幻灯片开始。
#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;
}重要: 使用栈分配或 std::unique_ptr,以便析构函数自动释放内部资源。
步骤 4:访问幻灯片
第一张幻灯片的索引是 0。空白演示文稿恰好有一张幻灯片。
namespace asf = Aspose::Slides::Foss;
asf::Presentation prs;
auto& slide = prs.slides()[0]; // zero-based index
prs.save("output.pptx", asf::SaveFormat::PPTX);步骤 5:添加形状
使用 slide.shapes().add_auto_shape() 添加 AutoShape。参数为 (shape_type, x, y, width, height),全部以点为单位(1 点 = 1/72 英寸;标准幻灯片为 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);
// Set text on the shape's text frame
shape.text_frame()->set_text("Hello from Aspose.Slides FOSS!");
prs.save("with-shape.pptx", asf::SaveFormat::PPTX);
return 0;
}第6步:保存演示文稿
在 Presentation 超出作用域之前调用 prs.save(path, SaveFormat::PPTX)。PPTX 是唯一受支持的输出格式。
prs.save("result.pptx", asf::SaveFormat::PPTX);文件以原子方式写入;如果在此调用之前发生错误,则不会创建输出文件。
完整工作示例
以下程序创建了一个包含两张幻灯片的演示文稿,第一张幻灯片上有标题形状,第二张幻灯片上有表格。
#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);
title.text_frame()->set_text("Q1 Results: Executive Summary");
auto& fmt = title.text_frame()->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;
}常见问题及解决方案
Segfault 或 double-free 在 Presentation 销毁时
在 Presentation 被销毁后,您仍在存储对幻灯片或形状的引用或指针。当 Presentation 析构函数运行时,slides()、shapes() 等返回的所有引用都会失效。请在使用其子对象的整个期间保持 Presentation 存活。
SaveFormat::PPTX is not a function 编译错误
SaveFormat::PPTX 是一个枚举值,而不是函数。请将其用作 prs.save("file.pptx", asf::SaveFormat::PPTX)。
链接错误:未定义对 Aspose::Slides::Foss::Presentation 的引用
确保您的 CMakeLists.txt 链接到带有 target_link_libraries(my_app PRIVATE aspose_slides_foss) 的库。
常见问题
默认幻灯片尺寸是多少?
新的 Presentation() 会创建标准 10 x 7.5 英寸(720 x 540 点)尺寸的幻灯片。此版本尚不支持更改幻灯片尺寸。
我可以添加多个幻灯片吗?
是的。调用 prs.slides().add_empty_slide(&prs.layout_slides()[0]) 来追加一个空白幻灯片并通过索引访问它:
prs.slides().add_empty_slide(&prs.layout_slides()[0]);
auto& slide2 = prs.slides()[1];我可以打开现有文件并添加幻灯片吗?
是:
asf::Presentation prs("existing.pptx");
prs.slides().add_empty_slide(&prs.layout_slides()[0]);
prs.save("existing.pptx", asf::SaveFormat::PPTX);我可以保存为哪些格式?
仅支持 SaveFormat::PPTX。此版本不提供导出为 PDF、HTML、SVG 或图像的功能。