Applying Protection to Presentation

Composition of a Slide

A PPTX slide is composed of a number of components like auto shapes, tables, OLE objects, grouped shapes, picture frames, video frames, connectors and the various other elements available to build up a presentation.

In Aspose.Slides for C++, each element on a slide is turned into a Shape object. In other words, each element on the slide is either a Shape object or an object derived from the Shape object.

The structure of PPTX is complex so unlike PPT, where a generic lock can be used for all type of shapes, there are different types of locks for different shape type. The BaseShapeLock class is the generic PPTX locking class. The following types of locks are supported in Aspose.Slides for C++ for PPTX.

  • AutoShapeLock locks auto shapes.
  • ConnectorLock locks connecter shapes.
  • GraphicalObjectLock locks graphical objects.
  • GroupshapeLock locks group shapes.
  • PictureFrameLock locks picture frames.

Any action performed on all Shape objects in a Presentation object is applied to the whole presentation.

Applying and Removing Protection

Applying protection ensures that a presentation cannot be edited. It is a useful technique for protecting a presentation’s content.

Applying Protection to PPTX Shapes

Aspose.Slides for C++ provides the Shape class to handle a shape on the slide.

As mentioned earlier, each shape class has an associated shape lock class for protection. This article focuses on the NoSelect, NoMove and NoResize locks. These locks ensure that shapes cannot be selected (through mouse clicks or other selection methods), and it cannot be moved or resized.

The code samples that follow apply protection to all shapes types in a presentation.

// The path to the documents directory.
const String templatePath = u"../templates/RectPicFrame.pptx";
const String outPath = u"../out/ProtectedSample.pptx";
//Instatiate Presentation class that represents a PPTX
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
//ISlide object for accessing the slides in the presentation
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
//IShape object for holding temporary shapes
SharedPtr<IShape> shape;
//Traversing through all the slides in the presentation
for (int slideCount = 0; slideCount < pres->get_Slides()->get_Count(); slideCount++)
{
slide = pres->get_Slides()->idx_get(slideCount);
//Travesing through all the shapes in the slides
for (int count = 0; count < slide->get_Shapes()->get_Count(); count++)
{
shape = slide->get_Shapes()->idx_get(count);
if (System::ObjectExt::Is<IAutoShape>(shape)) {
//Type casting to Auto shape and getting auto shape lock
SharedPtr<IAutoShape> aShp = DynamicCast<Aspose::Slides::IAutoShape>(shape);
SharedPtr<IAutoShapeLock> autoShapeLock = DynamicCast<Aspose::Slides::IAutoShapeLock>(aShp->get_ShapeLock());
//Applying shapes locks
autoShapeLock->set_PositionLocked(true);
autoShapeLock->set_SelectLocked(true);
autoShapeLock->set_SizeLocked(true);
}
//if shape is group shape
else if (System::ObjectExt::Is<IGroupShape>(shape)) {
//Type casting to group shape and getting group shape lock
SharedPtr<IGroupShape> group = DynamicCast<Aspose::Slides::IGroupShape>(shape);
SharedPtr<IGroupShapeLock> groupShapeLock = DynamicCast<Aspose::Slides::IGroupShapeLock>(group->get_ShapeLock());
//Applying shapes locks
groupShapeLock->set_GroupingLocked(true);
groupShapeLock->set_PositionLocked(true);
groupShapeLock->set_SelectLocked(true);
groupShapeLock->set_SizeLocked(true);
}
//if shape is a connector
else if (System::ObjectExt::Is<IConnector>(shape)) {
//Type casting to connector shape and getting connector shape lock
SharedPtr<IConnector> conn = DynamicCast<Aspose::Slides::IConnector>(shape);
SharedPtr<IConnectorLock> connLock = DynamicCast<Aspose::Slides::IConnectorLock>(conn->get_ShapeLock());
//Applying shapes locks
connLock->set_PositionMove(true);
connLock->set_SelectLocked(true);
connLock->set_SizeLocked(true);
}
//if shape is picture frame
else if (System::ObjectExt::Is<IPictureFrame>(shape)) {
//Type casting to pitcture frame shape and getting picture frame shape lock
SharedPtr<IPictureFrame> pic = DynamicCast<Aspose::Slides::IPictureFrame>(shape);
SharedPtr<IPictureFrameLock> picLock = DynamicCast<Aspose::Slides::IPictureFrameLock>(pic->get_ShapeLock());
//Applying shapes locks
picLock->set_PositionLocked(true);
picLock->set_SelectLocked(true);
picLock->set_SizeLocked(true);
}
}
}
//Saving the presentation file
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Removing Protection

