在 .NET 中检索和更新演示文稿信息

概述

Aspose.Slides 可以识别演示文稿的格式并读取其文档元数据,而无需创建完整的 Presentation 对象模型。这在需要对文件进行分类、建立清单或在决定是否加载和处理演示文稿内容之前检查属性时非常有用。

本文演示了通过 PresentationFactory 和 IPresentationInfo 进行轻量检查,以及通过 IDocumentProperties 进行有针对性的更新。

检查演示文稿格式

如果您已经加载了演示文稿,请参阅 Determine the Original Presentation Format 了解加载后检测以及传统 PPT、PPS、POT 流的限制。

使用 PresentationFactory.GetPresentationInfo 可在不创建 Presentation 实例的情况下检查文件。IPresentationInfo.LoadFormat 属性会报告检测到的格式,例如 PPTX、PPT 或 ODP。

using System;
using Aspose.Slides;

var fileNames = new[] { "pres.pptx", "pres.ppt", "pres.odp" };

foreach (var fileName in fileNames)
{
    var presentationInfo = PresentationFactory.Instance.GetPresentationInfo(fileName);
    Console.WriteLine($"{fileName}: {presentationInfo.LoadFormat}");
}

构建轻量演示文稿清单

当处理大量演示文稿文件时,您可能需要一个紧凑的清单用于验证、索引或文档管理系统。在这种情况下,使用 PresentationFactory.GetPresentationInfo 获取 IPresentationInfo 对象,然后调用 IPresentationInfo.ReadDocumentProperties 读取文档元数据。此方法不会创建 Presentation 实例,也不需要遍历完整的演示文稿对象模型。

IDocumentProperties 暴露的扩展属性提供以下清单值:

属性 清单值
Slides 幻灯片总数。
HiddenSlides 隐藏幻灯片的数量。
Notes 包含备注的幻灯片数量。
Paragraphs (若可用)段落总数。
Words 单词总数。
MultimediaClips 音频和视频剪辑的总数。

下面的示例在不创建 Presentation 对象的情况下读取这些值并打印紧凑的清单。它还将 HeadingPairs 与 TitlesOfParts 结合,以显示诸如字体、主题和幻灯片标题等内容组。

using System;
using System.IO;
using Aspose.Slides;

var filePath = "sample.pptx";
var presentationInfo = PresentationFactory.Instance.GetPresentationInfo(filePath);
var documentProperties = presentationInfo.ReadDocumentProperties();

Console.WriteLine($"File: {Path.GetFileName(filePath)}");
Console.WriteLine($"Format: {presentationInfo.LoadFormat}");
Console.WriteLine($"Title: {documentProperties.Title}");
Console.WriteLine($"Author: {documentProperties.Author}");
Console.WriteLine("Statistics:");
Console.WriteLine($"  Slides: {documentProperties.Slides}");
Console.WriteLine($"  Hidden slides: {documentProperties.HiddenSlides}");
Console.WriteLine($"  Slides with notes: {documentProperties.Notes}");
Console.WriteLine($"  Paragraphs: {documentProperties.Paragraphs}");
Console.WriteLine($"  Words: {documentProperties.Words}");
Console.WriteLine($"  Multimedia clips: {documentProperties.MultimediaClips}");

var headingPairs = documentProperties.HeadingPairs ?? Array.Empty<IHeadingPair>();
var titlesOfParts = documentProperties.TitlesOfParts ?? Array.Empty<string>();
var partIndex = 0;

if (headingPairs.Length == 0 || titlesOfParts.Length == 0)
{
    Console.WriteLine("Content groups: not available");
}
else
{
    Console.WriteLine("Content groups:");

    foreach (var headingPair in headingPairs)
    {
        Console.WriteLine($"  {headingPair.Name} ({headingPair.Count})");

        for (var partOffset = 0; partOffset < headingPair.Count && partIndex < titlesOfParts.Length; partOffset++)
        {
            Console.WriteLine($"    - {titlesOfParts[partIndex]}");
            partIndex++;
        }
    }

    if (partIndex < titlesOfParts.Length)
    {
        Console.WriteLine("  Other parts:");

        while (partIndex < titlesOfParts.Length)
        {
            Console.WriteLine($"    - {titlesOfParts[partIndex]}");
            partIndex++;
        }
    }
}

每个 IHeadingPair 提供组名及该组中项目的数量。IDocumentProperties.TitlesOfParts 是一个平坦且有序的数组,因此需要按每个标题对指定的连续标题数量进行消费。

