Aspose.Slides使用教程:使用 C++ 访问或修改 PowerPoint 文件的属性
PowerPoint 文件包含提供有关演示文稿的附加信息的元数据或文档属性。其中包括演示文稿的标题、日期、作者等信息。在本文中,将学习 如何使用 C++ 访问和修改 PowerPoint 演示文稿中的属性。
如果有需要,可以进入慧都官网下载最新版Aspose.Slides for C++
用于访问和修改 PowerPoint 演示文稿属性的 C++ API
Aspose.Slides for C++ 是一个用于处理 PowerPoint 文件的 C++ API。它无需其他软件即可创建、阅读和更新 PowerPoint 文件。此外,API 允许访问和修改 PowerPoint 演示文稿的属性。
PowerPoint 演示文稿中的属性类型
PowerPoint 演示文稿中有两种类型的属性:内置和自定义。内置属性存储有关演示文稿的一般信息,如标题、日期等。另一方面,自定义属性以键/值对的形式存储自定义信息。以下部分介绍如何添加、访问和修改 PowerPoint 演示文稿的内置和自定义属性。
使用 C++ 访问 PowerPoint 演示文稿中的内置属性
以下是访问 PowerPoint 演示文稿中的内置属性的步骤。
首先,使用Presentation 类加载 PowerPoint 文件 。
使用Presentation->get_DocumentProperties()方法访问属性。
使用IDocumentProperties对象和IDocumentProperties->get_Category()、IDocumentProperties->get_Author()等方法读取各个属性。
以下示例代码显示了如何使用 C++ 访问 PowerPoint 演示文稿中的内置属性。
// File path const String sourceFilePath = u"SourceDirectory\Slides\SamplePresentation.pptx"; // Load the presentation file auto presentation = System::MakeObject<Presentation>(sourceFilePath); // Get reference of document properties System::SharedPtr<IDocumentProperties> documentProperties = presentation->get_DocumentProperties(); // Print the property values 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 演示文稿中的内置属性的步骤。
首先,使用Presentation 类加载 PowerPoint 文件 。
使用Presentation->get_DocumentProperties()方法检索IDocumentProperties对象中的属性。
使用IDocumentProperties对象和IDocumentProperties->set_Author(System::String value)、IDocumentProperties->set_Title(System::String value)等方法修改属性。
最后,使用 Presentation->Save(System::String fname, Export::SaveFormat format) 方法保存演示文稿。
以下示例代码显示了如何使用 C++ 修改内置 PowerPoint 属性。
// File paths const String sourceFilePath = u"SourceDirectory\Slides\SamplePresentation.pptx"; const String outputFilePath = u"OutputDirectory\ModifyBuiltinProperties_out.pptx"; // Load the presentation file auto presentation = System::MakeObject<Presentation>(sourceFilePath); // Get reference of document properties System::SharedPtr<IDocumentProperties> documentProperties = presentation->get_DocumentProperties(); // Modify the built-in properties 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"); // Save Presentation presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);复制代码
使用 C++ 在 PowerPoint 演示文稿中添加自定义属性
以下是在 PowerPoint 演示文稿中添加自定义属性的步骤。
首先,使用Presentation 类加载 PowerPoint 文件 。
使用Presentation->get_DocumentProperties()方法检索IDocumentProperties对象中的属性。
使用IDocumentProperties->idx_set(System::String name, System::SharedPtrSystem::Object value)方法添加自定义属性。
最后,使用 Presentation->Save(System::String fname, Export::SaveFormat format) 方法保存演示文稿。
以下示例代码显示了如何在 PowerPoint 演示文稿中添加自定义属性。
// File paths const String sourceFilePath = u"SourceDirectory\Slides\SamplePresentation.pptx"; const String outputFilePath = u"OutputDirectory\AddCustomProperties_out.pptx"; // Load the presentation file auto presentation = System::MakeObject<Presentation>(sourceFilePath); // Get reference of document properties auto documentProperties = presentation->get_DocumentProperties(); // Adding Custom properties 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)); // Getting property name at particular index String getPropertyName = documentProperties->GetCustomPropertyName(2); // Removing selected property documentProperties->RemoveCustomProperty(getPropertyName); // Save Presentation presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);复制代码
在 PowerPoint 演示文稿中访问和修改自定义属性
以下是访问和修改 PowerPoint 演示文稿中的自定义属性的步骤。
首先,使用Presentation 类加载 PowerPoint 文件 。
使用Presentation->get_DocumentProperties()方法检索IDocumentProperties对象中的属性。
循环遍历属性并分别使用IDocumentProperties->GetCustomPropertyName(int32_t index)和IDocumentProperties->idx_get(System::String name)方法访问每个属性的名称和值。
根据要存储的值的类型,使用IDocumentProperties->SetCustomPropertyValue()方法修改所需的自定义属性。
最后,使用 Presentation->Save(System::String fname, Export::SaveFormat format) 方法保存演示文稿。
以下示例代码显示了如何使用 C++ 访问和修改 PowerPoint 演示文稿中的自定义属性。
// File paths const String sourceFilePath = u"SourceDirectory\Slides\SamplePresentation.pptx"; const String outputFilePath = u"OutputDirectory\AccessAndModifyCustomProperties_out.pptx"; // Load the presentation file auto presentation = System::MakeObject<Presentation>(sourceFilePath); // Create a reference to DocumentProperties object associated with Presentation System::SharedPtr<IDocumentProperties> documentProperties = presentation->get_DocumentProperties(); // Access custom properties for (int32_t i = 0; i < documentProperties->get_CountOfCustomProperties(); i++) { // Print the name and value of custom properties 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))); // Modify the custom property documentProperties->SetCustomPropertyValue(documentProperties->GetCustomPropertyName(i), String::Format(u"Title : {0}", i)); } // Save Presentation presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
作者:Augenstern__zyx
链接:https://juejin.cn/post/7025442853987811364