在 Java 中打开演示文稿

简介

Aspose.Slides for Java 可以从文件和流中加载 PowerPoint 和 OpenDocument 演示文稿。加载演示文稿后,您可以检查其结构、编辑幻灯片、管理资源,并以原始格式或其他受支持的格式保存。

可以通过 LoadOptions 类自定义加载行为。例如,您可以提供打开密码、将大型二进制对象保存在 Java 堆外、控制外部资源,或省略嵌入的二进制数据。

打开演示文稿

加载文件或流后,您可以确定其原始演示文稿格式以选择应用程序的处理方式。

要打开现有演示文稿,请将其文件路径传递给 Presentation 构造函数。使用完毕后释放演示文稿,以便及时释放文件句柄、临时数据和其他资源。

下面的 Java 示例演示如何打开演示文稿并获取其幻灯片计数:

import com.aspose.slides.Presentation;

Presentation presentation = new Presentation("sample.pptx");
try {
    System.out.println("Slide count: " + presentation.getSlides().size());
} finally {
    presentation.dispose();
}

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

打开密码会对演示文稿内容进行加密。要加载完整的演示文稿,请将正确的密码传递给 LoadOptions.setPassword,并将该选项提供给 Presentation 构造函数。当密码缺失或不正确时,加载将失败。

import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;

LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("open_password");

Presentation presentation = new Presentation("encrypted-presentation.pptx", loadOptions);
try {
    System.out.println("Slide count: " + presentation.getSlides().size());
} finally {
    presentation.dispose();
}

有关密码检测、验证和加密工作流,请参阅Password-Protect Presentations。若加密的演示文稿故意以公共文档属性保存,则可以在不提供密码的情况下读取这些属性;请参阅Manage Presentation Properties

打开大型演示文稿

LoadOptions.getBlobManagementOptions 返回用于控制 Aspose.Slides 如何处理图像、音频和视频等二进制大对象 (BLOB) 的选项。您可以保持源文件锁定、允许使用临时文件,并限制保留在内存中的 BLOB 数据量。

下面的 Java 代码演示加载一个大型演示文稿(例如 2 GB):

import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.PresentationLockingBehavior;
import com.aspose.slides.SaveFormat;

final String filePath = "large-presentation.pptx";

LoadOptions loadOptions = new LoadOptions();
loadOptions.getBlobManagementOptions().setPresentationLockingBehavior(PresentationLockingBehavior.KeepLocked);
loadOptions.getBlobManagementOptions().setTemporaryFilesAllowed(true);
loadOptions.getBlobManagementOptions().setMaxBlobsBytesInMemory(10 * 1024 * 1024);

Presentation presentation = new Presentation(filePath, loadOptions);
try {
    presentation.getSlides().get_Item(0).setName("Large presentation");
    presentation.save("large-presentation-copy.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

控制外部资源

LoadOptions.setResourceLoadingCallback 接受 IResourceLoadingCallback 实现。回调可提供替代数据、重定向资源、使用默认加载器或跳过资源。当演示文稿包含需根据应用程序特定安全或存储规则解析的外部图像时,此功能非常有用。

import com.aspose.slides.IResourceLoadingArgs;
import com.aspose.slides.IResourceLoadingCallback;
import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.ResourceLoadingAction;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Locale;

class ImageLoadingHandler implements IResourceLoadingCallback {
    public int resourceLoading(IResourceLoadingArgs args) {
        boolean isJpeg = args.getOriginalUri().toLowerCase(Locale.ROOT).endsWith(".jpg");
        Path approvedImagePath = Paths.get("approved-image.jpg");
        if (!isJpeg || !Files.exists(approvedImagePath)) {
            return ResourceLoadingAction.Skip;
        }

        try {
            byte[] imageData = Files.readAllBytes(approvedImagePath);
            args.setData(imageData);
            return ResourceLoadingAction.UserProvided;
        } catch (IOException exception) {
            System.err.println("The approved replacement image could not be read.");
            return ResourceLoadingAction.Skip;
        }
    }
}

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

Presentation presentation = new Presentation("presentation-with-external-images.pptx", loadOptions);
try {
    System.out.println("Slide count: " + presentation.getSlides().size());
} finally {
    presentation.dispose();
}

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

演示文稿可能包含应用程序不需要或不想保留的嵌入二进制数据。示例包括:

LoadOptions.setDeleteEmbeddedBinaryObjects 设置为 true,即可在加载时删除这些二进制数据。保存加载后的演示文稿以保留已清理的结果。

此选项可降低不需要的嵌入式负载的风险,但它并非完整的恶意软件检测或内容清理系统。

import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

LoadOptions loadOptions = new LoadOptions();
loadOptions.setDeleteEmbeddedBinaryObjects(true);

Presentation presentation = new Presentation("presentation-with-embedded-data.pptx", loadOptions);
try {
    presentation.save("presentation-without-embedded-data.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

常见问题

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

Aspose.Slides 在加载期间会抛出解析或格式异常。请将此类失败与密码错误错误区分处理,以便应用程序能够准确报告原因。

如果缺少必需的字体会怎样?

演示文稿仍然可以加载,但渲染和导出时可能会替代字体。您可以配置字体替代提供自定义字体以使输出更可预测。

加载演示文稿时是否也会加载其嵌入的媒体?

嵌入的音频和视频可通过演示文稿对象模型访问。外部资源将根据配置的资源加载行为进行解析,如果其位置无法访问,则可能不可用。