存储的元数据和格式限制

IPresentationInfo.ReadDocumentProperties 返回的清单属性反映了源文档中可用的元数据。Aspose.Slides 不会加载并遍历演示文稿对象模型来重新计算这些值。缺失的属性会使用默认值表示,如果上一次保存文件的应用程序未更新文档属性,存储的值可能已过时。

  • PPTX: 该格式提供幻灯片、备注、隐藏幻灯片、段落、单词和多媒体计数的扩展文档属性,以及标题对和部件标题。可用性取决于文档生成者写入了哪些属性。
  • PPT: 二进制格式可以存储相应的文档摘要属性。如果属性缺失或未由文档生成者刷新,Aspose.Slides 将返回其存储的或默认值,而不是从幻灯片计算得出。
  • ODP: OpenDocument 元数据提供一般文档统计信息,例如页面、段落和单词计数,但这些值并不映射到每个 PowerPoint 特有的扩展属性。隐藏幻灯片、备注幻灯片、多媒体、标题对和部件标题的元数据可能不可用,清单属性可能返回默认值。请勿将零值或空数组视为对应内容不存在的权威证明。

在需要清单和初步检查时使用轻量元数据方法。当结果必须反映内存中的更改或需要验证实际演示文稿内容时,请加载演示文稿并检查其实时对象模型。

更新演示文稿属性

IPresentationInfo.ReadDocumentProperties 返回的属性也可以在不创建 Presentation 实例的情况下进行更改。使用 IPresentationInfo.UpdateDocumentProperties 应用更改,然后使用 IPresentationInfo.WriteBindedPresentation 将绑定的演示文稿写回。

以下图像显示了原始文档属性。

PowerPoint 演示文稿的原始文档属性

以下示例更改标题和最后保存时间并将结果写入新文件:

using System;
using System.IO;
using Aspose.Slides;

var sourceFile = "sample.pptx";
var outputFile = "sample_with_updated_properties.pptx";
var presentationInfo = PresentationFactory.Instance.GetPresentationInfo(sourceFile);
var documentProperties = presentationInfo.ReadDocumentProperties();

documentProperties.Title = "Quarterly sales report";
documentProperties.LastSavedTime = DateTime.UtcNow;

presentationInfo.UpdateDocumentProperties(documentProperties);
using var outputStream = File.Create(outputFile);
presentationInfo.WriteBindedPresentation(outputStream);

以下图像显示了更新后的文档属性。

PowerPoint 演示文稿的已更改文档属性

有用的链接

有关相关安全检查和保护设置,请参阅以下文章:

常见问题解答

如何检查是否嵌入了字体以及具体是哪一些?

加载演示文稿并使用 Presentation.FontsManager。调用 FontsManager.GetEmbeddedFonts 获取嵌入的字体,调用 FontsManager.GetFonts 获取演示文稿使用的字体。比较两者即可找出需要渲染但未嵌入的字体。

如何快速判断文件是否包含隐藏幻灯片以及数量?

当存储的文档元数据足够时,通过 PresentationFactory.GetPresentationInfo 和 IPresentationInfo.ReadDocumentProperties 读取 IDocumentProperties.HiddenSlides。这适用于轻量清单。如果演示文稿已在内存中修改,存储的元数据可能缺失或已过时,或者需要验证实时值,则遍历 Presentation.Slides 并检查每个幻灯片的 Slide.Hidden 属性。

我能否检测自定义幻灯片尺寸和方向是否被使用,以及它们是否与默认值不同?

可以。加载演示文稿并读取 Presentation.SlideSize。检查 ISlideSize.Type、ISlideSize.Size 和 ISlideSize.Orientation 以将当前设置与预设和尺寸进行比较。

是否有快速方法查看图表是否引用外部数据源?

有。定位每个 Chart 并检查 ChartData.DataSourceType。对于外部工作簿,读取 ChartData.ExternalWorkbookPath。数据源类型和路径可标识外部引用,但是否可用需要单独的资源检查。

我如何评估可能导致渲染或 PDF 导出变慢的“重”幻灯片?

没有单一的复杂度属性。遍历 Presentation.Slides 并检查每个幻灯片的 IBaseSlide.Shapes 集合。使用形状数量以及大图片、特效、动画或多媒体的存在作为筛选信号,并在将幻灯片标记为确认的性能瓶颈前进行代表性的渲染或导出测量。