How to lock a text in shape in a PowerPoint presentation

drazanjarak

Drazan-Jarak

Posted on August 12, 2020

How to lock a text in shape in a PowerPoint presentation

Introduction and the problem

The tool used for doing this is Aspose.Slides.
The issue here is that after locking the shape, (when double click on it) the text within the shape is still editable.
This is the code that should usually work to lock a shape:
C#:

//Applying shapes locks
AutoShapeLock.PositionLocked = true;
AutoShapeLock.SelectLocked = true;
AutoShapeLock.SizeLocked = true;
AutoShapeLock.AdjustHandlesLocked = true;
AutoShapeLock.ArrowheadsLocked = true;
AutoShapeLock.AspectRatioLocked = true;
AutoShapeLock.EditPointsLocked = true;
AutoShapeLock.GroupingLocked = true;
AutoShapeLock.PositionLocked = true;
AutoShapeLock.RotateLocked = true;
AutoShapeLock.ShapeTypeLocked = true;
AutoShapeLock.SizeLocked = true;
AutoShapeLock.TextLocked = true;

The main problem is in the fact that it is not possible to do this even when using MS PowerPoint.

Solution/workaround

So, the idea behind this article is to explore a workaround and an approach from a different perspective or think outside of the box. Theoretically, using a transparent rectangle shape makes it an easy solution by overlaying it in front of the text box or shape and lock it. Programmatically, it would look like this code snippet:

  Presentation pr = new Presentation(path + "Before.pptx");
  //Traversing through all the shapes in the slides
  ISlide slide = pr.Slides[0];
  for (int slideCount = 0; slideCount < pr.Slides.Count; slideCount++)
  {
      slide = pr.Slides[slideCount];
      IShape shapeEx;
      int shapesCount = slide.Shapes.Count;
      for (int count = 0; count < shapesCount; count++)
      {
          shapeEx = slide.Shapes[count];
          //if shape is autoshape
          if (shapeEx is AutoShape)
          {
              //Type casting to Auto shape and getting auto shape lock
              AutoShape Ashp = shapeEx as AutoShape;
              IAutoShapeLock AutoShapeLock = Ashp.ShapeLock;
              //Applying shapes locks
              AutoShapeLock.PositionLocked = true;
              AutoShapeLock.SelectLocked = true;
              AutoShapeLock.SizeLocked = true;
              AutoShapeLock.AdjustHandlesLocked = true;
              AutoShapeLock.ArrowheadsLocked = true;
              AutoShapeLock.AspectRatioLocked = true;
              AutoShapeLock.EditPointsLocked = true;
              AutoShapeLock.GroupingLocked = true;
              AutoShapeLock.PositionLocked = true;
              AutoShapeLock.RotateLocked = true;
              AutoShapeLock.ShapeTypeLocked = true;
              AutoShapeLock.SizeLocked = true;
              AutoShapeLock.TextLocked = true;
              //Workaround for text locking: create a transparent rectangle shape, overlay it in front of the text box and lock it
              IAutoShape transpRect = slide.Shapes.AddAutoShape(ShapeType.Rectangle, Ashp.X - 5, Ashp.Y - 5, Ashp.Width + 10, Ashp.Height + 10);
              transpRect.FillFormat.FillType = FillType.Solid;
              transpRect.FillFormat.SolidFillColor.Color = System.Drawing.Color.Transparent;
              transpRect.LineFormat.FillFormat.FillType = FillType.NoFill;

              transpRect.ShapeLock.PositionLocked = true;
              transpRect.ShapeLock.SelectLocked = true;
              transpRect.ShapeLock.SizeLocked = true;
          }
          //if shape is GraphicalObjectEx
          if (shapeEx is GraphicalObject)
          {
              //Type casting to Auto shape and getting auto shape lock
              GraphicalObject Ashp = shapeEx as GraphicalObject;
              IGraphicalObjectLock AutoShapeLock = Ashp.ShapeLock;

              //Applying shapes locks
              AutoShapeLock.PositionLocked = true;
              AutoShapeLock.SelectLocked = true;
              AutoShapeLock.SizeLocked = true;
              AutoShapeLock.AspectRatioLocked = true;
              AutoShapeLock.GroupingLocked = true;
              AutoShapeLock.PositionLocked = true;
              AutoShapeLock.SizeLocked = true;
          }

          //if shape is group shape
          else if (shapeEx is GroupShape)
          {
              //Type casting to group shape and getting group shape loc
              GroupShape Group = shapeEx as GroupShape;
              IGroupShapeLock groupShapeLock = Group.ShapeLock;

              //Applying shapes locks
              groupShapeLock.PositionLocked = true;
              groupShapeLock.SelectLocked = true;
              groupShapeLock.SizeLocked = true;
              groupShapeLock.AspectRatioLocked = true;
              groupShapeLock.GroupingLocked = true;
              groupShapeLock.PositionLocked = true;
              groupShapeLock.SizeLocked = true;
          }

          //if shape is Connector shape
          else if (shapeEx is Connector)
          {
              //Type casting to connector shape and getting connector shape lock
              Connector Conn = shapeEx as Connector;
              IConnectorLock ConnLock = Conn.ShapeLock;
              //Applying shapes locks
              ConnLock.PositionMove = false;
              ConnLock.SelectLocked = true;
              ConnLock.SizeLocked = true;
              ConnLock.AdjustHandlesLocked = true;
              ConnLock.ArrowheadsLocked = true;
              ConnLock.AspectRatioLocked = true;
              ConnLock.EditPointsLocked = true;
              ConnLock.GroupingLocked = true;
              ConnLock.RotateLocked = true;
              ConnLock.ShapeTypeLocked = true;
              ConnLock.SizeLocked = true;
          }

          //if shape is picture frame
          else if (shapeEx is PictureFrame)

              //Type casting to picture frame shape and getting picture frame shape lock
              PictureFrame Pic = shapeEx as PictureFrame;
              IPictureFrameLock PicLock = Pic.ShapeLock;
              //Applying shapes locks
              PicLock.PositionLocked = true;
              PicLock.SelectLocked = true;
              PicLock.SizeLocked = true;
              PicLock.AdjustHandlesLocked = true;
              PicLock.ArrowheadsLocked = true;
              PicLock.AspectRatioLocked = true;
              PicLock.EditPointsLocked = true;
              PicLock.GroupingLocked = true;
              PicLock.PositionLocked = true;
              PicLock.ShapeTypeLocked = true;
              PicLock.SizeLocked = true;
          }
      }
  }
  pr.Save(path + @"output.pptx", Aspose.Slides.Export.SaveFormat.Pptx);

This example shows a simple and easy workaround on how to lock all shapes in a presentation as well as text within shapes using Aspose.Slides.
I hope this article will be useful for others encountering the same problem.

💖 💪 🙅 🚩
drazanjarak
Drazan-Jarak

Posted on August 12, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related