article

How to Apply or Remove Protection on PPTX Shapes inside .NET Applications

Email
Submitted on: 9/9/2015 10:04:21 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 2054
 
     This technical tip explains how .NET developers can apply and remove protection to presentation. A common use for Aspose.Slides is to create, update and save Microsoft PowerPoint 2007 (PPTX) presentations as part of an automated workflow. Users of the application that uses Aspose.Slides this way get access to the output presentations. Protecting them from editing is a common concern. It is important that auto-generated presentations retain their original formatting and content. A common use for Aspose.Slides is to create, update and save Microsoft PowerPoint 2007 (PPTX) presentations as part of an automated workflow.

 
				This technical tip explains how .NET developers can apply and remove protection to presentation. A common use for Aspose.Slides is to create, update and save Microsoft PowerPoint 2007 (PPTX) presentations as part of an automated workflow. Users of the application that uses Aspose.Slides this way get access to the output presentations. Protecting them from editing is a common concern. It is important that auto-generated presentations retain their original formatting and content. A common use for Aspose.Slides is to create, update and save Microsoft PowerPoint 2007 (PPTX) presentations as part of an automated workflow. Users of the application that uses Aspose.Slides this way get access to the output presentations. Protecting them from editing is a common concern. It is important that auto-generated presentations retain their original formatting and content. This article explains how presentations and slides are constructed and how Aspose.Slides for .NET can apply protection to, and then remove it from a presentation. This feature is unique to Aspose.Slides and, at the time of writing, is not available in Microsoft PowerPoint. It gives developers a way of controlling how the presentations their applications create are used. 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 .NET, 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. 
// Applying Protection to PPTX Shapes
//[C# Code Sample]
 
