Read the complete article on how to lock and unlock shapes in PowerPoint PPT using C#: https://blog.aspose.com/2022/02/11/lock-shapes-in-ppt-in-csharp/
Last active
February 11, 2022 14:09
-
-
Save aspose-com-gists/345dbabceedbf2feda6da683b6cca6e2 to your computer and use it in GitHub Desktop.
Lock Shapes in PowerPoint PPT in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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