Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 11, 2022 14:09
Show Gist options
  • Save aspose-com-gists/345dbabceedbf2feda6da683b6cca6e2 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/345dbabceedbf2feda6da683b6cca6e2 to your computer and use it in GitHub Desktop.
Lock Shapes in PowerPoint PPT in 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);
}
// 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment