Restrict Presentation Edits with Shape Locks in PHP

Overview

A common use for Aspose.Slides for PHP via Java is to create, update, and save PowerPoint (PPTX) presentations as part of an automated workflow. When users receive these presentations, restricting changes to shapes can help preserve the intended layout and content.

This article explains how shapes are organized on slides and how to apply and remove shape locks in a PPTX presentation. These locks let you restrict specific editing operations on individual shapes.

Composition of a Slide

A presentation slide is composed of components such as autoshapes, tables, OLE objects, grouped shapes, picture frames, video frames, connectors, and other elements used to build a presentation. In Aspose.Slides for PHP via Java, each element on a slide is represented by an object that inherits from the Shape class.

Different shape types expose different locking options. The BaseShapeLock class is the base class for shape locks. Aspose.Slides for PHP via Java provides the following lock classes:

To apply locks across a Presentation, iterate through its slides and set the appropriate locks on each supported shape.

Apply and Remove Protection

Shape locks restrict operations such as selecting, moving, and resizing shapes. They do not make the entire presentation uneditable.

Apply Protection to PPTX Shapes

Aspose.Slides for PHP via Java provides the Shape class to work with shapes on a slide.

The example calls setSelectLocked, setPositionLocked, and setSizeLocked with true for autoshapes, with equivalent locks for group shapes, connectors, and picture frames. Connectors use setPositionMove for movement; groups also use setGroupingLocked to restrict grouping.

The example loads an existing PPTX presentation, checks the shapes in each slide’s shape collection, and saves a copy with these restrictions applied. Shapes outside the four handled types are left unchanged. The loop does not traverse the child shapes inside a group.

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("Sample.pptx");
try {
    foreach ($presentation->getSlides()->toArray() as $slide) {
        foreach ($slide->getShapes()->toArray() as $shape) {
            if (java_instanceof($shape, new JavaClass("com.aspose.slides.AutoShape"))) {
                $shapeLock = $shape->getShapeLock();
                $shapeLock->setPositionLocked(true);
                $shapeLock->setSelectLocked(true);
                $shapeLock->setSizeLocked(true);
            } elseif (java_instanceof($shape, new JavaClass("com.aspose.slides.GroupShape"))) {
                $shapeLock = $shape->getShapeLock();
                $shapeLock->setGroupingLocked(true);
                $shapeLock->setPositionLocked(true);
                $shapeLock->setSelectLocked(true);
                $shapeLock->setSizeLocked(true);
            } elseif (java_instanceof($shape, new JavaClass("com.aspose.slides.Connector"))) {
                $shapeLock = $shape->getShapeLock();
                $shapeLock->setPositionMove(true);
                $shapeLock->setSelectLocked(true);
                $shapeLock->setSizeLocked(true);
            } elseif (java_instanceof($shape, new JavaClass("com.aspose.slides.PictureFrame"))) {
                $shapeLock = $shape->getShapeLock();
                $shapeLock->setPositionLocked(true);
                $shapeLock->setSelectLocked(true);
                $shapeLock->setSizeLocked(true);
            }
        }
    }

    $presentation->save("ProtectedSample.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

Remove Protection

To remove a shape lock, set its value to false. The following example loads a presentation with shape locks and clears the selection, movement, and resizing restrictions through AutoShapeLock, GroupShapeLock, ConnectorLock, and PictureFrameLock. It also removes the grouping restriction by passing false to GroupShapeLock::setGroupingLocked and saves a copy of the presentation. Other lock settings are left unchanged.

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("ProtectedSample.pptx");
try {
    foreach ($presentation->getSlides()->toArray() as $slide) {
        foreach ($slide->getShapes()->toArray() as $shape) {
            if (java_instanceof($shape, new JavaClass("com.aspose.slides.AutoShape"))) {
                $shapeLock = $shape->getShapeLock();
                $shapeLock->setPositionLocked(false);
                $shapeLock->setSelectLocked(false);
                $shapeLock->setSizeLocked(false);
            } elseif (java_instanceof($shape, new JavaClass("com.aspose.slides.GroupShape"))) {
                $shapeLock = $shape->getShapeLock();
                $shapeLock->setGroupingLocked(false);
                $shapeLock->setPositionLocked(false);
                $shapeLock->setSelectLocked(false);
                $shapeLock->setSizeLocked(false);
            } elseif (java_instanceof($shape, new JavaClass("com.aspose.slides.Connector"))) {
                $shapeLock = $shape->getShapeLock();
                $shapeLock->setPositionMove(false);
                $shapeLock->setSelectLocked(false);
                $shapeLock->setSizeLocked(false);
            } elseif (java_instanceof($shape, new JavaClass("com.aspose.slides.PictureFrame"))) {
                $shapeLock = $shape->getShapeLock();
                $shapeLock->setPositionLocked(false);
                $shapeLock->setSelectLocked(false);
                $shapeLock->setSizeLocked(false);
            }
        }
    }

    $presentation->save("RemovedProtectionSample.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

Conclusion

Aspose.Slides for PHP via Java lets you restrict specific editing operations on individual shapes or apply the same restrictions across slides. Choose the locks supported by each shape type, and set their values to false when you need to remove those restrictions.

FAQ

Can I combine shape locks and password protection in the same presentation?

Yes. Shape locks restrict specific editing operations, while password protection can restrict opening or modifying the presentation. You can use both in the same file.

Can I restrict editing on specific slides without affecting others?

Yes. Apply locks only to shapes on the selected slides. This does not change the lock settings on other slides.

How can I apply locks to individual shapes inside a group?

Iterate through the collection returned by the group’s getShapes method and apply the appropriate locks to each child shape. Repeat this for any nested groups. The examples above process only the slide’s shape collection, so they do not set locks on individual group members.