Protection applied using Aspose.Slides for C++ can only be removed with Aspose.Slides for C++. To unlock a shape, set the value of the applied lock to false. The code sample that follows shows how to unlock shapes in a locked presentation.

// The path to the documents directory.
const String templatePath = u"../templates/ProtectedSample.pptx";
const String outPath = u"../out/RemoveProtectionSample.pptx";
//Instatiate Presentation class that represents a PPTX
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
//ISlide object for accessing the slides in the presentation
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
//IShape object for holding temporary shapes
SharedPtr<IShape> shape;
//Traversing through all the slides in the presentation
for (int slideCount = 0; slideCount < pres->get_Slides()->get_Count(); slideCount++)
{
slide = pres->get_Slides()->idx_get(slideCount);
//Travesing through all the shapes in the slides
for (int count = 0; count < slide->get_Shapes()->get_Count(); count++)
{
shape = slide->get_Shapes()->idx_get(count);
if (System::ObjectExt::Is<IAutoShape>(shape)) {
//Type casting to Auto shape and getting auto shape lock
SharedPtr<IAutoShape> aShp = DynamicCast<Aspose::Slides::IAutoShape>(shape);
SharedPtr<IAutoShapeLock> autoShapeLock = DynamicCast<Aspose::Slides::IAutoShapeLock>(aShp->get_ShapeLock());
//Applying shapes locks
autoShapeLock->set_PositionLocked(false);
autoShapeLock->set_SelectLocked(false);
autoShapeLock->set_SizeLocked(false);
}
//if shape is group shape
else if (System::ObjectExt::Is<IGroupShape>(shape)) {
//Type casting to group shape and getting group shape lock
SharedPtr<IGroupShape> group = DynamicCast<Aspose::Slides::IGroupShape>(shape);
SharedPtr<IGroupShapeLock> groupShapeLock = DynamicCast<Aspose::Slides::IGroupShapeLock>(group->get_ShapeLock());
//Applying shapes locks
groupShapeLock->set_GroupingLocked(false);
groupShapeLock->set_PositionLocked(false);
groupShapeLock->set_SelectLocked(false);
groupShapeLock->set_SizeLocked(false);
}
//if shape is a connector
else if (System::ObjectExt::Is<IConnector>(shape)) {
//Type casting to connector shape and getting connector shape lock
SharedPtr<IConnector> conn = DynamicCast<Aspose::Slides::IConnector>(shape);
SharedPtr<IConnectorLock> connLock = DynamicCast<Aspose::Slides::IConnectorLock>(conn->get_ShapeLock());
//Applying shapes locks
connLock->set_PositionMove(false);
connLock->set_SelectLocked(false);
connLock->set_SizeLocked(false);
}
//if shape is picture frame
else if (System::ObjectExt::Is<IPictureFrame>(shape)) {
//Type casting to pitcture frame shape and getting picture frame shape lock
SharedPtr<IPictureFrame> pic = DynamicCast<Aspose::Slides::IPictureFrame>(shape);
SharedPtr<IPictureFrameLock> picLock = DynamicCast<Aspose::Slides::IPictureFrameLock>(pic->get_ShapeLock());
//Applying shapes locks
picLock->set_PositionLocked(false);
picLock->set_SelectLocked(false);
picLock->set_SizeLocked(false);
}
}
}
//Saving the presentation file
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Summary