Open a Presentation in JavaScript

Overview

Beyond creating PowerPoint presentations from scratch, Aspose.Slides also lets you open existing presentations. After loading a presentation, you can retrieve information about it, edit slide content, add new slides, remove existing ones, and more.

Open Presentations

To open an existing presentation, instantiate the Presentation class and pass the file path to its constructor.

The following JavaScript example shows how to open a presentation and get its slide count:

// Instantiate the Presentation class and pass a file path to its constructor.
let presentation = new aspose.slides.Presentation("Sample.pptx");
try {
    // Print the total number of slides in the presentation.
    console.log(presentation.getSlides().size());
} finally {
    presentation.dispose();
}

Open Password-Protected Presentations

When you need to open a password-protected presentation, pass the password through the setPassword method of the LoadOptions class to decrypt and load it. The following JavaScript code demonstrates this operation:

let loadOptions = new aspose.slides.LoadOptions();
loadOptions.setPassword("YOUR_PASSWORD");

let presentation = new aspose.slides.Presentation("Sample.pptx", loadOptions);
try {
    // Perform operations on the decrypted presentation.
} finally {
    presentation.dispose();
}

Open Large Presentations

Aspose.Slides provides options—particularly the getBlobManagementOptions method in the LoadOptions class—to help you load large presentations.

The following JavaScript code demonstrates loading a large presentation (for example, 2 GB):

const filePath = "LargePresentation.pptx";

let loadOptions = new aspose.slides.LoadOptions();
// Choose the KeepLocked behavior—the presentation file will remain locked for the lifetime of
// the Presentation instance, but it does not need to be loaded into memory or copied to a temporary file.
loadOptions.getBlobManagementOptions().setPresentationLockingBehavior(aspose.slides.PresentationLockingBehavior.KeepLocked);
loadOptions.getBlobManagementOptions().setTemporaryFilesAllowed(true);
loadOptions.getBlobManagementOptions().setMaxBlobsBytesInMemory(10 * 1024 * 1024); // 10 MB

let presentation = new aspose.slides.Presentation(filePath, loadOptions);
try {
    // The large presentation has been loaded and can be used, while memory consumption remains low.
    
    // Make changes to the presentation.
    presentation.getSlides().get_Item(0).setName("Large presentation");

    // Save the presentation to another file. Memory consumption remains low during this operation.
    presentation.save("LargePresentation-copy.pptx", aspose.slides.SaveFormat.Pptx);

    // Don't do this! An I/O exception will be thrown because the file is locked until the presentation object is disposed.
    //fs.unlinkSync(filePath);
} finally {
    presentation.dispose();
}

// It is OK to do it here. The source file is no longer locked by the presentation object.
fs.unlinkSync(filePath);

Control External Resources

Aspose.Slides provides the IResourceLoadingCallback interface that lets you manage external resources. The following JavaScript code shows how to use the IResourceLoadingCallback interface:

const ImageLoadingHandler = java.newProxy("com.aspose.slides.IResourceLoadingCallback", {
  resourceLoading: function(args) {
        if (args.getOriginalUri().endsWith(".jpg")) {
            try {
                // Load a substitute image.
                const imageData = fs.readFileSync("aspose-logo.jpg");
                args.setData(imageData);
                return aspose.slides.ResourceLoadingAction.UserProvided;
            } catch {
                return aspose.slides.ResourceLoadingAction.Skip;
            }
        } else if (args.getOriginalUri().endsWith(".png")) {
            // Set a substitute URL.
            args.setUri("http://www.google.com/images/logos/ps_logo2.png");
            return aspose.slides.ResourceLoadingAction.Default;
        }
        // Skip all other images.
        return aspose.slides.ResourceLoadingAction.Skip;
      }
});
let loadOptions = new aspose.slides.LoadOptions();
loadOptions.setResourceLoadingCallback(ImageLoadingHandler);

let presentation = new aspose.slides.Presentation("Sample.pptx", loadOptions);

Load Presentations Without Embedded Binary Objects

A PowerPoint presentation can contain the following types of embedded binary objects:

Using the LoadOptions.setDeleteEmbeddedBinaryObjects method, you can load a presentation without any embedded binary objects.

This method is useful for removing potentially malicious binary content. The following JavaScript code demonstrates how to load a presentation without any embedded binary content:

let loadOptions = new aspose.slides.LoadOptions();
loadOptions.setDeleteEmbeddedBinaryObjects(true);

let presentation = new aspose.slides.Presentation("malware.ppt", loadOptions);
try {
    // Perform operations on the presentation.
} finally {
    presentation.dispose();
}