在 C++ 中管理演示文稿属性

介绍

Aspose.Slides for C++ 支持 内置 和 自定义 文档属性。使用这些属性读取和更新演示文稿元数据,例如作者、标题和用户定义的值。

Aspose.Slides 允许您通过 IDocumentProperties 接口处理演示文稿文档属性。该接口的实例由 IPresentation::get_DocumentProperties 返回。以下示例展示了如何读取、修改和管理这些属性。

管理演示文稿属性

文档属性在演示文稿内容旁存储描述性信息。它们分为两类:

  • 内置属性 包含标准元数据,例如标题、作者和文档统计信息。
  • 自定义属性 存储用户定义的名称/值对。

Aspose.Slides for C++ 提供对两类属性的访问。在 PowerPoint 中,选择 文件 → 信息 → 属性 → 高级属性 检查文档元数据。对话框将信息分组到 摘要、统计、内容 和 自定义 等选项卡。使用 自定义 选项卡管理用户定义的属性。

读取加密演示文稿的公共属性

打开密码通常保护演示文稿内容和文档属性。当通过将 false 传递给 IProtectionManager::set_EncryptDocumentProperties 对演示文稿进行加密时,其文档属性保持公共。此时应用程序可以将 true 传递给 LoadOptions::set_OnlyLoadDocumentProperties 并在不提供打开密码的情况下读取公共元数据。

LoadOptions::set_OnlyLoadDocumentProperties 控制 Aspose.Slides 加载的内容;它不会解密任何内容。如果属性已被加密,则在没有密码的情况下加载会失败。如果演示文稿未加密,则该选项被忽略,完整的演示文稿将被加载。

该示例需要一个文档属性保持公共的加密演示文稿。它通过 IProtectionManager::get_IsOnlyDocumentPropertiesLoaded 验证加载模式,然后通过 IPresentation::get_DocumentProperties 读取内置属性:

#include <DOM/IDocumentProperties.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <DOM/LoadOptions.h>
#include <DOM/IProtectionManager.h>

using namespace Aspose::Slides;

auto loadOptions = System::MakeObject<LoadOptions>();
loadOptions->set_OnlyLoadDocumentProperties(true);

auto presentation = System::MakeObject<Presentation>(u"public-properties-encrypted.pptx", loadOptions);

if (presentation->get_ProtectionManager()->get_IsOnlyDocumentPropertiesLoaded())
{
    auto documentProperties = presentation->get_DocumentProperties();

    System::Console::WriteLine(u"Author: {0}", documentProperties->get_Author());
    System::Console::WriteLine(u"Title: {0}", documentProperties->get_Title());
    System::Console::WriteLine(u"Keywords: {0}", documentProperties->get_Keywords());
}
else
{
    System::Console::WriteLine(u"The presentation was not loaded in document-properties-only mode.");
}

在此模式下,幻灯片内容不会被加载。幻灯片、母版、布局、形状、媒体以及其他演示文稿对象均不可用。应用程序在执行需要完整演示文稿对象模型的操作之前,应始终检查 IProtectionManager::get_IsOnlyDocumentPropertiesLoaded。

更新加密演示文稿的属性

对于加密的 PPTX 文件,通过将 true 传递给 LoadOptions::set_OnlyLoadDocumentProperties 加载的演示文稿旨在读取公共元数据。Aspose.Slides 无法从该仅元数据对象保存更改的属性,因为公共属性必须与加密演示文稿内部的相应数据保持一致。因此,更新这些属性需要正确的打开密码并完整加载。

该示例需要一个具有公共文档属性且已知打开密码的加密 PPTX 演示文稿。它使用 LoadOptions::set_Password 打开完整演示文稿,更新公共内置属性并保存结果。随后使用 IPresentationInfo::get_IsEncrypted 验证加密仍然保留,并在不提供密码的情况下重新打开公共元数据以验证新值:

#include <DOM/IDocumentProperties.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <DOM/LoadOptions.h>
#include <DOM/IProtectionManager.h>
#include <DOM/PresentationFactory.h>
#include <DOM/IPresentationInfo.h>
#include <Export/SaveFormat.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;

const System::String inputPath = u"public-properties-encrypted.pptx";
const System::String outputPath = u"updated-public-properties-encrypted.pptx";

auto loadOptions = System::MakeObject<LoadOptions>();
loadOptions->set_Password(u"open_password");

auto presentation = System::MakeObject<Presentation>(inputPath, loadOptions);

auto documentProperties = presentation->get_DocumentProperties();
documentProperties->set_Title(u"Updated Product Roadmap");
documentProperties->set_Keywords(u"roadmap, planning, indexed");

