Lock unlock shapes in PowerPoint PPT in C#

The protection of digital documents is a common concern these days. You don’t want any unauthorized person to change the content of your documents. Therefore, it becomes inevitable to use effective protection mechanisms. Accordingly, in this article, we will show you how to protect PPT/PPTX presentations from within your .NET applications. We will accomplish that by locking the shapes in a PowerPoint PPT or PPTX in C#.

C# .NET API to Lock Shapes in PowerPoint PPT

To lock and unlock PowerPoint presentations, we will use Aspose.Slides for .NET. The API provides a range of features to create and manipulate PowerPoint presentations. You can either download API’s DLL or install it using NuGet.

PM> Install-Package Aspose.Slides.NET

Lock Shapes in PowerPoint PPT in C#

A PowerPoint presentation may contain a variety of elements such as text, images, audio, etc. Aspose.Slides for .NET takes each element as a Shape or an abject derived from Shape. So, to protect the content of the presentations, we need to lock all their shapes. Aspose.Slides provides appropriate locks for the following types of shapes.

  • Auto Shape
  • Group Shape
  • Connector
  • Picture Frame

The following steps show how to lock shapes in a PowerPoint PPT in C#.

  • First, load the PPT/PPTX file using Presentation class.
  • Then, get slides in the presentation using Presentation.Slides property.
  • For each slide, access its shapes using ISlide.Shapes collection.
  • For each shape in the collection, perform the following steps:
    • Check type of the shape.
    • Use appropriate lock according to 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 PPTX using C#.

// Load presentation
using (Presentation presentation = new Presentation("presentation.pptx"))
{
// IShape object for holding temporary shapes
IShape shape;
// Traverse through all the slides in the presentation
for (int slideCount = 0; slideCount < presentation.Slides.Count; slideCount++)
{
var slide = presentation.Slides[slideCount];
// Travese through all the shapes in the slides
for (int count = 0; count < slide.Shapes.Count; count++)
{
shape = slide.Shapes[count];
// If shape is auto shape
if (shape is IAutoShape)
{
// Type cast to auto shape and get auto shape lock
IAutoShape Ashp = shape as IAutoShape;
IAutoShapeLock AutoShapeLock = Ashp.ShapeLock;
// Apply shape locks
AutoShapeLock.PositionLocked = true;
AutoShapeLock.SelectLocked = true;
AutoShapeLock.SizeLocked = true;
}
// If shape is group shape
else if (shape is IGroupShape)
{
// Type cast to group shape and get group shape lock
IGroupShape Group = shape as IGroupShape;
IGroupShapeLock groupShapeLock = Group.ShapeLock;
// Apply shape locks
groupShapeLock.GroupingLocked = true;
groupShapeLock.PositionLocked = true;
groupShapeLock.SelectLocked = true;
groupShapeLock.SizeLocked = true;
}
// If shape is a connector
else if (shape is IConnector)
{
// Type cast to connector shape and get connector shape lock
IConnector Conn = shape as IConnector;
IConnectorLock ConnLock = Conn.ShapeLock;
// Apply shape locks
ConnLock.PositionMove = true;
ConnLock.SelectLocked = true;
ConnLock.SizeLocked = true;
}
// If shape is picture frame
else if (shape is IPictureFrame)
{
// Type cast to pitcture frame shape and get picture frame shape lock
IPictureFrame Pic = shape as IPictureFrame;
IPictureFrameLock PicLock = Pic.ShapeLock;
// Apply shape locks
PicLock.PositionLocked = true;
PicLock.SelectLocked = true;
PicLock.SizeLocked = true;
}
}
}
// Save presentation
presentation.Save("locked-ppt.pptx", SaveFormat.Pptx);
}

Unlock PowerPoint Presentations in C#

To unlock the shapes and make them editable, you will need to turn off the locks. Please note that if you have locked the shapes using Aspose.Slides for .NET then you will have to use the same API for unlocking. Unlocking is done by disabling the shape locks and setting their values to false.

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

// Load presentation
using (Presentation presentation = new Presentation("locked-ppt.ppt"))
{
// IShape object for holding temporary shapes
IShape shape;
// Traverse through all the slides in the presentation
for (int slideCount = 0; slideCount < presentation.Slides.Count; slideCount++)
{
var slide = presentation.Slides[slideCount];
// Travese through all the shapes in the slides
for (int count = 0; count < slide.Shapes.Count; count++)
{
shape = slide.Shapes[count];
// If shape is auto shape
if (shape is IAutoShape)
{
// Type cast to auto shape and get auto shape lock
IAutoShape Ashp = shape as IAutoShape;
IAutoShapeLock AutoShapeLock = Ashp.ShapeLock;
// Unlock shape
AutoShapeLock.PositionLocked = false;
AutoShapeLock.SelectLocked = false;
AutoShapeLock.SizeLocked = false;
}
// If shape is group shape
else if (shape is IGroupShape)
{
// Type cast to group shape and get group shape lock
IGroupShape Group = shape as IGroupShape;
IGroupShapeLock groupShapeLock = Group.ShapeLock;
// Unlock shape
groupShapeLock.GroupingLocked = false;
groupShapeLock.PositionLocked = false;
groupShapeLock.SelectLocked = false;
groupShapeLock.SizeLocked = false;
}
// If shape is a connector
else if (shape is IConnector)
{
// Type cast to connector shape and get connector shape lock
IConnector Conn = shape as IConnector;
IConnectorLock ConnLock = Conn.ShapeLock;
// Unlock shape
ConnLock.PositionMove = false;
ConnLock.SelectLocked = false;
ConnLock.SizeLocked = false;
}
// If shape is picture frame
else if (shape is IPictureFrame)
{
// Type cast to pitcture frame shape and get picture frame shape lock
IPictureFrame Pic = shape as IPictureFrame;
IPictureFrameLock PicLock = Pic.ShapeLock;
// Unlock shape
PicLock.PositionLocked = false;
PicLock.SelectLocked = false;
PicLock.SizeLocked = false;
}
}
}
// Save presentation
presentation.Save("unlocked-ppt.ppt", SaveFormat.Ppt);
}

Get a Free License

Use Aspose.Slides for .NET without evaluation limitations by getting a free temporary license.

Conclusion

In this article, you have learned how to lock shapes in PowerPoint PPT/PPTX in C#. Furthermore, you have seen how to unprotect the presentations by unlocking their shapes programmatically. Apart from that, you can explore more about Aspose.Slides for .NET by visiting the documentation. Also, you can post your queries to our forum.

See Also