Lock unlock shapes in PowerPoint PPT in Java

This article provides you with a comprehensive guide to lock shapes in PowerPoint PPT in Java. Thus, enabling you to protect the content of your PowerPoint presentations. Locking shapes could be useful for various reasons including prevention from accidental changes, protection of brand identity, maintaining layout integrity, and so on. So let’s proceed and see how to lock or unlock shapes in a presentation in Java.

Java Library to Lock Shapes in PowerPoint PPT

To lock and unlock PowerPoint presentations, we will use Aspose.Slides for Java. It is a feature-rich Java library to create and manipulate presentation documents. You can either download the library or install it using the following dependency in pom.xml.

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>23.7</version>
    <classifier>jdk16</classifier>
</dependency>

Lock Shapes in PowerPoint PPT in Java

PowerPoint presentations are composed of a variety of elements such as text, images, audio, etc. Aspose.Slides for Java takes each element as a Shape or an abject derived from Shape. So if you lock all the shapes in a presentation, you can protect PPT from being modified.

Aspose.Slides for Java classifies PowerPoint shapes into the following types:

  • Auto Shape
  • Group Shape
  • Connector
  • Picture Frame
  • Graphical Objects

Let’s now have a look at how to lock shapes in a PowerPoint PPT in Java.

  • First, load the PPT/PPTX file using Presentation class.
  • Then, get slides in the presentation using Presentation.getSlides() method.
  • For each slide, access its shapes using ISlide.getShapes() method.
  • For each shape in the collection, perform the following steps:
    • Check the type of the shape.
    • Use appropriate lock according to the type of the shape.
  • Finally, save the presentation using Presentation.save(String, SaveFormat) method.

The following code sample shows how to lock shapes in a PowerPoint PPT using Java.

try {
//Load presentation file
Presentation pTemplate = new Presentation("presentation.pptx");
//ISlide object for accessing the slides in the presentation
ISlide slide = pTemplate.getSlides().get_Item(0);
//IShape object for holding temporary shapes
IShape shape;
//Traverse through all the slides in the presentation
for (int slideCount = 0; slideCount < pTemplate.getSlides().size(); slideCount++) {
slide = pTemplate.getSlides().get_Item(slideCount);
//Traverse through all the shapes in the slides
for (int count = 0; count < slide.getShapes().size(); count++) {
shape = slide.getShapes().get_Item(count);
//if shape is auto shape
if (shape instanceof IAutoShape) {
//Type casting to Auto shape and getting auto shape lock
IAutoShape Ashp = (IAutoShape) shape;
IAutoShapeLock AutoShapeLock = (IAutoShapeLock) Ashp.getShapeLock();
//Apply shape locks
AutoShapeLock.setPositionLocked(true);
AutoShapeLock.setSelectLocked(true);
AutoShapeLock.setSizeLocked(true);
}
//if shape is group shape
else if (shape instanceof IGroupShape) {
//Type casting to group shape and getting group shape lock
IGroupShape Group = (IGroupShape) shape;
IGroupShapeLock groupShapeLock = (IGroupShapeLock) Group.getShapeLock();
//Apply shape locks
groupShapeLock.setGroupingLocked(true);
groupShapeLock.setPositionLocked(true);
groupShapeLock.setSelectLocked(true);
groupShapeLock.setSizeLocked(true);
}
//if shape is a connector
else if (shape instanceof IConnector) {
//Type casting to connector shape and getting connector shape lock
IConnector Conn = (IConnector) shape;
IConnectorLock ConnLock = Conn.getShapeLock();
//Apply shape locks
ConnLock.setPositionMove(true);
ConnLock.setSelectLocked(true);
ConnLock.setSizeLocked(true);
}
//if shape is picture frame
else if (shape instanceof IPictureFrame) {
//Type casting to pitcture frame shape and getting picture frame shape lock
IPictureFrame Pic = (IPictureFrame) shape;
IPictureFrameLock PicLock = (IPictureFrameLock) Pic.getShapeLock();
//Apply shape locks
PicLock.setPositionLocked(true);
PicLock.setSelectLocked(true);
PicLock.setSizeLocked(true);
}
}
}
//Save the presentation file
pTemplate.save("ProtectedSample.pptx", SaveFormat.Pptx);
} catch (Exception e) {
}