presentation->Save(outputPath, SaveFormat::Pptx);

auto presentationInfo = PresentationFactory::get_Instance()->GetPresentationInfo(outputPath);
System::Console::WriteLine(u"The presentation is encrypted: {0}", presentationInfo->get_IsEncrypted());

auto metadataLoadOptions = System::MakeObject<LoadOptions>();
metadataLoadOptions->set_OnlyLoadDocumentProperties(true);

auto metadataPresentation = System::MakeObject<Presentation>(outputPath, metadataLoadOptions);

if (metadataPresentation->get_ProtectionManager()->get_IsOnlyDocumentPropertiesLoaded())
{
    System::Console::WriteLine(u"Title: {0}", metadataPresentation->get_DocumentProperties()->get_Title());
    System::Console::WriteLine(u"Keywords: {0}", metadataPresentation->get_DocumentProperties()->get_Keywords());
}
else
{
    System::Console::WriteLine(u"The presentation was not loaded in document-properties-only mode.");
}

如果应用程序不被允许解密或加载演示文稿内容,则必须将加密 PPTX 文件的公共属性视为只读。

访问内置属性

IDocumentProperties 接口通过类型化的 getter 公开内置元数据,包括 get_Author、get_Title 和 get_CreatedTime。此示例打开现有演示文稿并将其存储的元数据打印到控制台。值取决于输入的演示文稿;某些字段可能为空。

#include <DOM/IDocumentProperties.h>
#include <DOM/Presentation.h>
#include <system/console.h>

using namespace Aspose::Slides;

auto presentation = System::MakeObject<Presentation>(u"AccessBuiltInProperties.pptx");

auto documentProperties = presentation->get_DocumentProperties();

System::Console::WriteLine(u"Category: {0}", documentProperties->get_Category());
System::Console::WriteLine(u"Content status: {0}", documentProperties->get_ContentStatus());
System::Console::WriteLine(u"Creation date: {0}", documentProperties->get_CreatedTime());
System::Console::WriteLine(u"Author: {0}", documentProperties->get_Author());
System::Console::WriteLine(u"Comments: {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"Manager: {0}", documentProperties->get_Manager());
System::Console::WriteLine(u"Modified date: {0}", documentProperties->get_LastSavedTime());
System::Console::WriteLine(u"Presentation format: {0}", documentProperties->get_PresentationFormat());
System::Console::WriteLine(u"Last print date: {0}", documentProperties->get_LastPrinted());
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());

修改内置属性

要更新内置元数据,请使用由 IPresentation::get_DocumentProperties 返回的对象的 setter。此示例打开现有演示文稿,调用 set_Author、set_Title、set_Subject、set_Comments 和 set_Manager,然后使用 Presentation::Save 保存更新后的演示文稿。保存的 PPTX 包含新的元数据。

#include <DOM/IDocumentProperties.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <Export/SaveFormat.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;

auto presentation = System::MakeObject<Presentation>(u"ModifyBuiltInProperties.pptx");

auto documentProperties = presentation->get_DocumentProperties();

documentProperties->set_Author(u"Aspose.Slides for C++");
documentProperties->set_Title(u"Manage PowerPoint Presentation Properties");
documentProperties->set_Subject(u"Modify Built-in Properties");
documentProperties->set_Comments(u"Aspose description");
documentProperties->set_Manager(u"Aspose manager");

presentation->Save(u"DocumentProperties_output.pptx", SaveFormat::Pptx);

管理超链接基准

IDocumentProperties::get_HyperlinkBase 读取文档级别的相对超链接基准。当将演示文稿及其链接文件移动到其他位置时,这非常有用。它与单个超链接的 IHyperlink::get_ExternalUrl 分离:更改基准不会重写存储的链接地址或更改绝对目标。

