快速入门
快速入门
本指南展示了从 CMake 设置到使用 aspose_slides_foss 为 C++ 保存 .pptx 文件的最快路径。该库采用 MIT 许可证,不需要 Microsoft Office,并且可以在任何配备 C++20 编译器和 CMake 3.20 或更高版本的平台上构建。
先决条件
| 要求 | 细节 |
|---|---|
| C++ 标准 | C++20 或更高 |
| CMake | 3.20 或更高 |
| OS | Windows、macOS、Linux |
| Package | aspose_slides_foss 通过 CMake FetchContent |
Install
在配置阶段使用 CMake FetchContent 拉取库。将以下内容添加到你的 CMakeLists.txt 中,然后链接到 aspose_slides_foss:
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)
target_link_libraries(your_target PRIVATE aspose_slides_foss)创建演示文稿
使用无参构造 Presentation 以创建空白演示文稿,然后使用路径和 SaveFormat::PPTX 调用 save()。库会自动添加一张空白幻灯片:
#include <Aspose/Slides/Foss/presentation.h>
#include <Aspose/Slides/Foss/export/save_format.h>
using namespace Aspose::Slides::Foss;
int main() {
Presentation prs;
prs.save("empty.pptx", SaveFormat::PPTX);
return 0;
}添加带文本的形状
通过 prs.slides()[0] 访问第一张幻灯片,使用 shapes().add_auto_shape() 插入一个矩形,并通过 text_frame()->set_text() 设置其文本:
#include <Aspose/Slides/Foss/auto_shape.h>
#include <Aspose/Slides/Foss/presentation.h>
#include <Aspose/Slides/Foss/shape_type.h>
#include <Aspose/Slides/Foss/text_frame.h>
#include <Aspose/Slides/Foss/export/save_format.h>
using namespace Aspose::Slides::Foss;
int main() {
Presentation prs;
auto& slide = prs.slides()[0];
auto& shape = slide.shapes().add_auto_shape(
ShapeType::RECTANGLE, 50, 50, 400, 150
);
shape.text_frame()->set_text("Hello from Aspose.Slides FOSS!");
prs.save("with_shape.pptx", SaveFormat::PPTX);
return 0;
}应用纯色填充
在形状上调用 fill_format().set_fill_type(FillType::SOLID) 并通过 solid_fill_color().set_color() 提供 ARGB 颜色,然后再调用 save():
#include <Aspose/Slides/Foss/auto_shape.h>
#include <Aspose/Slides/Foss/fill_format.h>
#include <Aspose/Slides/Foss/fill_type.h>
#include <Aspose/Slides/Foss/presentation.h>
#include <Aspose/Slides/Foss/shape_type.h>
#include <Aspose/Slides/Foss/export/save_format.h>
using namespace Aspose::Slides::Foss;
int main() {
Presentation prs;
auto& shape = prs.slides()[0].shapes().add_auto_shape(
ShapeType::RECTANGLE, 100, 100, 400, 200
);
shape.fill_format().set_fill_type(FillType::SOLID);
shape.fill_format().solid_fill_color().set_color(0xFF, 0x46, 0x82, 0xB4);
shape.text_frame()->set_text("Styled shape");
prs.save("styled.pptx", SaveFormat::PPTX);
return 0;
}加载现有文件
将文件路径字符串传递给 Presentation 构造函数,以加载现有的 .pptx 文件。根据需要修改演示文稿,然后调用 save() 写入输出:
#include <Aspose/Slides/Foss/presentation.h>
#include <Aspose/Slides/Foss/export/save_format.h>
using namespace Aspose::Slides::Foss;
int main() {
Presentation prs("existing.pptx");
prs.save("copy.pptx", SaveFormat::PPTX);
return 0;
}