在 .NET 中打开演示文稿

概述

除了从零创建 PowerPoint 演示文稿,Aspose.Slides 还可以打开现有的演示文稿。加载演示文稿后,您可以获取其信息、编辑幻灯片内容、添加新幻灯片、删除已有幻灯片等。

打开演示文稿

要打开现有演示文稿,请实例化Presentation类,并将文件路径传递给其构造函数。

下面的 C# 示例展示了如何打开演示文稿并获取其幻灯片数量:

// 实例化 Presentation 类并将文件路径传递给其构造函数。
using (Presentation presentation = new Presentation("Sample.pptx"))
{
    // 打印演示文稿中的幻灯片总数。
    System.Console.WriteLine(presentation.Slides.Count);
}

打开受密码保护的演示文稿

当需要打开受密码保护的演示文稿时,请通过LoadOptions类的Password属性传入密码,以解密并加载演示文稿。以下 C# 代码演示了此操作:

LoadOptions loadOptions = new LoadOptions {Password = "YOUR_PASSWORD"};
using (Presentation presentation = new Presentation("Sample.pptx", loadOptions))
{
    // 对已解密的演示文稿执行操作。
}

打开大型演示文稿

Aspose.Slides 提供了选项——尤其是LoadOptions类中的BlobManagementOptions属性——帮助您加载大型演示文稿。

下面的 C# 代码演示了加载大型演示文稿(例如 2 GB)的方式:

const string filePath = "LargePresentation.pptx";

LoadOptions loadOptions = new LoadOptions
{
    BlobManagementOptions = 
    {
        // 选择 KeepLocked 行为——演示文稿文件将在整个生命周期内保持锁定
        // Presentation 实例,但它无需加载到内存或复制到临时文件。
        PresentationLockingBehavior = PresentationLockingBehavior.KeepLocked,
        IsTemporaryFilesAllowed = true,
        MaxBlobsBytesInMemory = 10 * 1024 * 1024 // 10 MB
    }
};

using (Presentation presentation = new Presentation(filePath, loadOptions))
{
    // 大型演示文稿已加载并可使用,同时内存消耗保持低水平。

    // 对演示文稿进行修改。
    presentation.Slides[0].Name = "Large presentation";

    // 将演示文稿保存到另一个文件。在此操作期间内存消耗仍保持低水平。
    presentation.Save("LargePresentation-copy.pptx", SaveFormat.Pptx);

    // 不要这样做!会抛出 I/O 异常,因为文件会被锁定,直到释放 Presentation 对象。
    File.Delete(filePath);
}

// 这里可以这样做。源文件已不再被 Presentation 对象锁定。
File.Delete(filePath);

控制外部资源

Aspose.Slides 提供了IResourceLoadingCallback接口,允许您管理外部资源。以下 C# 代码展示了如何使用IResourceLoadingCallback接口:

LoadOptions loadOptions = new LoadOptions();
loadOptions.ResourceLoadingCallback = new ImageLoadingHandler();

Presentation presentation = new Presentation("Sample.pptx", loadOptions);
public class ImageLoadingHandler : IResourceLoadingCallback
{
    public ResourceLoadingAction ResourceLoading(IResourceLoadingArgs args)
    {
        if (args.OriginalUri.EndsWith(".jpg"))
        {
            try
            {
                // 加载替代图像。
                byte[] imageData = File.ReadAllBytes("aspose-logo.jpg");
                args.SetData(imageData);
                return ResourceLoadingAction.UserProvided;
            }
            catch (Exception)
            {
                return ResourceLoadingAction.Skip;
            }
        }
        else if (args.OriginalUri.EndsWith(".png"))
        {
            // 设置替代 URL。
            args.Uri = "http://www.google.com/images/logos/ps_logo2.png";
            return ResourceLoadingAction.Default;
        }

        // 跳过所有其他图像。
        return ResourceLoadingAction.Skip;
    }
}

加载不含嵌入二进制对象的演示文稿

PowerPoint 演示文稿可能包含以下类型的嵌入二进制对象:

使用ILoadOptions.DeleteEmbeddedBinaryObjects属性,您可以在加载演示文稿时排除所有嵌入的二进制对象。

此属性对于移除潜在的恶意二进制内容非常有用。下面的 C# 代码演示了如何在不加载任何嵌入二进制内容的情况下加载演示文稿:

LoadOptions loadOptions = new LoadOptions()
{
	DeleteEmbeddedBinaryObjects = true
}

using (Presentation presentation = new Presentation("malware.ppt", loadOptions))
{
    // 对演示文稿执行操作。
}

FAQ

如何判断文件已损坏且无法打开?

加载期间会抛出解析/格式验证异常。此类错误通常会提及无效的 ZIP 结构或破损的 PowerPoint 记录。

打开时缺少必需的字体会怎样?

文件仍然会打开,但后续的渲染/导出可能会使用替代字体。请配置字体替代添加所需字体到运行时环境。

打开时嵌入的媒体(视频/音频)会怎样?

它们会作为演示文稿资源可用。如果媒体是通过外部路径引用,请确保这些路径在您的环境中可访问;否则渲染/导出可能会省略这些媒体。