Restrict Presentation Edits with Shape Locks on Android

Overview

A common use for Aspose.Slides for Android 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 Android via Java, each element on a slide is represented by an object that implements the IShape interface or inherits from a class that does.

Different shape types expose different locking options. The IBaseShapeLock interface is the base interface for shape locks. Aspose.Slides for Android via Java provides the following lock interfaces:

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 Android via Java provides the IShape interface 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.

import com.aspose.slides.*;

Presentation presentation = new Presentation("Sample.pptx");
try {
    for (ISlide slide : presentation.getSlides()) {
        for (IShape shape : slide.getShapes()) {
            if (shape instanceof IAutoShape) {
                IAutoShape autoShape = (IAutoShape) shape;
                IAutoShapeLock autoShapeLock = (IAutoShapeLock) autoShape.getShapeLock();
                autoShapeLock.setPositionLocked(true);
                autoShapeLock.setSelectLocked(true);
                autoShapeLock.setSizeLocked(true);
            } else if (shape instanceof IGroupShape) {
                IGroupShape groupShape = (IGroupShape) shape;
                IGroupShapeLock groupShapeLock = (IGroupShapeLock) groupShape.getShapeLock();
                groupShapeLock.setGroupingLocked(true);
                groupShapeLock.setPositionLocked(true);
                groupShapeLock.setSelectLocked(true);
                groupShapeLock.setSizeLocked(true);
            } else if (shape instanceof IConnector) {
                IConnector connectorShape = (IConnector) shape;
                IConnectorLock connectorShapeLock = connectorShape.getShapeLock();
                connectorShapeLock.setPositionMove(true);
                connectorShapeLock.setSelectLocked(true);
                connectorShapeLock.setSizeLocked(true);
            } else if (shape instanceof IPictureFrame) {
                IPictureFrame pictureFrame = (IPictureFrame) shape;
                IPictureFrameLock pictureFrameLock = (IPictureFrameLock) pictureFrame.getShapeLock();
                pictureFrameLock.setPositionLocked(true);
                pictureFrameLock.setSelectLocked(true);
                pictureFrameLock.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 IAutoShapeLock, IGroupShapeLock, IConnectorLock, and IPictureFrameLock. It also removes the grouping restriction by passing false to IGroupShapeLock.setGroupingLocked and saves a copy of the presentation. Other lock settings are left unchanged.

import com.aspose.slides.*;

Presentation presentation = new Presentation("ProtectedSample.pptx");
try {
    for (ISlide slide : presentation.getSlides()) {
        for (IShape shape : slide.getShapes()) {
            if (shape instanceof IAutoShape) {
                IAutoShape autoShape = (IAutoShape) shape;
                IAutoShapeLock autoShapeLock = (IAutoShapeLock) autoShape.getShapeLock();
                autoShapeLock.setPositionLocked(false);
                autoShapeLock.setSelectLocked(false);
                autoShapeLock.setSizeLocked(false);
            } else if (shape instanceof IGroupShape) {
                IGroupShape groupShape = (IGroupShape) shape;
                IGroupShapeLock groupShapeLock = (IGroupShapeLock) groupShape.getShapeLock();
                groupShapeLock.setGroupingLocked(false);
                groupShapeLock.setPositionLocked(false);
                groupShapeLock.setSelectLocked(false);
                groupShapeLock.setSizeLocked(false);
            } else if (shape instanceof IConnector) {
                IConnector connectorShape = (IConnector) shape;
                IConnectorLock connectorShapeLock = connectorShape.getShapeLock();
                connectorShapeLock.setPositionMove(false);
                connectorShapeLock.setSelectLocked(false);
                connectorShapeLock.setSizeLocked(false);
            } else if (shape instanceof IPictureFrame) {
                IPictureFrame pictureFrame = (IPictureFrame) shape;
                IPictureFrameLock pictureFrameLock = (IPictureFrameLock) pictureFrame.getShapeLock();
                pictureFrameLock.setPositionLocked(false);
                pictureFrameLock.setSelectLocked(false);
                pictureFrameLock.setSizeLocked(false);
            }
        }
    }

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

Conclusion

Aspose.Slides for Android 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.