该示例需要一个包含相对链接(例如 target.html)和绝对链接(例如 https://example.org/absolute.html)的演示文稿。它读取现有基准,使用 IDocumentProperties::set_HyperlinkBase 分配新的基准地址,并保存并重新打开演示文稿以检查该值。随后使用 u"" 清除基准,并在单独的保存文件中验证为空值。请将示例基准地址替换为您链接文件的位置。

#include <DOM/IDocumentProperties.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <Export/SaveFormat.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;

auto presentation = System::MakeObject<Presentation>(u"hyperlinks.pptx");

System::Console::WriteLine(u"Original base: {0}", presentation->get_DocumentProperties()->get_HyperlinkBase());

const System::String newBase = u"https://example.com/new/";
presentation->get_DocumentProperties()->set_HyperlinkBase(newBase);
presentation->Save(u"hyperlinks-updated.pptx", SaveFormat::Pptx);

auto updatedPresentation = System::MakeObject<Presentation>(u"hyperlinks-updated.pptx");
System::Console::WriteLine(u"Base preserved: {0}", (updatedPresentation->get_DocumentProperties()->get_HyperlinkBase() == newBase));

updatedPresentation->get_DocumentProperties()->set_HyperlinkBase(u"");
updatedPresentation->Save(u"hyperlinks-cleared.pptx", SaveFormat::Pptx);

auto clearedPresentation = System::MakeObject<Presentation>(u"hyperlinks-cleared.pptx");
System::Console::WriteLine(u"Base cleared: {0}", (clearedPresentation->get_DocumentProperties()->get_HyperlinkBase() == u""));

两个验证消息应打印 True。清除基准后,单个超链接地址保持完整。

当基准指向 old 文件夹时,指向 target.pptx 的相对链接会打开 old/target.pptx。将基准更改为 new 文件夹后,保存并重新打开演示文稿,同一链接会打开 new/target.pptx。绝对链接仍然打开相同的文件。在移动演示文稿或更改基准前后,请在 PowerPoint 中测试您的相对链接。

添加自定义演示文稿属性

自定义属性存储没有对应内置字段的元数据。IDocumentProperties::idx_set 允许您按属性名称分配值。此示例创建一个演示文稿,添加两个文本值和一个整数版本值,并使用 Presentation::Save 保存演示文稿。为已有名称分配值会替换该属性的值。

#include <DOM/IDocumentProperties.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;

auto presentation = System::MakeObject<Presentation>();

auto documentProperties = presentation->get_DocumentProperties();

documentProperties->idx_set(u"Reviewed by", System::ObjectExt::Box<System::String>(u"John Smith"));
documentProperties->idx_set(u"Confidentiality level", System::ObjectExt::Box<System::String>(u"Internal"));
documentProperties->idx_set(u"Document version", System::ObjectExt::Box(2));

presentation->Save(u"CustomDocumentProperties_output.pptx", SaveFormat::Pptx);

访问和修改自定义属性

此示例打开包含自定义属性的演示文稿,并使用 get_CountOfCustomProperties 进行遍历。GetCustomPropertyName 接受从零开始的索引并返回由 IDocumentProperties::idx_get 和 idx_set 使用的名称。示例打印每个原始名称和值,将每个值替换为带编号的字符串(不论其原始类型),并保存更改。如果没有自定义属性,循环将不做任何更改。

#include <DOM/IDocumentProperties.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;

auto presentation = System::MakeObject<Presentation>(u"AccessAndModifyProperties.pptx");

auto documentProperties = presentation->get_DocumentProperties();

for (auto i = 0; i < documentProperties->get_CountOfCustomProperties(); i++)
{
    auto propertyName = documentProperties->GetCustomPropertyName(i);
    auto propertyValue = documentProperties->idx_get(propertyName);

    System::Console::WriteLine(u"Custom property name: {0}", propertyName);
    System::Console::WriteLine(u"Custom property value: {0}", propertyValue);

    documentProperties->idx_set(propertyName, System::ObjectExt::Box(System::String::Format(u"New Value {0}", i + 1)));
}

presentation->Save(u"CustomProperties_output.pptx", SaveFormat::Pptx);

实时示例

尝试在线应用程序 View & Edit PowerPoint Metadata 了解如何使用 Aspose.Slides API 操作文档属性:

查看并编辑 PowerPoint 元数据

常见问题

如何从演示文稿中移除内置属性?

内置属性是演示文稿的整体组成部分,无法完全移除。但您可以更改其值,或者在该属性允许的情况下将其设为空。

如果添加已存在的自定义属性会怎样?

如果添加的自定义属性已存在,其现有值将被新值覆盖。无需事先删除或检查该属性,Aspose.Slides 会自动更新属性的值。

是否可以在不完整加载演示文稿的情况下访问演示文稿属性?

可以。使用 PresentationFactory::GetPresentationInfo 然后 IPresentationInfo::ReadDocumentProperties 在不创建 Presentation 实例的情况下读取存储的文档元数据。请参见 构建轻量级演示文稿清单 获取完整的报告示例和格式特定的限制。

是否可以在没有打开密码的情况下读取加密演示文稿的公共属性?

可以。演示文稿必须通过将 false 传递给 IProtectionManager::set_EncryptDocumentProperties 进行加密,并且必须通过将 true 传递给 LoadOptions::set_OnlyLoadDocumentProperties 加载。

是否可以在仅文档属性模式下更新加密的 PPTX 文件?

不可以。公共属性和加密属性数据必须保持一致,因此更新加密的 PPTX 文件需要使用正确的打开密码完整加载演示文稿。