After finishing designs of shapes in slides, accidental changes might happen if we haven't made necessary protecting settings. Locking shapes from being selected firstly or preventing changes to shape attributes (like size, position, rotation etc.) can be used to prevent changes to shapes. It's necessary to mention that Spire.Presentation supports the features to prevent changes to shapes. This article is going to introduce how to prevent or allow changes to shapes in C# using Spire.Presentation.
Note: before start, please download the latest version of Spire.Presentation and add the .dll in the bin folder as the reference of Visual Studio.
Step 1: create a presentation file and add a sample shape.
Presentation presentation = new Presentation(); IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 100, 450, 150));
Step 2: format the sample shape.
shape.Fill.FillType = FillFormatType.None; shape.ShapeStyle.LineColor.Color = Color.DarkGreen; shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Justify; shape.TextFrame.Text = "Demo for locking shapes:\n Green/Black stands for editable.\n Grey points stands for non-editable."; shape.TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial Rounded MT Bold"); shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid; shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;
Step 3: set which operations are disabled on the shape to prevent changes. Here the selection and rotation changing of the shape are allowed while the changes of size, position, shape type, text, rotation, handles, and aspect ratio are not allowed.
shape.Locking.RotationProtection = false; shape.Locking.SelectionProtection = false; shape.Locking.ResizeProtection = true; shape.Locking.PositionProtection = true; shape.Locking.ShapeTypeProtection = true; shape.Locking.AspectRatioProtection = true; shape.Locking.TextEditingProtection = true; shape.Locking.AdjustHandlesProtection = true;
Step 4: save the document and launch to see effects.
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx");
Effects:
Full codes:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; namespace test { class Program { static void Main(string[] args) { Presentation presentation = new Presentation(); IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 100, 450, 150)); shape.Fill.FillType = FillFormatType.None; shape.ShapeStyle.LineColor.Color = Color.DarkGreen; shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Justify; shape.TextFrame.Text = "Demo for locking shapes:\n Green/Black stands for editable.\n Grey stands for non-editable."; shape.TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial Rounded MT Bold"); shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid; shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black; shape.Locking.RotationProtection = false; shape.Locking.SelectionProtection = false; shape.Locking.ResizeProtection = true; shape.Locking.PositionProtection = true; shape.Locking.ShapeTypeProtection = true; shape.Locking.AspectRatioProtection = true; shape.Locking.TextEditingProtection = true; shape.Locking.AdjustHandlesProtection = true; presentation.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx"); } } }