Unlock PowerPoint Shapes in Java

To unlock the locked shapes in a PowerPoint PPT, you only need to turn off the locks by setting their values to false. It is worth noting that the shapes that are locked using Aspose.Slides for Java can not be unlocked using any other library.

The following code sample shows how to unlock shapes in a PPTX file in Java.

try {
//Load presentation file
Presentation pTemplate = new Presentation("presentation.pptx");
//ISlide object for accessing the slides in the presentation
ISlide slide = pTemplate.getSlides().get_Item(0);
//IShape object for holding temporary shapes
IShape shape;
//Traverse through all the slides in the presentation
for (int slideCount = 0; slideCount < pTemplate.getSlides().size(); slideCount++) {
slide = pTemplate.getSlides().get_Item(slideCount);
//Traverse through all the shapes in the slides
for (int count = 0; count < slide.getShapes().size(); count++) {
shape = slide.getShapes().get_Item(count);
//if shape is auto shape
if (shape instanceof IAutoShape) {
//Type casting to Auto shape and getting auto shape lock
IAutoShape Ashp = (IAutoShape) shape;
IAutoShapeLock AutoShapeLock = (IAutoShapeLock) Ashp.getShapeLock();
//Unlock shape
AutoShapeLock.setPositionLocked(false);
AutoShapeLock.setSelectLocked(false);
AutoShapeLock.setSizeLocked(false);
}
//if shape is group shape
else if (shape instanceof IGroupShape) {
//Type casting to group shape and getting group shape lock
IGroupShape Group = (IGroupShape) shape;
IGroupShapeLock groupShapeLock = (IGroupShapeLock) Group.getShapeLock();
//Unlock shape
groupShapeLock.setGroupingLocked(false);
groupShapeLock.setPositionLocked(false);
groupShapeLock.setSelectLocked(false);
groupShapeLock.setSizeLocked(false);
}
//if shape is a connector
else if (shape instanceof IConnector) {
//Type casting to connector shape and getting connector shape lock
IConnector Conn = (IConnector) shape;
IConnectorLock ConnLock = Conn.getShapeLock();
//Unlock shape
ConnLock.setPositionMove(false);
ConnLock.setSelectLocked(false);
ConnLock.setSizeLocked(false);
}
//if shape is picture frame
else if (shape instanceof IPictureFrame) {
//Type casting to pitcture frame shape and getting picture frame shape lock
IPictureFrame Pic = (IPictureFrame) shape;
IPictureFrameLock PicLock = (IPictureFrameLock) Pic.getShapeLock();
//Unlock shape
PicLock.setPositionLocked(false);
PicLock.setSelectLocked(false);
PicLock.setSizeLocked(false);
}
}
}
//Save the presentation file
pTemplate.save("ProtectedSample.pptx", SaveFormat.Pptx);
} catch (Exception e) {
}

Lock PPT Shapes for Free

Use Aspose.Slides for Java and lock PPT shapes without evaluation limitations by getting a free temporary license.

Online Tool to Lock PowerPoint Presentations

Use our free online tool to lock PowerPoint presentations. No need to register yourself or provide any other information.

Conclusion

In this article, you have learned how to lock shapes in a PowerPoint PPT in Java. Also, you have seen how to unlock the PPT shapes programmatically. You can easily use this shape locking mechanism to protect your PowerPoint presentations.

Apart from that, you can explore more about Aspose.Slides for Java by visiting the documentation. Also, you can post your queries to our forum.

See Also