//Instatiate Presentation class that represents a PPTX file
Presentation pTemplate = new Presentation(path + "RectPicFrame.pptx");//Instatiate Presentation class that represents a PPTX file
Presentation pTemplate = new Presentation("RectPicFrame.pptx");
//ISlide object for accessing the slides in the presentation
ISlide slide = pTemplate.Slides[0];
//IShape object for holding temporary shapes
IShape shape;
//Traversing through all the slides in the presentation
for (int slideCount = 0; slideCount < pTemplate.Slides.Count; slideCount++)
{
slide = pTemplate.Slides[slideCount];
//Travesing through all the shapes in the slides
for (int count = 0; count < slide.Shapes.Count; count++)
{
shape = slide.Shapes[count];
//if shape is autoshape
if (shape is IAutoShape)
{
//Type casting to Auto shape and getting auto shape lock
IAutoShape Ashp = shape as IAutoShape;
IAutoShapeLock AutoShapeLock = Ashp.ShapeLock;
//Applying shapes locks
AutoShapeLock.PositionLocked = true;
AutoShapeLock.SelectLocked = true;
AutoShapeLock.SizeLocked = true;
}
//if shape is group shape
else if (shape is IGroupShape)
{
//Type casting to group shape and getting group shape lock
IGroupShape Group = shape as IGroupShape;
IGroupShapeLock groupShapeLock = Group.ShapeLock;
//Applying shapes locks
groupShapeLock.GroupingLocked = true;
groupShapeLock.PositionLocked = true;
groupShapeLock.SelectLocked = true;
groupShapeLock.SizeLocked = true;
}
//if shape is a connector
else if (shape is IConnector)
{
//Type casting to connector shape and getting connector shape lock
IConnector Conn = shape as IConnector;
IConnectorLock ConnLock = Conn.ShapeLock;
//Applying shapes locks
ConnLock.PositionMove = true;
ConnLock.SelectLocked = true;
ConnLock.SizeLocked = true;
}
//if shape is picture frame
else if (shape is IPictureFrame)
{
//Type casting to pitcture frame shape and getting picture frame shape lock
IPictureFrame Pic = shape as IPictureFrame;
IPictureFrameLock PicLock = Pic.ShapeLock;
//Applying shapes locks
PicLock.PositionLocked = true;
PicLock.SelectLocked = true;
PicLock.SizeLocked = true;
}
}
}
//Saving the presentation file
pTemplate.Save("ProtectedSample.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
 
//[VB.NET Code Sample]
 
'Instatiate Presentation class that represents a PPTX file
Dim pTemplate As New Presentation("RectPicFrame.pptx")
'ISlide object for accessing the slides in the presentation
Dim slide As ISlide = pTemplate.Slides(0)
'IShape object for holding temporary shapes
Dim shape As IShape
'Traversing through all the slides in the presentation
For slideCount As Integer = 0 To pTemplate.Slides.Count - 1
	slide = pTemplate.Slides(slideCount)
	'Travesing through all the shapes in the slides
	For count As Integer = 0 To slide.Shapes.Count - 1
		shape = slide.Shapes(count)
		'if shape is autoshape
		If TypeOf shape Is IAutoShape Then
			'Type casting to Auto shape and getting auto shape lock
			Dim Ashp As IAutoShape = TryCast(shape, IAutoShape)
			Dim AutoShapeLock As IAutoShapeLock = Ashp.ShapeLock
			'Applying shapes locks
			AutoShapeLock.PositionLocked = True
			AutoShapeLock.SelectLocked = True
			AutoShapeLock.SizeLocked = True
		'if shape is group shape
		ElseIf TypeOf shape Is IGroupShape Then
			'Type casting to group shape and getting group shape lock
			Dim Group As IGroupShape = TryCast(shape, IGroupShape)
			Dim groupShapeLock As IGroupShapeLock = Group.ShapeLock
			'Applying shapes locks
			groupShapeLock.GroupingLocked = True
			groupShapeLock.PositionLocked = True
			groupShapeLock.SelectLocked = True
			groupShapeLock.SizeLocked = True
		'if shape is a connector
		ElseIf TypeOf shape Is IConnector Then
			'Type casting to connector shape and getting connector shape lock
			Dim Conn As IConnector = TryCast(shape, IConnector)
			Dim ConnLock As IConnectorLock = Conn.ShapeLock
			'Applying shapes locks
			ConnLock.PositionMove = True
			ConnLock.SelectLocked = True
			ConnLock.SizeLocked = True
		'if shape is picture frame
		ElseIf TypeOf shape Is IPictureFrame Then
			'Type casting to pitcture frame shape and getting picture frame shape lock
			Dim Pic As IPictureFrame = TryCast(shape, IPictureFrame)
			Dim PicLock As IPictureFrameLock = Pic.ShapeLock
			'Applying shapes locks
			PicLock.PositionLocked = True
			PicLock.SelectLocked = True
			PicLock.SizeLocked = True
		End If
	Next count
Next slideCount
'Saving the presentation file
pTemplate.Save("ProtectedSample.pptx", Aspose.Slides.Export.SaveFormat.Pptx)
// Removing Protection in Presentations
//[C# Code Sample]
 
//Open the desired presentation
Presentation pTemplate = new Presentation("ProtectedSample.pptx");
//ISlide object for accessing the slides in the presentation
ISlide slide = pTemplate.Slides[0];
//IShape object for holding temporary shapes
IShape shape;
//Traversing through all the slides in presentation
for (int slideCount = 0; slideCount < pTemplate.Slides.Count; slideCount++)
{
slide = pTemplate.Slides[slideCount];
//Travesing through all the shapes in the slides
for (int count = 0; count < slide.Shapes.Count; count++)
{
shape = slide.Shapes[count];
//if shape is autoshape
if (shape is IAutoShape)
{
//Type casting to Auto shape and getting auto shape lock
IAutoShape Ashp = shape as AutoShape;
IAutoShapeLock AutoShapeLock = Ashp.ShapeLock;
//Applying shapes locks
AutoShapeLock.PositionLocked = false;
AutoShapeLock.SelectLocked = false;
AutoShapeLock.SizeLocked = false;
}
//if shape is group shape
else if (shape is IGroupShape)
{
//Type casting to group shape and getting group shape lock
IGroupShape Group = shape as IGroupShape;
IGroupShapeLock groupShapeLock = Group.ShapeLock;
//Applying shapes locks
groupShapeLock.GroupingLocked = false;
groupShapeLock.PositionLocked = false;
groupShapeLock.SelectLocked = false;
groupShapeLock.SizeLocked = false;
}
//if shape is Connector shape
else if (shape is IConnector)
{
//Type casting to connector shape and getting connector shape lock
IConnector Conn = shape as IConnector;
IConnectorLock ConnLock = Conn.ShapeLock;
//Applying shapes locks
ConnLock.PositionMove = false;
ConnLock.SelectLocked = false;
ConnLock.SizeLocked = false;
}
//if shape is picture frame
else if (shape is IPictureFrame)
{
//Type casting to pitcture frame shape and getting picture frame shape lock
IPictureFrame Pic = shape as IPictureFrame;
IPictureFrameLock PicLock = Pic.ShapeLock;
//Applying shapes locks
PicLock.PositionLocked = false;
PicLock.SelectLocked = false;
PicLock.SizeLocked = false;
}
}
}
//Saving the presentation file
pTemplate.Save("RemoveProtectionSample.pptx",Aspose.Slides.Export.SaveFormat.Pptx);
 
//[VB.NET Code Sample]
 
'Open the desired presentation
Dim pTemplate As New Presentation(path & "ProtectedSample.pptx")
'ISlide object for accessing the slides in the presentation
Dim slide As ISlide = pTemplate.Slides(0)
'IShape object for holding temporary shapes
Dim shape As IShape
'Traversing through all the slides in presentation
For slideCount As Integer = 0 To pTemplate.Slides.Count - 1
	slide = pTemplate.Slides(slideCount)
	'Travesing through all the shapes in the slides
	For count As Integer = 0 To slide.Shapes.Count - 1
		shape = slide.Shapes(count)
		'if shape is autoshape
		If TypeOf shape Is IAutoShape Then
			'Type casting to Auto shape and getting auto shape lock
			Dim Ashp As IAutoShape = TryCast(shape, AutoShape)
			Dim AutoShapeLock As IAutoShapeLock = Ashp.ShapeLock
			'Applying shapes locks
			AutoShapeLock.PositionLocked = False
			AutoShapeLock.SelectLocked = False
			AutoShapeLock.SizeLocked = False
		'if shape is group shape
		ElseIf TypeOf shape Is IGroupShape Then
			'Type casting to group shape and getting group shape lock
			Dim Group As IGroupShape = TryCast(shape, IGroupShape)
			Dim groupShapeLock As IGroupShapeLock = Group.ShapeLock
			'Applying shapes locks
			groupShapeLock.GroupingLocked = False
			groupShapeLock.PositionLocked = False
			groupShapeLock.SelectLocked = False
			groupShapeLock.SizeLocked = False
		'if shape is Connector shape
		ElseIf TypeOf shape Is IConnector Then
			'Type casting to connector shape and getting connector shape lock
			Dim Conn As IConnector = TryCast(shape, IConnector)
			Dim ConnLock As IConnectorLock = Conn.ShapeLock
			'Applying shapes locks
			ConnLock.PositionMove = False
			ConnLock.SelectLocked = False
			ConnLock.SizeLocked = False
		'if shape is picture frame
		ElseIf TypeOf shape Is IPictureFrame Then
			'Type casting to pitcture frame shape and getting picture frame shape lock
			Dim Pic As IPictureFrame = TryCast(shape, IPictureFrame)
			Dim PicLock As IPictureFrameLock = Pic.ShapeLock
			'Applying shapes locks
			PicLock.PositionLocked = False
			PicLock.SelectLocked = False
			PicLock.SizeLocked = False
		End If
	Next count
Next slideCount
'Saving the presentation file
pTemplate.Save(path & "RemoveProtectionSample.pptx", Aspose.Slides.Export.SaveFormat.Pptx)
More about Aspose.Slides for .NET


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.