使用 C++ 访问或修改 PowerPoint 文件的属性

PowerPoint 文件包含提供有关演示文稿的其他信息的元数据或文档属性。其中包括演示文稿的标题、日期、作者等信息。在本文中,您将学习如何使用 C++ 访问和修改 PowerPoint 演示文稿中的属性。

用于访问和修改 PowerPoint 演示文稿属性的 C++ API

Aspose.Slides for C++ 是一个用于处理 PowerPoint 文件的 C++ API。它使您无需其他软件即可创建、阅读和更新 PowerPoint 文件。此外,API 允许您访问和修改 PowerPoint 演示文稿的属性。您可以通过 NuGet 安装 API,也可以直接从 下载 部分下载。

PM> Install-Package Aspose.Slides.Cpp

PowerPoint 演示文稿中的属性类型

PowerPoint 演示文稿中有两种类型的属性:内置和自定义。内置属性存储有关演示文稿的一般信息,如标题、日期等。另一方面,自定义属性将自定义信息存储在键/值对中。以下部分介绍如何添加、访问和修改 PowerPoint 演示文稿的内置和自定义属性。

使用 C++ 访问 PowerPoint 演示文稿中的内置属性

以下是访问 PowerPoint 演示文稿中的内置属性的步骤。

以下示例代码显示如何使用 C++ 访问 PowerPoint 演示文稿中的内置属性。

// 文件路径
const String sourceFilePath = u"SourceDirectory\\Slides\\SamplePresentation.pptx";

// 加载演示文件
auto presentation = System::MakeObject<Presentation>(sourceFilePath);

// 获取文档属性的参考
System::SharedPtr<IDocumentProperties> documentProperties = presentation->get_DocumentProperties();

// 打印属性值
System::Console::WriteLine(u"Category : {0}", documentProperties->get_Category());
System::Console::WriteLine(u"Current Status : {0}", documentProperties->get_ContentStatus());
System::Console::WriteLine(u"Creation Date : {0}", documentProperties->get_CreatedTime().ToString());
System::Console::WriteLine(u"Author : {0}", documentProperties->get_Author());
System::Console::WriteLine(u"Description : {0}", documentProperties->get_Comments());
System::Console::WriteLine(u"KeyWords : {0}", documentProperties->get_Keywords());
System::Console::WriteLine(u"Last Modified By : {0}", documentProperties->get_LastSavedBy());
System::Console::WriteLine(u"Supervisor : {0}", documentProperties->get_Manager());
System::Console::WriteLine(u"Modified Date : {0}", documentProperties->get_LastSavedTime().ToString());
System::Console::WriteLine(u"Presentation Format : {0}", documentProperties->get_PresentationFormat());
System::Console::WriteLine(u"Last Print Date : {0}", documentProperties->get_LastPrinted().ToString());
System::Console::WriteLine(u"Is Shared between producers : {0}", documentProperties->get_SharedDoc());
System::Console::WriteLine(u"Subject : {0}", documentProperties->get_Subject());
System::Console::WriteLine(u"Title : {0}", documentProperties->get_Title());

使用 C++ 修改 PowerPoint 演示文稿中的内置属性

以下是在 PowerPoint 演示文稿中修改内置属性的步骤。

以下示例代码显示如何使用 C++ 修改内置 PowerPoint 属性。

// 文件路径
const String sourceFilePath = u"SourceDirectory\\Slides\\SamplePresentation.pptx";
const String outputFilePath = u"OutputDirectory\\ModifyBuiltinProperties_out.pptx";

// 加载演示文件
auto presentation = System::MakeObject<Presentation>(sourceFilePath);

// 获取文档属性的参考
System::SharedPtr<IDocumentProperties> documentProperties = presentation->get_DocumentProperties();

// 修改内置属性
documentProperties->set_Author(u"Aspose.Slides for C++");
documentProperties->set_Title(u"Modifying Presentation Properties");
documentProperties->set_Subject(u"Aspose Subject");
documentProperties->set_Comments(u"Aspose Comments");
documentProperties->set_Manager(u"Aspose Manager");

// 保存演示文稿
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

使用 C++ 在 PowerPoint 演示文稿中添加自定义属性

以下是在 PowerPoint 演示文稿中添加自定义属性的步骤。

以下示例代码显示如何在 PowerPoint 演示文稿中添加自定义属性。

// 文件路径
const String sourceFilePath = u"SourceDirectory\\Slides\\SamplePresentation.pptx";
const String outputFilePath = u"OutputDirectory\\AddCustomProperties_out.pptx";

// 加载演示文件
auto presentation = System::MakeObject<Presentation>(sourceFilePath);

// 获取文档属性的参考
auto documentProperties = presentation->get_DocumentProperties();

// 添加自定义属性
documentProperties->idx_set(u"New Custom", ObjectExt::Box<int32_t>(12));
documentProperties->idx_set(u"My Name", ObjectExt::Box<String>(u"Aspose"));
documentProperties->idx_set(u"Custom", ObjectExt::Box<int32_t>(124));

// 在特定索引处获取属性名称
String getPropertyName = documentProperties->GetCustomPropertyName(2);

// 删除选定的属性
documentProperties->RemoveCustomProperty(getPropertyName);

// 保存演示文稿
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

访问和修改 PowerPoint 演示文稿中的自定义属性

以下是在 PowerPoint 演示文稿中访问和修改自定义属性的步骤。

以下示例代码显示如何使用 C++ 访问和修改 PowerPoint 演示文稿中的自定义属性。

// 文件路径
const String sourceFilePath = u"SourceDirectory\\Slides\\SamplePresentation.pptx";
const String outputFilePath = u"OutputDirectory\\AccessAndModifyCustomProperties_out.pptx";

// 加载演示文件
auto presentation = System::MakeObject<Presentation>(sourceFilePath);

// 创建对与 Presentation 关联的 DocumentProperties 对象的引用
System::SharedPtr<IDocumentProperties> documentProperties = presentation->get_DocumentProperties();

// 访问自定义属性
for (int32_t i = 0; i < documentProperties->get_CountOfCustomProperties(); i++)
{
	// 打印自定义属性的名称和值
	System::Console::WriteLine(u"Custom Property Name : {0}", documentProperties->GetCustomPropertyName(i));
	System::Console::WriteLine(u"Custom Property Value : {0}", documentProperties->idx_get(documentProperties->GetCustomPropertyName(i)));

	// 修改自定义属性
	documentProperties->SetCustomPropertyValue(documentProperties->GetCustomPropertyName(i), String::Format(u"Title : {0}", i));
}

// 保存演示文稿
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

获得免费许可证

为了在没有评估限制的情况下试用 API,您可以申请 免费的临时许可证

结论

在本文中,您学习了如何访问和修改 PowerPoint 演示文稿中的内置属性。此外,您还了解了如何使用 Aspose.Slides for C++ API 添加、访问和修改自定义 PowerPoint 属性。它是一个强大的 API,具有许多用于自动执行与 PowerPoint 相关的任务的附加功能。您可以通过访问 官方文档 来详细探索 API。如有任何疑问,请随时通过我们的 免费支持论坛 与我们联系。

也可以看看