PowerPoint C# 中的文档属性

PowerPoint 文件中的文档属性或元数据用于识别演示文稿。此外,它们还提供有关演示文稿的其他信息,例如作者、标题、关键字、主题等。在本文中,您将学习如何使用 C# 以编程方式访问或修改 PowerPoint 文件中的属性。

C# API 访问/修改 PowerPoint PPT 中的属性

要访问或修改内置或自定义文档属性,我们将使用 Aspose.Slides for .NET。它是用于创建和操作 PowerPoint 和 OpenOffice 文档的强大 API。该 API 可作为 可下载的 DLL 以及 NuGet 获得。

PM> Install-Package Aspose.Slides.NET 

PowerPoint 演示文稿中的属性类型

PowerPoint 演示文稿支持两种类型的文档属性:内置和自定义。内置属性提供有关演示文稿的一般信息,例如标题、作者、主题等。而自定义属性由用户以键/值对的形式定义。以下部分演示如何添加、访问和修改属于上述每种类型的属性。

使用 C# 访问 PowerPoint PPT 中的内置属性

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

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

// 加载演示文稿
Presentation pres = new Presentation("AccessBuiltin Properties.pptx");

// 创建对与 Presentation 关联的 IDocumentProperties 对象的引用
IDocumentProperties documentProperties = pres.DocumentProperties;

// 显示内置属性
System.Console.WriteLine("Category : " + documentProperties.Category);
System.Console.WriteLine("Current Status : " + documentProperties.ContentStatus);
System.Console.WriteLine("Creation Date : " + documentProperties.CreatedTime);
System.Console.WriteLine("Author : " + documentProperties.Author);
System.Console.WriteLine("Description : " + documentProperties.Comments);
System.Console.WriteLine("KeyWords : " + documentProperties.Keywords);
System.Console.WriteLine("Last Modified By : " + documentProperties.LastSavedBy);
System.Console.WriteLine("Supervisor : " + documentProperties.Manager);
System.Console.WriteLine("Modified Date : " + documentProperties.LastSavedTime);
System.Console.WriteLine("Presentation Format : " + documentProperties.PresentationFormat);
System.Console.WriteLine("Last Print Date : " + documentProperties.LastPrinted);
System.Console.WriteLine("Is Shared between producers : " + documentProperties.SharedDoc);
System.Console.WriteLine("Subject : " + documentProperties.Subject);
System.Console.WriteLine("Title : " + documentProperties.Title);

使用 C# 修改 PowerPoint PPTX 中的内置属性

以下是使用 C# 修改 PowerPoint PPT 中内置属性值的步骤。

以下代码示例展示了如何在 C# 中修改 PowerPoint PPT 中的内置属性。

// 加载演示文稿
Presentation presentation = new Presentation("ModifyBuiltinProperties.pptx");

// 创建对与 Presentation 关联的 IDocumentProperties 对象的引用
IDocumentProperties documentProperties = presentation.DocumentProperties;

// 设置内置属性
documentProperties.Author = "Aspose.Slides for .NET";
documentProperties.Title = "Modifying Presentation Properties";
documentProperties.Subject = "Aspose Subject";
documentProperties.Comments = "Aspose Description";
documentProperties.Manager = "Aspose Manager";

// 将演示文稿保存到文件
presentation.Save("DocumentProperties_out.pptx", SaveFormat.Pptx);

使用 C# 在 PowerPoint PPT 中添加自定义属性

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

以下代码示例演示如何在 C# 中的 PowerPoint PPT 中添加自定义属性。

// 加载演示文稿
Presentation presentation = new Presentation("Presentation.pptx");

// 获取文档属性的参考
IDocumentProperties documentProperties = presentation.DocumentProperties;

// 添加自定义属性
documentProperties["New Custom"] = 12;
documentProperties["My Name"] = "Mudassir";
documentProperties["Custom"] = 124;

// 保存演示文稿
presentation.Save("CustomDocumentProperties_out.pptx", SaveFormat.Pptx);

使用 C# 访问 PowerPoint PPTX 中的自定义属性

以下步骤演示如何使用 C# 访问 PowerPoint 演示文稿中的自定义属性。

以下代码示例显示了如何访问 PowerPoint PPT 中的自定义属性。

// 加载演示文稿
Presentation presentation = new Presentation("Presentation.pptx");

// 获取文档属性的参考
IDocumentProperties documentProperties = presentation.DocumentProperties;

// 访问自定义属性
for (int i = 0; i < documentProperties.CountOfCustomProperties; i++)
{
    // 显示自定义属性的名称和值
    System.Console.WriteLine("Custom Property Name : " + documentProperties.GetCustomPropertyName(i));
    System.Console.WriteLine("Custom Property Value : " + documentProperties[documentProperties.GetCustomPropertyName(i)]);
}

使用 C# 修改 PowerPoint PPT 中的自定义属性

以下是在 C# 中修改 PowerPoint PPTX 中的自定义属性的步骤。

以下 C# 代码示例演示如何修改 PowerPoint PPTX 中的自定义属性。

// 加载演示文稿
Presentation presentation = new Presentation("Presentation.pptx");

// 获取文档属性的参考
IDocumentProperties documentProperties = presentation.DocumentProperties;

// 访问和修改自定义属性
for (int i = 0; i < documentProperties.CountOfCustomProperties; i++)
{
    // 修改自定义属性的值
    documentProperties[documentProperties.GetCustomPropertyName(i)] = "New Value " + (i + 1);
}

// 保存演示文稿
presentation.Save("CustomDocumentProperties_out.pptx", SaveFormat.Pptx);

C# .NET PowerPoint API - 获得免费许可证

您可以通过请求 临时许可证 来使用 Aspose.Slides for .NET,而不受评估限制。

处理 PPT 属性 - 在线演示

您还可以尝试使用 在线工具 来查看和编辑演示文稿中的文档属性,它基于 Aspose.Slides。

您可能还想试用 Aspose 免费的在线 PowerPoint 编辑器。

结论

在本文中,您学习了如何使用 C# 在 PowerPoint PPT/PPTX 中添加、访问和修改文档属性。本文已明确介绍了对内置和自定义文档属性的操作。此外,您可以访问 文档 以探索更多关于 Aspose.Slides for .NET 的信息。此外,您可以将您的查询发布到我们的 论坛

也可以看看